Dynamic EditItemTemplate (events not firing)

sysdesigner

Newcomer
Joined
Feb 8, 2004
Messages
10
I am dynamically building EditItemTemplate columns for a datagrid.

Whenever I bind the datagrid during the PageLoad
the "edit" or "update" commands cause the appropriate event code to fire.
But when I change the code to only do the binding on "Not IsPostBack" then
the events don't fire any more. I need the event to fire without binding, otherwise the values will be lost.


Any help would be appreciated.

TIA ,
Shawn

PS. The following article shows the method that I am using to create the
template columns.

http://www.dotnetbips.com/displayarticle.aspx?id=84
 
Still not working

Thanks for your help. I added the code to add the Edit and Update command handlers, but they still will only fire if the Databind is called in the PageLoad on the PostBack. The code is below, whenever the Databind statement in the Pageload is replaced with the if statement (commented below), then the event stops firing.

~Shawn

Code starts here----------------------------------------------------


public class Datagrid : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;

DataSet ds=new DataSet();

private void Page_Load(object sender, System.EventArgs e)
{

string connstr2 = @"User ID=myUid;Initial Catalog=TEST;Data Source=Server";
SqlConnection cnn2=new SqlConnection(connstr2);
DataSet ds2=new DataSet();
SqlDataAdapter da2=new SqlDataAdapter("SELECT COL_HDNG_TXT FROM TRPT_COL_HDNG WHERE RPT_NM = 'test' ORDER BY COL_HDNG_NUM", cnn2);
da2.Fill(ds2, "TRPT_COL_HDNG");
string connstr = @"User ID=myUid; Initial Catalog=Northwind;Data Source=Server";
SqlConnection cnn=new SqlConnection(connstr);

SqlDataAdapter da=
new SqlDataAdapter("SELECT * FROM employees", cnn);
da.Fill(ds, "employees");
ITemplate itemTemp1= Page.LoadTemplate("ActionTemplate.ascx");
ITemplate editItemTemp1= Page.LoadTemplate("ActionEditTemplate.ascx");
TemplateColumn tc1=new TemplateColumn();
tc1.HeaderText = "Action";
tc1.ItemTemplate = itemTemp1;
tc1.EditItemTemplate=editItemTemp1;
this.DataGrid1.Columns.Add(tc1);
for (int columnNumber=0; columnNumber< 1; columnNumber++)
{
ITemplate itemTemp2= Page.LoadTemplate("Template" + columnNumber + ".ascx");
ITemplate editItemTemp2= Page.LoadTemplate("EditTemplate" + columnNumber + ".ascx");
TemplateColumn tc=new TemplateColumn();
tc.HeaderText = ds2.Tables["TRPT_COL_HDNG"].Rows[columnNumber]["COL_HDNG_TXT"].ToString();
tc.ItemTemplate = itemTemp2;
tc.EditItemTemplate = editItemTemp2;
this.DataGrid1.Columns.Add(tc);
}
this.DataGrid1.DataSource = ds;
this.DataGrid1.DataMember = "employees";
this.DataGrid1.EditCommand += new DataGridCommandEventHandler(this.DataGrid1_EditCommand);
this.DataGrid1.UpdateCommand += new DataGridCommandEventHandler(this.DataGrid1_UpdateCommand);


this.DataGrid1.DataBind();
//if (!Page.IsPostBack)
//{
// this.DataGrid1.DataBind();
//}

}














Robby said:
When you create all the controls at runtime you also need to recreate the event handlers on each postback.
 
Back
Top