从代码隐藏更新GridView行

时间:2012-07-27 19:41:28

标签: c# asp.net datagrid code-behind

当用户更改编辑模板中文本框中的文本并单击更新时,当我尝试获取这些新值时,它仍然会重新显示文本框的旧值。

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataKeyNames="CompanyID" CellPadding="4"  
        GridLines="None" Width="1079px" ForeColor="#333333" 
        OnRowCancelingEdit="GridView1_RowCancelling"
        OnRowUpdating="GridView1_RowUpdating"
        OnRowEditing="GridView1_RowEditing">


        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        <Columns>
            <asp:TemplateField ShowHeader="False">

                <EditItemTemplate>
                    <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" 
                        CommandName="Update" CommandArgument='<%# Eval("CompanyID") %>' Text="Update"></asp:LinkButton>
                    &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" 
                        CommandName="Cancel" Text="Cancel"></asp:LinkButton>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" 
                        CommandName="Edit" Text="Edit" ></asp:LinkButton>
                </ItemTemplate>


            </asp:TemplateField>



            <asp:TemplateField HeaderText="Issue Date">

                <ItemTemplate>
                    <asp:Label runat="server" ID="IssueDate" Text='<%#Eval("IssueDate") %>' />                    
                </ItemTemplate>

                <EditItemTemplate>
                    <asp:TextBox runat="server" ID="txtIssueDate" Text='<%#Eval("IssueDate") %>' />
                </EditItemTemplate>

            </asp:TemplateField>


            <asp:TemplateField HeaderText="Notice Intent Response Due">

                <ItemTemplate>
                    <asp:Label runat="server" ID="NoticeIntentResponseDue" Text='<%#Eval("NoticeIntentResponseDue") %>' />                    
                </ItemTemplate>

                <EditItemTemplate>
                    <asp:TextBox runat="server" ID="txtNoticeIntentResponseDue" Text='<%#Eval("NoticeIntentResponseDue") %>' />
                </EditItemTemplate>

            </asp:TemplateField>


            <asp:TemplateField HeaderText="Deadline For Questions">

                <ItemTemplate>
                    <asp:Label runat="server" ID="DeadlineForQuestions" Text='<%#Eval("DeadlineForQuestions") %>' />                    
                </ItemTemplate>

                <EditItemTemplate>
                    <asp:TextBox runat="server" ID="txtDeadlineForQuestions" Text='<%#Eval("DeadlineForQuestions") %>' />
                </EditItemTemplate>

            </asp:TemplateField>


            <asp:TemplateField HeaderText="Bids Due">

                <ItemTemplate>
                    <asp:Label runat="server" ID="BidsDue" Text='<%#Eval("BidsDue") %>' />                    
                </ItemTemplate>

                <EditItemTemplate>
                    <asp:TextBox runat="server" ID="txtBidsDue" Text='<%#Eval("BidsDue") %>' />
                </EditItemTemplate>

            </asp:TemplateField>


            <asp:TemplateField HeaderText="Shortlist Notice">

                <ItemTemplate>
                    <asp:Label runat="server" ID="ShortlistNotice" Text='<%#Eval("ShortlistNotice") %>' />                    
                </ItemTemplate>

                <EditItemTemplate>
                    <asp:TextBox runat="server" ID="txtShortlistNotice" Text='<%#Eval("ShortlistNotice") %>' />
                </EditItemTemplate>

            </asp:TemplateField>


            <asp:TemplateField HeaderText="Final Selection">

                <ItemTemplate>
                    <asp:Label runat="server" ID="FinalSelection" Text='<%#Eval("FinalSelection") %>' />                    
                </ItemTemplate>

                <EditItemTemplate>
                    <asp:TextBox runat="server" ID="txtFinalSelection" Text='<%#Eval("FinalSelection") %>' />
                </EditItemTemplate>

            </asp:TemplateField>





            <asp:TemplateField Visible="false" HeaderText="CompanyID">

                <ItemTemplate>
                    <asp:Label runat="server" ID="CompanyID" Text='<%#Eval("CompanyID") %>' />
                </ItemTemplate>

            </asp:TemplateField>




        </Columns>



    </asp:GridView>

更新按钮调用此功能:

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)

    {

        int key = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);

        Label CompanyID = (Label)GridView1.Rows[e.RowIndex].FindControl("txtCompanyID");

        TextBox thisIssueDate = (TextBox)(GridView1.Rows[e.RowIndex].FindControl("txtIssueDate"));

        TextBox NoticeIntentResponseDue = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtNoticeIntentResponseDue");

        Response.Write(NoticeIntentResponseDue.Text + " " + thisIssueDate.Text);
        Response.End();

        TextBox DeadlineForQuestions = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtDeadlineForQuestions");

        TextBox BidsDue = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtBidsDue");

        TextBox ShortlistNotice = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtShortlistNotice");

        TextBox FinalSelection = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtFinalSelection");
    }  

响应告诉我,抓取的值仍然是该框的原始文本值。不是你输入的内容。

2 个答案:

答案 0 :(得分:0)

在网格视图行更新事件中添加以下条件

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
 if (e.Row.RowState == DataControlRowState.Edit )
 {


    int key = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);

    Label CompanyID = (Label)GridView1.Rows[e.RowIndex].FindControl("txtCompanyID");

    TextBox thisIssueDate = (TextBox)(GridView1.Rows[e.RowIndex].FindControl("txtIssueDate"));

    TextBox NoticeIntentResponseDue = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtNoticeIntentResponseDue");

    Response.Write(NoticeIntentResponseDue.Text + " " + thisIssueDate.Text);
    Response.End();

    TextBox DeadlineForQuestions = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtDeadlineForQuestions");

    TextBox BidsDue = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtBidsDue");

    TextBox ShortlistNotice = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtShortlistNotice");

    TextBox FinalSelection = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtFinalSelection");
    }  
  }

更新: 问题看起来你已经将Edit Item模板列与数据表中的数据绑定,当你在后面的代码中获取数据时,你没有得到用户在编辑模式下更新的更新数据,你仍然得到旧数据。 If you remove the Binding from the Edit Item Template feilds then your code will work.

答案 1 :(得分:0)

我明白了,德里克是对的。它与页面加载中回发时的绑定数据有关。我每次都将数据绑定,而不仅仅是第一次。感谢