更新时间:2022-05-17 03:07:23
我不认为我们可以利用查找控制方法访问绑定字段。 AUTHOR_NAME
是数据字段的绑定使用费尔德你的FindControl不能访问它里面的名字,它不是控制。
I don't think we can access bound field using Find control method. author_name
is the name of datafield inside the bound feild you can't access it using FindControl, it is not a control.
使用的e.RowIndex来获取更新的值。
Use the e.RowIndex to get the updated values.
public void gv_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string bookname = ((TextBox)(gv_table1.Rows[e.RowIndex].Cells[1].Controls[0])).Text;
// OR
string bookname =(gv_table1.Rows[e.RowIndex].Cells[1].Controls[0] as TextBox).Text;
gv_table1.EditIndex = -1; // reset the edit index
// Again Bind the gridview to show updated data
}
更新答案:
问题:的问题是,你在每个岗位绑定你的GridView回
解决方案:
要测试它,我创建了一个GridView添加两个栏,我得到了旧的价值观念,如果我绑定在每次回发的GridView。为了避免它只是在添加 Page.IsPostBack
检查之前绑定的网格。
To test it,I create a gridview add two column, and I got old values if I bind gridview on each postback. To avoid from it just add the Page.IsPostBack
check in your before binding the grid.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack) // If you not place this check then you will get the old values because GridView in Bind on every postback
{
BindGrid(); // Bind you grid here
}
}
我的完整code:
// Aspx Code
<asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server"
AllowPaging="True"
onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating" onrowcancelingedit="GridView1_RowCancelingEdit"
>
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="City" HeaderText="Name" />
<asp:CommandField ShowEditButton ="true" ShowCancelButton="true" ShowDeleteButton="true" />
</Columns>
</asp:GridView>
// Aspx Code Behind
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack) // If you not place this check then you will get the old values because GridView in Bind on every postback
{
BindGrid();
}
}
private void BindGrid() // function for binding gridview
{
DataTable dt = new DataTable();
dt.Columns.Add("Name");
dt.Columns.Add("City");
DataRow r = dt.NewRow();
r[0] = "Name 1";
r[1] = "City 1";
DataRow r1 = dt.NewRow();
r1[0] = "Name 2";
r1[1] = "City 2";
dt.Rows.Add(r);
dt.Rows.Add(r1);
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex; // setting new index
BindGrid();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = GridView1.Rows[e.RowIndex];
string newvalue = ((TextBox)row.Cells[0].Controls[0]).Text;
GridView1.EditIndex = -1; // Again reset
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1; // reseting grid view
BindGrid();
}