I have a page that has 5 user controls of the same typ. Each user control has a gridview and a couple of buttons.
I don't understand why the controls on the user controls don't retain their value.
EDIT
Below is how I'm populating the gridview inside a user control.
protected void Page_Load(object sender, EventArgs e)
{
var list = new List<CKListByDprtment>
{
new CKListByDprtment { ID = 1, TaskName = "New laptop needed",
CompletedBy = "My Boss", HMReq = true },
new CKListByDprtment { ID = 2, TaskName = "New Workstation needed",
CompletedBy = "My Boss", HMReq = false }
};
gvCKList.DataSource = list;
gvCKList.DataBind();
}
#endregion
#region GridView Validation
protected void cvValidateGrid_ServerValidate(object sender, ServerValidateEventArgs args)
{
ValidateGrid();
}
private void ValidateGrid()
{
foreach (GridViewRow gvRow in gvCKList.Rows)
{
CheckBox ckHMreq = gvRow.Cells[0].FindControl("ckHMreq") as CheckBox;
}
}
I tried to add a checkbox on the main aspx page. When the page posted back, the checked was able to retain its value. I tried also on the user control but not inside the gridview. When the postback, the checkbox retained its value.
It looks like only inside the gridview where controls lose their values after a postback. I have added ViewStateMode = "Enabled" first on the aspx page, then on the usercontrol and finaly on the gridview itself, but with no success.
Any reason?
if(!Page.IsPostBack)
around the usercontrol population code? – Shai Cohen