更新数据到asp.net的GridView [英] updating data into gridview in asp.net
问题描述
我是比较新的asp.net.So请容忍我,如果我的查询是幼稚
我的网格设计:
I am relatively new to asp.net.So please bear with me if my query is childish my grid design:
<asp:GridView runat ="server" GridLines = "Both" DataKeyNames="book_id"AutoGenerateColumns ="false" CellPadding ="5" CellSpacing ="5" allowpaging="True" allowsorting="True"
ID="gv_table1" EmptyDataText ="No data exists" OnRowEditing="gv_RowEditing"
OnRowCancelingEdit="gv_RowCancelingEdit" OnRowUpdating="gv_RowUpdating" OnRowDeleting="gv_RowDeleting">
<Columns>
<asp:BoundField HeaderText="Book ID" DataField="book_id">
</asp:BoundField>
<asp:BoundField DataField="book_name" HeaderText="Book Name">
</asp:BoundField>
<asp:BoundField DataField="author_name" HeaderText="Author Name">
</asp:BoundField>
<asp:BoundField DataField="publisher" HeaderText="Publisher">
</asp:BoundField>
<asp:BoundField DataField="year_edition" HeaderText="Year/Edition">
</asp:BoundField>
<asp:BoundField DataField="total_no" HeaderText="Total No">
</asp:BoundField>
<asp:BoundField DataField="available" HeaderText="Available">
</asp:BoundField>
<asp:BoundField DataField="tags" HeaderText="Tags">
</asp:BoundField>
<asp:BoundField DataField="fare" HeaderText="Fare">
</asp:BoundField>
<asp:BoundField DataField="state" HeaderText="State">
</asp:BoundField>
<asp:templatefield HeaderText ="Options">
<itemtemplate >
<asp:linkbutton id="btnEdit" runat="server" commandname="Edit" text="Edit" />
<asp:linkbutton id="btnDelete" runat="server" commandname="Delete" text="Delete" />
</itemtemplate>
<edititemtemplate>
<asp:linkbutton id="btnUpdate" runat="server" commandname="Update" text="Update" />
<asp:linkbutton id="btnCancel" runat="server" commandname="Cancel" text="Cancel" />
</edititemtemplate>
</asp:templatefield>
</Columns>
</asp:GridView>
我的code背后:
my code behind:
public void gv_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int bookid = Convert.ToInt32(gv_table1.DataKeys[e.RowIndex].Values["book_id"].ToString());
string bookname =(gv_table1.Rows[e.NewValues].Cells[1].Controls[0] as TextBox).Text;
string bookname = (gv_table1.Rows[e.RowIndex].FindControl("author_name") as TextBox).Text;
string book = gv_table1.Rows[e.RowIndex].Cells[1].Text ;
}
我能删除但不能编辑data.I我不能够得到更改后的值,我在网格视图。我进最大的努力让我那是在gridview的编辑或更改前的值。
在此先感谢朋友们。
I am able to delete but not edit data.I am not able to get the changed values that i enter in the grid view .My best effort got me the value that was in gridview before editing or changing. Thanks in advance friends.
推荐答案
我不认为我们可以利用查找控制方法访问绑定字段。 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();
}
这篇关于更新数据到asp.net的GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!