0

我已经使用了下面的代码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();

}
4

2 回答 2

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 事件。

于 2012-12-20T05:49:01.387 回答
0

我认为您需要在中断前将数据绑定代码移动到 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;

    }

}
于 2012-12-20T13:44:36.257 回答