Hi,
I have the following code:
protected override void OnInit(EventArgs e) { base.OnInit(e); myTemplate temp = new myTemplate(); ItemTemplate template = new ItemTemplate();
template.Template = temp; template.TemplateID = "template1";
WebDataGrid1.Templates.Add(template); }
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { TemplateDataField templateColumn1 = (TemplateDataField)this.WebDataGrid1.Columns["TemplateColumn1"]; templateColumn1.ItemTemplate = new myTemplate1();
WebDataGrid1.DataSource = populateGrid(); WebDataGrid1.DataBind(); }
}
protected DataTable populateGrid() { DataTable dt = new DataTable(); dt.Columns.Add("ID"); dt.Columns.Add("Name"); DataRow row; for (int i = 0; i < 10; i++) { row = dt.NewRow(); row.SetField<Int32>("ID", i); row.SetField<string>("Name", "Name" + i); dt.Rows.Add(row); }
return dt; }
protected void WebDataGrid1_ItemCommand(object sender, HandleCommandEventArgs e) {
....................
My question is that if I remove the if (!IsPostBack) statement then the button in the ItemTemplate is firing the ItemCommand event. I need the IsPostback statement. Is there a way to solve this issue? Thanks!
Hi tangolp,
Your OnInit and Page_Load methods should look like this:
protected override void OnInit(EventArgs e){ TemplateDataField templateColumn1 = (TemplateDataField)this.WebDataGrid1.Columns["TemplateColumn1"]; templateColumn1.ItemTemplate = new myTemplate();}
protected void Page_Load(object sender, EventArgs e){ if (!IsPostBack) { WebDataGrid1.DataSource = populateGrid(); WebDataGrid1.DataBind(); }}
If you have any further questions, please let me know.
Hello tangolp,
Feel free to contact me if you need any further assistance with the matter.