Hi all,
I have the issue and I'm unable to solve it.
I have programatically created gridview, where is Templatefield and dropdownlist inside.
I created TemplateField following this manual: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.templatefield.templatefield.aspx
I declare my gridview in OnInit() method and do databind() in Page_PreRender(). The problem is the creating of dropdownlists in Templatefield is fire up on databind() in Page_PreRender() and ViewState can't catch it, so I'm unable to reach changed values after PostBack.
Please can you tell me the correct way how to manage it ?
override protected void OnInit(EventArgs e)
{
//...
GridView gridView = new GridView();
gridView.ID = "gv" + depItem["dptId"];
gridView.RowCommand += new GridViewCommandEventHandler(delItem);
gridView.EnableViewState = true;
TemplateField field = new TemplateField();
field.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, headerTextFromLocRes);
gridView.Columns.Add(field);
pnContent.Controls.Add(gridView);
//...
}
protected void Page_PreRender(object sender, EventArgs e)
{
//...
GridView gridView = (GridView)pnContent.FindControl("gv" + depItem["dptId"]);
gridView.DataSource = tasks;
gridView.DataBind();
//...
}
//-------
public class GridViewTemplate : ITemplate
{
private DataControlRowType templateType;
private string columnName;
public GridViewTemplate(DataControlRowType type, string colname)
{
templateType = type;
columnName = colname;
}
public void InstantiateIn(System.Web.UI.Control container)
{
switch (templateType)
{
case DataControlRowType.Header:
Literal lc = new Literal();
lc.Text = "<b>" + columnName + "</b>";
container.Controls.Add(lc);
break;
case DataControlRowType.DataRow:
DropDownList priorityList = new DropDownList();
priorityList.DataBinding += new EventHandler(this.PriorityList_DataBinding);
container.Controls.Add(priorityList);
break;
default:
break;
}
}
private void PriorityList_DataBinding(Object sender, EventArgs e)
{
DropDownList priorityList = (DropDownList)sender;
GridViewRow row = (GridViewRow)priorityList.NamingContainer;
//...
priorityList.DataSource = loop;
priorityList.ID = "ddlPriority" + dptId;
priorityList.SelectedValue = selectedValue.ToString();
string order = (selectedValue - 1).ToString();
//..
}
}