在asp.net中将数据更新到gridview - c#

我是asp.net的新手,如果我的查询很幼稚,请多多包涵
我的网格设计:

<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>

我的代码后面:

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 ;
}

我能够删除但不能编辑数据。我无法获取在网格视图中输入的更改值。尽我所能,在编辑或更改之前让我获得了网格视图中的值。
在此先感谢朋友。

参考方案

我认为我们无法使用Find控制方法访问绑定字段。 author_name是绑定区域内的数据字段的名称,您无法使用FindControl对其进行访问,它不是控件。

使用e.RowIndex获取更新的值。

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
}

更新的答案:

问题:
The problem is that you are binding your gridview on each post back

解:

为了测试它,我创建了一个添加两列的gridview,如果在每个回发上绑定gridview,我都会得到旧值。为了避免这种情况,只需在绑定网格之前添加Page.IsPostBack检查。

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
    }
}

我的完整代码:

// 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的另一个下拉框中进行选择后,如何显示隐藏的下拉框? - c#

我在执行此操作时遇到了很多问题,因此我在页面上有2个下拉菜单,而第一个我想先隐藏。在第二个下拉列表(任何选择)中做出选择之后,我希望第三个下拉列表及其标签变为可见。它们都连接到数据库。我已经以一种简单的方式重新创建了代码的这一方面,以提供视觉效果。 我已在网上搜索以寻求帮助。我是.NET的新手,并且从未使用过jquery或ajax,如果可能的话,我只想在C#…

将Web用户控件添加到页面时,asp按钮onclick不会触发 - javascript

我正在使用使用Visual Studio模板创建的Web表单应用程序。模板具有一个内容占位符,该占位符被访问的任何页面的内容替换。有问题的页面有几个服务器控件,例如文本框,标签和按钮。当我单击我的更新按钮时,它可以正常工作,这些值会回传并更新数据库。我想在所有子页面上创建通用的登录提示。我的导航栏(位于我的母版页中)具有引导程序设置。我在导航栏中添加了一个下…

Asp.net发送信息表单到页面 - c#

我正在尝试使用弹出窗口中的新信息更新旧页面。到目前为止,我尝试过将结果保存在会话中Session["Data"] = DLvrijecampingplaatsen.SelectedItem; 然后当它达到Page_Load时,将其重新加载回旧页面if (Session["Data"] != null) { LBkies…

与Mootools Scriptmanager Ajax Asp.net发生冲突? - c#

我正在尝试不同的方法来使这项工作成功,但是没有成功。我正在尝试将mootools与asp.net应用程序集成。我只想用它为我的网站添加一些效果。我也使用更新面板,scriptmanager,因为不希望有完整的回发。在控制台上引发错误TypeError:clientID.startsWith不是函数,并且在进行更新时brwoser reset...。这是整个代…

Div单击与单选按钮相同吗? - php

有没有一种方法可以使div上的click事件与表单环境中的单选按钮相同?我只希望下面的div提交值,单选按钮很丑代码输出如下:<input id="radio-2011-06-08" value="2011-06-08" type="radio" name="radio_date&#…