I have three usercontrols: usercontrolA, usercontrolB and usercontrolC which all share the same codebehind each having:
<%@ Control Language="c#" AutoEventWireup="True" Codebehind="usercontrolA.ascx.cs" Inherits="usercontrolA" TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %> at the top of the ascx.
In the codebehind file I have a public property called ShowAll.
I know I can set this property when I put the usercontrol on the page e.g.
<uc1:usercontrolB ID="usercontrolB1" runat="server" ShowAll="true" />
However I would like ShowAll to always be set to true on usercontrolB so would rather not have to set it every time it is placed on a page.
I know I can add a script tag to usercontrolB to set ShowAll in Page_Load:
<script runat="server">
protected void Page_Load(object sender, System.EventArgs e)
{
ShowAll = true;
}
</script>
But want to keep the Page_Load implementation I already have in the codebehind. Is there any other way to set this property automatically for usercontrolB?
Edit: If it’s possible I’d like to be able to set this in the ascx rather than in the code behind, so that someone else later on could add usercontrolD and set ShowAll to true for all instances of usercontrolD without needing to get me to modify and recompile the codebehind.
You have to set this in usercontrol class constructor.
here is complete example with code…
I set the default value to true, but you can also pass the value where you add this user control. e.g.
When this is called, it will overwrite the value to
false