Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
1253
ItemTemplate help
posted

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!

Parents
  • 37874
    posted

    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.

Reply Children
No Data