Datagrid events only fire if Databind is called in Page Load event

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();
//}

}
 
T

Teemu Keiski

Does the Page and the controls have ViewState enabled? E.g you haven't
specifically disabled ViewState?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top