如何在网格视图控件中获取标签的文本值

本文关键字:标签 获取 文本 控件 网格 视图 | 更新日期: 2023-09-27 18:05:58

我有几个标签在我的网格视图控件,我需要得到标签lbl_Activity_Code的文本值,当我从我的网格视图控件选择单选按钮。源代码

 <asp:GridView ID="gvActList" runat="server" Font-Size="Smaller" Width="100%" CssClass="tableStyle"
    EmptyDataText="No Data Found" AllowPaging="false"
    CellPadding="4" AutoGenerateColumns="False" >
    <FooterStyle Font-Bold="True" CssClass="tablefooter" />
    <RowStyle CssClass="tableRow1" />
    <SelectedRowStyle Font-Bold="True" CssClass="tableRowHighlight" />
    <HeaderStyle Font-Bold="True" CssClass="tableHeader" Font-Size="9px" />
    <Columns>
        <asp:TemplateField HeaderText="TM Activity Code" HeaderStyle-HorizontalAlign="Center">
            <ItemStyle Width="5%" HorizontalAlign="Left"></ItemStyle>
            <ItemTemplate>
                <asp:Label ID="lbl_Activity_Code" runat="server" Visible="true" Text='<%# Bind("ACTIVITY_CODE") %>'></asp:Label></ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Description" HeaderStyle-HorizontalAlign="Center">
            <ItemStyle Width="19%" HorizontalAlign="Left"></ItemStyle>
            <ItemTemplate>
                <asp:Label ID="lbl_ACTDESC" runat="server" Text='<%# Bind("ACTIVITY_DESC") %>' Visible="true"></asp:Label></ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="User Details" HeaderStyle-HorizontalAlign="Center">
            <ItemStyle Width="5%"></ItemStyle>
            <ItemTemplate>
                <asp:Label ID="lbl_User_Details" runat="server" Text='<%# Bind("CREATED_BY") %>' Visible="true"></asp:Label></ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Type" HeaderStyle-HorizontalAlign="Center">
            <ItemStyle Width="5%"></ItemStyle>
            <ItemTemplate>
                <asp:Label ID="lbl_PROMO_TYPE" runat="server" Text='<%# Bind("PROMO_TYPE") %>' Visible="true"></asp:Label></ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Category" HeaderStyle-HorizontalAlign="Center">
            <ItemStyle Width="5%"></ItemStyle>
            <ItemTemplate>
                <asp:Label ID="lblCatgDescription" runat="server" Text='<%# Bind("CATEGORY_CODE") %>'
                    Visible="true"></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Date Range" HeaderStyle-HorizontalAlign="Center">
            <ItemStyle Width="5%"></ItemStyle>
            <ItemTemplate>
                <asp:Label ID="lblACTIVEFROM" runat="server" Text='<%# Bind("ACTIVE_FROM") %>' Visible="true"></asp:Label>&#160; To &#160;
                <asp:Label ID="lblACTIVETO" runat="server" Text='<%# Bind("ACTIVE_TO") %>' Visible="true"></asp:Label></ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Activity Type" HeaderStyle-HorizontalAlign="Center">
            <ItemStyle Width="5%"></ItemStyle>
            <ItemTemplate>
                <asp:Label ID="lbl_Actvity_Type" runat="server" Text='<%# Bind("ACTIVITY_TYPE") %>' Visible="true"></asp:Label></ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Current Status" HeaderStyle-HorizontalAlign="Center">
            <ItemStyle Width="5%"></ItemStyle>
            <ItemTemplate>
                <asp:Label ID="lbl_Current_Status" runat="server" Text='<%# Bind("status") %>' Visible="true"></asp:Label></ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="View Details" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center">
            <ItemTemplate>
                <itemstyle width="5%" />
                <asp:LinkButton CssClass="l_link" Text="View" ID="lnkView" runat="server" ToolTip="View" /></ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderStyle-HorizontalAlign="Center">
            <HeaderTemplate>
                <asp:Label ID="lblCap" Text="Confirm" runat="server"></asp:Label></HeaderTemplate>
            <ItemStyle HorizontalAlign="Center"></ItemStyle>
            <ItemTemplate>
                <itemstyle width="1%" font-size="2px" horizontalalign="Center" />
                <table style="text-align: Center; vertical-align: text-top;">
                    <tr>
                        <td style="width: 98%;">
                            <asp:RadioButton ID="rdYes" runat="server" Text="Yes" GroupName="CH"></asp:RadioButton></td>
                    </tr>
                    <tr>
                        <td style="width: 98%;">
                            <asp:RadioButton ID="rdNo" runat="server" Text="No" GroupName="CH"></asp:RadioButton></td>
                    </tr>
                    <tr>
                        <td style="width: 98%;">
                            <asp:RadioButton ID="rdNone" runat="server" Text="None" GroupName="CH"></asp:RadioButton></td>
                    </tr>
                </table>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
    <EditRowStyle CssClass="tableRowHighlight" />
    <RowStyle CssClass="odd_tb" />
    <PagerStyle CssClass="foot_tbl" />
    <AlternatingRowStyle CssClass="even_tb" />
    <PagerSettings Mode="NextPreviousFirstLast" />
</asp:GridView>

.cs文件如下所示

protected void btnSubmit_Click(object sender, EventArgs e)
{
    try
    {
        if (gvActList.Rows.Count > 0)
        {
            foreach (GridViewRow row in gvActList.Rows)
            {
                RadioButton rdYes = (RadioButton)row.FindControl("rdYes");
                RadioButton rdNo = (RadioButton)row.FindControl("rdNo");
                RadioButton rdNone = (RadioButton)row.FindControl("rdNone");
                if (rdYes != null && rdNo != null && rdNone != null)
                {
                    if (rdYes.Checked == true)
                    {
                        v_Confirm = "YES";
                        v_Act_Code = gvActList.SelectedRow.Cells[0].Text;
                    }
                    else if (rdNo.Checked == true)
                    {
                        v_Confirm = "NO";
                    }
                }
            }
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

请帮帮我提前感谢

如何在网格视图控件中获取标签的文本值

在foreach循环中添加以下代码:

Label lbl_Activity_Code = (Label)row.FindControl("lbl_Activity_Code");
if(lbl_Activity_Code != null)
     string text = lbl_Activity_Code.Text;