Why does GridView Rowupdating event not capture the new values of textbox?
I am using a gridview. When I click on the edit button the update and cancel button appear. Upon modifying the values in textbox which come from EditItemTemplate , the new values dont show in the event handler rowupdating(), instead I get the values which appear when the page was rendered. How do I grab the new values from these textboxes and proceed further? Here is the code.
<asp:GridView ID="GridView1" runat="server" AutoGenerateEditButton="true" AutoGenerateColumns="false"
AutoGenerateDeleteButton="true" onrowediting="GridView1_RowEditing"
onrowupdating="GridView1_RowUpdating">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblId" runat="server" Text='<%# Eval("id") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtId" runat="server" Text='<%# Eval("id") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("cpuname") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtName" runat="server" Text='<%# Eval("cpuname") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblStatus" runat="server" Text='<%# Eval("status") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtStatus" runat="server" Text='&l开发者_StackOverflowt;%# Eval("status") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
TextBox text = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtName");
}
You should use the two way binding here. I.e. Bind instead of Eval:
<asp:TextBox ID="txtId" runat="server" Text='<%# Bind("id") %>'></asp:TextBox>
Here is the link to documentation:
Data-Binding Expressions Overview
Here is my code behind:
protected void Page_Load(object sender, EventArgs e)
{
DataTable table = new DataTable();
table.Columns.Add("Data");
for(int i = 0; i < 20; i++)
table.Rows.Add(new object[] { i });
GridView1.DataSource = table;
if(!IsPostBack) // <<<<<<<<<<<<
GridView1.DataBind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
GridView1.DataBind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
TextBox text = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtId");
}
If you have written the code to bind the grid in page load ensure that it is enclosed in if(!IsPostBack)
I hope it does help
Try this
TextBox text = (TextBox)GridView1.Rows[e.EditIndex].FindControl("txtName");
This ended up working for me.
protected void MyGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string value = e.NewValues[0].ToString();
// ...
}
i'm follow platon and it's work!
Put if(!IsPostBack)
before databind()
,
and insert GridView1.DataBind();
after GridView1.EditIndex = e.NewEditIndex;
in GridView1_RowEditing
精彩评论