Reputation: 1249
It's been a while since I worked on WebForms so I need a refresher when working on an old site.
I have a userControl on the page that I need to programatically set the enabled state
<%@ Register Src="CalandarControl.ascx" TagName="CalandarControl" TagPrefix="uc" %>
I have this at the C# code but Enabled is not available here. What am I missing?
if (c is UserControl)
{
var x = c.GetType();
if (x.Name == "calendarcontrol_ascx")
{
((UserControl)c).Enabled = true;
}
}
Thanks
Upvotes: 2
Views: 2749
Reputation: 760
you can define a panel in usercontrol witch cotaines all of controls of the user control, then define a property Enabled named as bool (panelMain.Enabled;) and from ur page set it,
in user control ascx
<asp:panel runat="server" id="panelMain" Enabled="false">
<!-- define ur other controls between panel-->
</asp:panel>
in usercontrol ascx.cs
public bool Enabled
{
get { return panelMain.Enabled; }
set { panelMain.Enabled = value; }
}
in page first register ur usercontrol and then set Enabled property from code behind ....
for example
protected void Page_load(object sender,EventArgs e )
{
panelMain.Enabled = true;
}
as simple as drink water :))
Upvotes: 1
Reputation: 15797
You should have something on the code-front that places the control on the page, like:
<uc:CalendarControl ID="dtePrepaymentExpiresDate" FieldName="Prepayment expires date" runat="server" Enabled="false" />
Then in the code behind, you can set this custom property as follows:
dtePrepaymentExpiresDate.Enabled = true;
If you really need to do it in the loop, then you need to cast c
as the CalendarControl
and not UserControl
because CalendarControl
has the property Enabled
while a normal UserControl
does not.
((CalandarControl)c).Enabled = true;
Upvotes: 2