网格视图行更新不会获得新输入的值

时间:2012-12-20 04:06:47

标签: asp.net linq

我在GridViewRowUpdating下使用了以下代码,但点击更新按钮后,它会使用旧值。 我按断点检查GridViewRowIpdating,所有文本框都有旧值。并且新输入的值不会被抓住。

我在谷歌尝试了很多,但我无法解决问题。

protected void GridViewDocuments_Search_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
        _DataContext = new EDMSDataContext();

        int _Docid = (int)GridViewDocuments_Search.DataKeys[e.RowIndex].Value;

        TextBox DocumentNo = (TextBox)GridViewDocuments_Search.Rows[e.RowIndex].FindControl("DocumentNo");

        TextBox title = (TextBox)GridViewDocuments_Search.Rows[e.RowIndex].FindControl("title");
        TextBox unit = (TextBox)GridViewDocuments_Search.Rows[e.RowIndex].FindControl("unit");

        TextBox originator = (TextBox)GridViewDocuments_Search.Rows[e.RowIndex].FindControl("originator");

        _DataContext.updateDoc(_Docid, DocumentNo.Text, title.Text, unit.Text, originator.Text);
        _DataContext.SubmitChanges();
        GridViewDocuments_Search.EditIndex = -1;


        var query = _DataContext.spQuickSearchDoc(txtSearchKeywords.Text);

         GridViewDocuments_Search.DataSource = query;
        GridViewDocuments_Search.DataBind();
    }

protected void GridViewDocuments_Search_RowEditing(object sender, GridViewEditEventArgs e)
{
    //var query = _DataContext.spQuickSearchDoc(txtSearchKeywords.Text);
   // GridViewDocuments_Search.DataSource = query;
    GridViewDocuments_Search.EditIndex = e.NewEditIndex;
    GridViewDocuments_Search.DataBind();
}

protected void btnSearch_Click(object sender, EventArgs e)
{

        _DataContext = new EDMSDataContext();
        var query = _DataContext.spQuickSearchDoc(txtSearchKeywords.Text);
        GridViewDocuments.Visible = false;
        GridViewDocuments_Search.Visible = true;
        GridViewDocuments_Search.DataSource = query;
        GridViewDocuments_Search.DataBind();


}

网格视图标记:

<asp:GridView ID="GridViewDocuments_Search" runat="server" AutoGenerateColumns=False 
          Visible="False" onrowcommand="GridViewDocuments_Search_RowCommand" OnRowDeleting="GridViewDocuments_Search_RowDeleting" 
  DataKeyNames="DocID" PageSize="100" 
          onrowcancelingedit="GridViewDocuments_Search_RowCancelingEdit" 
          onrowediting="GridViewDocuments_Search_RowEditing" 
          onrowupdating="GridViewDocuments_Search_RowUpdating"  >
    <Columns>
    <asp:TemplateField HeaderText = "Details">
           <ItemTemplate>
      <asp:Button ID ="btn_Show" Text="Details" runat= "server" CommandName= "Details" CommandArgument='<%#
        Container.DataItemIndex%>' />
        </ItemTemplate>
            </asp:TemplateField>


        <asp:TemplateField HeaderText="DocNo">
            <EditItemTemplate>
                <asp:TextBox ID="DocumentNo" runat="server" Text='<%# Eval("DocumentNo") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Eval("DocumentNo") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Title" SortExpression="title">
            <EditItemTemplate>
                <asp:TextBox ID="title" runat="server" Text='<%# Eval("title") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label2" runat="server" Text='<%# Eval("title") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>





        <asp:TemplateField HeaderText="DocID">
            <EditItemTemplate>
                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("DocID") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label3" runat="server" Text='<%# Eval("DocID") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Originator">
            <EditItemTemplate>
                <asp:TextBox ID="Originator" runat="server" Text='<%# Eval("Originator") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label4" runat="server" Text='<%# Eval("Originator") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Unit">
            <EditItemTemplate>
                <asp:TextBox ID="Unit" runat="server" Text='<%# Eval("Unit") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label5" runat="server" Text='<%# Eval("Unit") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>





        <asp:CommandField ShowEditButton="True" />
        <asp:CommandField ShowDeleteButton="True" />
    </Columns>
</asp:GridView>

GridViewDocuments_Search_RowCommand

protected void GridViewDocuments_Search_RowCommand(object sender, GridViewCommandEventArgs e)
{
    int rowindex = Convert.ToInt32(e.CommandArgument.ToString());
    _DataContext = new EDMSDataContext();

    int _Docid = (int)GridViewDocuments_Search.DataKeys[rowindex].Value;

    switch (e.CommandName)
    {
        case "Details":
            Response.Redirect("~/Documentfortest.aspx?DocID=" + _Docid);
            break;
        case "Delete":
            _DataContext.DeleteDoc(_Docid);
            _DataContext.SubmitChanges();

            break;

    }

    var query = _DataContext.spQuickSearchDoc(txtSearchKeywords.Text);

    GridViewDocuments_Search.DataSource = query;
    GridViewDocuments_Search.DataBind();
    //UpdatePanel1.Update();

}

2 个答案:

答案 0 :(得分:0)

RowUpdating事件在网格中更新数据之前触发。这允许您在更新数据之前对数据进行任何更改。

您可以在集合NewValues中使用新输入的值。

这就是你如何使用它。

protected void GridViewDocuments_Search_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
        _DataContext = new EDMSDataContext();

        int _Docid = (int)GridViewDocuments_Search.DataKeys[e.RowIndex].Value;

        _DataContext.updateDoc(_Docid, 
                        e.NewValues["DocumentNo"].ToString(), 
                        e.NewValues["title"].ToString(),
                        e.NewValues["unit"].ToString(),
                        e.NewValues["originator"].ToString());
        _DataContext.SubmitChanges();
        GridViewDocuments_Search.EditIndex = -1;

        var query = _DataContext.spQuickSearchDoc(txtSearchKeywords.Text);

         GridViewDocuments_Search.DataSource = query;
        GridViewDocuments_Search.DataBind();
}

注意:更新行后还会触发RowUpdated事件。

答案 1 :(得分:0)

我认为您需要在break之前的case“Delete”下的rowcommand中移动数据绑定代码;对于每个被触发的命令(编辑/更新),您将在rowcommand中重新绑定gridview。

例如:

protected void GridViewDocuments_Search_RowCommand
(object sender, GridViewCommandEventArgs e)
{
    int rowindex = Convert.ToInt32(e.CommandArgument.ToString());
    _DataContext = new EDMSDataContext();

    int _Docid = (int)GridViewDocuments_Search.DataKeys[rowindex].Value;

    switch (e.CommandName)
    {
        case "Details":
            Response.Redirect("~/Documentfortest.aspx?DocID=" + _Docid);
            break;
        case "Delete":
            _DataContext.DeleteDoc(_Docid);
            _DataContext.SubmitChanges();
             //rebind only if row is deleted
             var query = _DataContext.spQuickSearchDoc(txtSearchKeywords.Text);

             GridViewDocuments_Search.DataSource = query;
             GridViewDocuments_Search.DataBind();
             break;

    }

}