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>