locked
ITemplate disappear when I click on a button RRS feed

  • Question

  • User-740209433 posted

    Hi,

    I have a gridview initiated in the aspx page without any column

      <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
                            OnRowCommand="GridView1_RowCommand" OnRowDataBound="GridView1_RowDataBound">
    </asp:GridView>

    I dynamically create the column on the fly from a DB. There are regular text columns and I also wanted to have a column with dropdown and an Apply button which I do have my own code.

    When I click on the button, the dropdown boxes disappears so does the "Apply" button.  If I don't add "if (!IsPostBack) in Page_load, it works perfectly. What should I do in order to get this right? Please advice.

    protected void Page_Load(object sender, EventArgs e)
            {          
                if (!IsPostBack)
                {            
                    gvbind();
                }
            }


     protected void gvbind()
            {
                try
                {
                    conn.Open();
     SqlCommand cmd = new SqlCommand("Select * from tbl", conn);
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    DataSet ds = new DataSet();
    
                    da.Fill(ds);
                    conn.Close();
    
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        GridView1.Columns.Clear();
                        DataTable dt = new DataTable();
                        dt = ds.Tables[0];
                        for (int i = 0; i < dt.Columns.Count; i++)
                        {
                            if (dt.Columns[i].ColumnName.ToString() != "SpecialColumn")
                            {
                                BoundField boundField = new BoundField();
                                boundField.ReadOnly = true;
                                boundField.DataField = dt.Columns[i].ColumnName.ToString();
                                boundField.HeaderText = dt.Columns[i].ColumnName.ToString();
                                GridView1.Columns.Add(boundField);
                            }
                            else
                            {
                                TemplateField ddlTmpField = new TemplateField();                           
                                ddlTmpField.HeaderText = "SpecialColumn";
                                ddlTmpField.ItemTemplate = new DropdownColumn();
                                GridView1.Columns.Add(ddlTmpField);
                            }
    
                        }
                        TemplateField btnTmpField = new TemplateField();
                        btnTmpField.ItemTemplate = new ButtonColumn();
                        GridView1.Columns.Add(btnTmpField);
              GridView1.DataSource = ds;
                        GridView1.DataBind();
                        GridView1.Width = 1000;
    }
    
    
    //Dropdown and Button itemTemplate
    public class DropdownColumn : ITemplate { public void InstantiateIn(System.Web.UI.Control container) { DropDownList dl = new DropDownList(); dl.ID = "ddl"; dl.DataTextField = "SpecialColumn"; dl.DataValueField = "SpecialColumn"; container.Controls.Add(dl); } } public class ButtonColumn : ITemplate { public void InstantiateIn(System.Web.UI.Control container) { Button btn = new Button(); btn.ID = "btnApply"; btn.Text = "Apply"; container.Controls.Add(btn); } }



    Monday, June 5, 2017 1:29 PM

All replies

  • User991499041 posted

    Hi wkpli,

    When I click on the button, the dropdown boxes disappears so does the "Apply" button.  If I don't add "if (!IsPostBack) in Page_load, it works perfectly. What should I do in order to get this right? Please advice.

    It's a lifecycle issue as you inferred in your question.

    Dynamically created controls need to be be created on every load of the page (usually the Page Load or Page Init event depending on your needs).

    When using dynamic controls, you must remember that they will exist only until the next postback.

    ASP.NET will not re-create a dynamically added control.

    So, Remove the Condition: !IsPostback, So that each time the page Loads, The control is also created.

    Regards,

    zxj

    Tuesday, June 6, 2017 3:04 AM