gridview.row [e.rowindex]的foreach循环

时间:2012-09-21 09:55:53

标签: c# asp.net loops gridview

每次点击更新链接按钮时,我都会连接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行单元格中的文本框值的好方法。感谢

1 个答案:

答案 0 :(得分: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);
}