0

This is the same question I asked earlier, but I figured out a better way (More concise and to the point) of asking. I have a gridview that i have a button dynamically created and added to the footer in the row_databound method. I have to have Gridview1.databind() in the page_load method for it to work and insert a record to the db. However when I have the databind in the page_load the edit button doesn't actually edit the row in the db. It brings up the editable boxes when hitting edit but does not update it when hitting update. Both the delete button and the edit button are using the gridview and datasources built in parameters and like I said, the delete button still works fine whether or not I have the gridview.databind in the page_load method. Why? below is code.

protected void Page_Load(object sender, EventArgs e)
{


    if (this.IsPostBack == false)
    {
       loadlist();
    }

    GridView1.DataBind();
}


protected void GridView1_RowDataBount(object sender, GridViewRowEventArgs e)
    {




        if (e.Row.RowType == DataControlRowType.Footer)
        {
            textboxes.Clear();
            for (int i = 0; i < e.Row.Cells.Count; i++)
            {
                if (i == 0)
                {
                    buttons.Clear();
                    String buttontitle = "Add";
                    //var tempbutton = new Button();
                    string tempString = i.ToString();
                    //tempbutton.ID = tempString;
                    addbutton.ID = tempString;
                    //tempbutton.Text = buttontitle;
                    addbutton.Text = buttontitle;
                    //tempbutton.Click += tempbutton_Click;
                    addbutton.Click += tempbutton_Click;
                    addbutton.UseSubmitBehavior = false;
                    buttons.Add(i, addbutton);
                    e.Row.Cells[i].Controls.Add(addbutton);

                }


                if (i > 0)
                {

                    var tempbox = new TextBox();
                    string tempString = i.ToString();
                    tempbox.ID = tempString;
                    textboxes.Add(i, tempbox);
                    e.Row.Cells[i].Controls.Add(tempbox);


                }

            }

<asp:GridView ID="GridView1" runat="server" AutoGenerateEditButton="True"
     AutoGenerateDeleteButton="True" ShowFooter="true"
     OnRowDataBound="GridView1_RowDataBount" 
     style="position:absolute; left:5%; width:90%; margin:0; top: 140px; height: 353px;" 
     AllowSorting="True" AutoGenerateColumns="true" AllowPaging="True"  >

    <AlternatingRowStyle BackColor="Aqua" />       
</asp:GridView>
Cœur
  • 37,241
  • 25
  • 195
  • 267
user519670
  • 685
  • 3
  • 15
  • 23

2 Answers2

0

Put the GridView1.DataBind() inside the if (this.IsPostBack == false) section. Every time you click an action (edit, delete, insert), it causes a postback and calls the databind again. It should only be called when the datasource gets updated.

Camilo
  • 345
  • 1
  • 6
  • Ok, so i tried that. Same issue, when i hit my dynamically created add button it disapears as well as my text boxes but then nothing happens as far as an insert. Also, my onclick method for the button never gets called. – user519670 Jun 09 '15 at 15:39
  • Post the code where you dynamically add the button and where you add the onclick event to the button handler. – Camilo Jun 09 '15 at 15:44
  • Posted that method. it is inside the row_databound method – user519670 Jun 09 '15 at 15:48
  • Instead of adding your dynamic button and textbox in the RowDataBound event, move that code to the RowCreated event. Dynamic controls need to be recreated after every postback, and as long as they have the same Ids, they will retain the value they had before. Reference this post: http://stackoverflow.com/questions/7658049/dynamic-columns-dissapears-after-postback – Camilo Jun 09 '15 at 16:27
0

You need to bind the gridview in " ispostback == false" condition

user3077222
  • 99
  • 11
  • Ok, so i tried that. Same issue, when i hit my dynamically created add button it disapears as well as my text boxes but then nothing happens as far as an insert. Also, my onclick method for the button never gets called. – user519670 Jun 09 '15 at 15:56