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

Discussion in 'ASP .Net Datagrid Control' started by sysdesigner, Jun 28, 2005.

  1. sysdesigner

    sysdesigner Guest

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

    }
     
    sysdesigner, Jun 28, 2005
    #1
    1. Advertising

  2. sysdesigner

    Teemu Keiski Guest

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

    --
    Teemu Keiski
    ASP.NET MVP, AspInsider
    Finland, EU
    http://blogs.aspadvice.com/joteke
     
    Teemu Keiski, Jul 2, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. S_K
    Replies:
    6
    Views:
    1,255
    Robert Dunlop
    Nov 8, 2007
  2. wuz
    Replies:
    0
    Views:
    687
  3. KJ
    Replies:
    1
    Views:
    163
    Teemu Keiski
    Aug 16, 2003
  4. Michael
    Replies:
    0
    Views:
    247
    Michael
    Dec 29, 2003
  5. Andy Fish

    load event doesn't fire unless init event is present

    Andy Fish, Jun 28, 2005, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    286
    Jonathan Mast
    Jul 7, 2005
Loading...

Share This Page