编辑绑定网格视图错误

时间:2016-08-24 16:44:54

标签: c# asp.net gridview

我正在Gridview上实现一个绑定到“books”表的“编辑”功能。我收到RowEditingEvent的错误没有处理。我不明白我哪里出错了,或者我在某个地方错过了一个引用,但在我看来,一切都在处理。

add_months(date_A, x * -1)

}

public partial class AddEdit_Text_Books : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            DataTable dt = new DataTable();
            string query = "select  * from textBooks   ";
            using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["HUTDMSConnectionString"].ToString()))
            using (SqlDataAdapter adapter = new SqlDataAdapter(query, connection))


                adapter.Fill(dt);
            ViewState["allBooks"] = dt;
            this.BindGrid();
        }
    }

    protected void BindGrid()
    {
        GridView1.DataSource = ViewState["allBooks"] as DataTable;
        GridView1.DataBind();
    }

    protected void OnRowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        this.BindGrid();
    }

    protected void OnUpdate(object sender, EventArgs e)
    {
        GridViewRow row = (sender as LinkButton).NamingContainer as GridViewRow;
        string name = (row.Cells[0].Controls[0] as TextBox).Text;
        string country = (row.Cells[1].Controls[0] as TextBox).Text;
        DataTable dt = ViewState["allBooks"] as DataTable;

        ViewState["allBooks"] = dt;
        GridView1.EditIndex = -1;
        this.BindGrid();
    }

    protected void OnCancel(object sender, EventArgs e)
    {
        GridView1.EditIndex = -1;
        this.BindGrid();
    }
}

Stack Trace Screen Shot

1 个答案:

答案 0 :(得分:0)

您尚未在gridview定义中注册onediting事件。 在gridview定义中添加OnRowEditing ="OnRowEditing"

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
AutoGenerateColumns="False" DataKeyNames="BookID"  Width="1482px" 
CellPadding="4" ForeColor="#333333" GridLines="None" OnRowEditing ="OnRowEditing">