S
sysdesigner
I need help with understanding why this happens.
In the code below the Edit and Update events will only fire if the Databind
is called in the PageLoad on the PostBack. Whenever the Databind statement
in the Pageload is replaced with the if statement (commented out below), then
the event stops firing. Why? Anyone know how to fix it?
TIA,
~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();
//}
}
In the code below the Edit and Update events will only fire if the Databind
is called in the PageLoad on the PostBack. Whenever the Databind statement
in the Pageload is replaced with the if statement (commented out below), then
the event stops firing. Why? Anyone know how to fix it?
TIA,
~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();
//}
}