0

每次单击更新链接按钮时,我都会连接一个 gridSearchResults_RowUpdating 事件。我想要做的是遍历事件 rowindex 处的行,并将行单元格中的文本框值添加到字符串集合中。我在访问单元格中的文本框时遇到了麻烦。如果这没有意义,我的代码应该有助于使事情更清晰。

HTML

        <asp:TemplateField HeaderText="Title" SortExpression="Title">
                <ItemTemplate>
                    <asp:Label ID="lblTitle" runat="server" Text='<%# Eval("jobTitle") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="txtTitle" runat="server" Text='<%# Bind("jobTitle") %>' ReadOnly="true"
                        BorderStyle="none" BorderWidth="0px" Width="80">
                    </asp:TextBox>
                </EditItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="First Name" SortExpression="FirstName">
                <ItemTemplate>
                    <asp:Label ID="lblFirstName" runat="server" Text='<%# Eval("FirstName") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="txtFirstName" runat="server" Text='<%# Bind("FirstName") %>' ReadOnly="true"
                        BorderStyle="none" BorderWidth="0px" Width="80">
                    </asp:TextBox>
                </EditItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="Last Name" SortExpression="LastName">
                <ItemTemplate>
                    <asp:Label ID="lblLastName" runat="server" Text='<%# Eval("LastName") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="txtLastName" runat="server" Text='<%# Bind("LastName") %>' ReadOnly="true"
                        BorderStyle="none" BorderWidth="0px" Width="80">
                    </asp:TextBox>
                </EditItemTemplate>
            </asp:TemplateField>

             <asp:TemplateField HeaderText="Email Address" SortExpression="EmailAddress">
                <ItemTemplate>
                    <asp:Label ID="lblEmailAddress" runat="server" Text='<%# Eval("EmailAddress") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="txtEmailAddress" runat="server" Text='<%# Bind("EmailAddress") %>' ReadOnly="true"
                        BorderStyle="none" BorderWidth="0px" Width="80">
                    </asp:TextBox>
                </EditItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="Telephone Number" SortExpression="TelephoneNumber">
                <ItemTemplate>
                    <asp:Label ID="lblTelephoneNumber" runat="server" Text='<%# Eval("TelephoneNumber") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="txtTelephoneNumber" runat="server" Text='<%# Bind("TelephoneNumber") %>' ReadOnly="true"
                        BorderStyle="none" BorderWidth="0px" Width="80">
                    </asp:TextBox>
                </EditItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="Company" SortExpression="Company">
                <ItemTemplate>
                    <asp:Label ID="lblCompany" runat="server" Text='<%# Eval("Company") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="txtCompany" runat="server" Text='<%# Bind("Company") %>' ReadOnly="true"
                        BorderStyle="none" BorderWidth="0px" Width="80">
                    </asp:TextBox>
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:LinkButton ID="lbEditLink" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit"></asp:LinkButton>
                </ItemTemplate>
                <EditItemTemplate>
                <asp:LinkButton ID="lbCancelLink" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
                <asp:LinkButton ID="lbUpdateLink" runat="server" CausesValidation="False" CommandName="Update" Text="Update"></asp:LinkButton>
                </EditItemTemplate>

            </asp:TemplateField>

        </Columns>
    </asp:GridView>

C#

protected void gridSearchResults_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
        GridViewRow row = gridSearchResults.Rows[e.RowIndex];

        foreach (gridviewrow c in gridSearchResults.Rows[e.RowIndex])
        {

        }
}

那么有没有人知道循环遍历gridview行的单元格并获取gridview行单元格中的文本框值的好方法。谢谢

4

1 回答 1

1

尝试这个:

foreach (TableCell cell in gv.Rows(e.RowIndex).Cells) 
{
    TextBox txt1 = (TextBox)cell.FindControl("txtControlName");
    // or if you don't want to hard code the control name and assuming that there is only 1 control in the cell then use this:
    TextBox txt2 = (TextBox)cell.Controls(0);
}
于 2012-09-21T15:00:21.653 回答