How to pass value in a constructor to enable or disable a button in asp.net control?
I am having a constructo开发者_高级运维r on my user control From a main page I just reinitialize the user control.
I tried
//Constructor
UserControl(isNew)
{
this.btnAdd.Enabled=true;
}
BtnAdd is the (aspx button runat server) control I want to disable.
While compiling it does not shows error.But while runtime It throws error saying btnAddis null.
Object reference not set to an instance of an object.
for btnAdd
. Thnaks
Use the Page_Init event and a property.
[BrowsableAttribute(True)]
[DefaultValue("true")]
public bool IsNew { get; set; }
protected void Page_Init(object sender, EventArgs e)
{
btnAdd = IsNew;
}
Why don't you simply make the Button enabled by default?
<asp:Button ID="btnAdd" Enabled="true" runat="server" Text="Add" />
You shouldn't use the constructor of the UserControl, because the content of the UserControl(as your Button) will be initialized after the UserControl itself. If you create the UserControl dynamically, you should use LoadControl to intitialize it. Expose a property in this UC (f.e. CanAdd as Boolean) so that you can control this behaviour from outside(f.e. Page).
精彩评论