I have three pages with very similiar behaviour, so I made a User Control with 3 behaviours, I did this with adding an enum and a property of this enum type.
public enum ucType
{
CustomersWhoHaveAContract, CustomersWaitingForContract, CustomerOfPreReservedContracts
}
public ucType UserControlType;
protected void BtnLoadInfo_Click(object sender, ImageClickEventArgs e)
{
switch (UserControlType)
{
case ucType.CustomersWhoHaveAContract:
DoA();
break;
case ucType.CustomersWaitingForContract:
DoB();
break;
case ucType.CustomerOfPreReservedContracts:
DoC();
break;
default:
break;
}
in my pages I assign a value to the UserControlType,
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
ucCustomersWithContract1.UserControlType = UserControls.ucCustomersWithContract.ucType.CustomerOfPreReservedContracts;
}
}
but when I click the button the UserControlType is always CustomersWhoHaveAContract, meaning that it is losing it’s value. Where is the problem?
You mean ASP.NET WebForms, right?
Controls don’t get all their data automaticaly restored, there is ViewState mechanism.
MSDN article
http://msdn.microsoft.com/en-us/library/ms972976.aspx
To fix an example, change your field into property:
and it should work.