简体   繁体   中英

Need to step through statically created radiobuttonlist for values

I have inherited some Asp.net code with the following table. I need to step through and find any radiobuttons that have been selected as 'Bad' and then find the associated columntext. Here is the aspx code:

<table class="checklistTable" id="checkListTable" cellpadding="0" cellspacing="0" runat="server">
    <!-- start of visual checks -->
    <tr>
        <td colspan="4" class="sectionTitle">
            Visual Checks
        </td>
    </tr>
    <tr>
        <td class="columnRadioPictures">
            <asp:Image ID="Image1" runat="server" AlternateText="Ok" ImageUrl="../../images/checkmark.gif" />
            <asp:Image ID="Image2" runat="server" AlternateText="Bad" ImageUrl="../../images/xmark.gif" />
            <asp:Image ID="Image6" runat="server" AlternateText="N/A" ImageUrl="../../images/notapplicable.gif" />
        </td>
        <td class="columnText">
            &nbsp;
        </td>
        <td class="columnRadioPictures">
            <asp:Image ID="Image3" runat="server" AlternateText="Ok" ImageUrl="../../images/checkmark.gif" />
            <asp:Image ID="Image4" runat="server" AlternateText="Bad" ImageUrl="../../images/xmark.gif" />
            <asp:Image ID="Image5" runat="server" AlternateText="N/A" ImageUrl="../../images/notapplicable.gif" />
        </td>
        <td class="columnText">
            &nbsp;
        </td>
    </tr>
    <tr>
        <td class="columnRadio">
            <asp:RadioButtonList ID="VC_MfgManual" RepeatDirection="Horizontal" runat="server">
                <asp:ListItem Value="Ok" Text="" />
                <asp:ListItem Value="Bad" Text="" />
                <asp:ListItem Value="N/A" Text="" />
            </asp:RadioButtonList>
        </td>
        <td class="columnText">
            MANUFACTURER'S MANUAL
        </td>
        <td class="columnRadio">
            <asp:RadioButtonList ID="VC_Hoses" RepeatDirection="Horizontal" runat="server">
                <asp:ListItem Value="Ok" Text="" />
                <asp:ListItem Value="Bad" Text="" />
                <asp:ListItem Value="N/A" Text="" />
            </asp:RadioButtonList>
        </td>
        <td class="columnText">
            HOSES
        </td>
    </tr>
    <tr>
        <td class="columnRadio">
            <asp:RadioButtonList ID="VC_DataPlate" RepeatDirection="Horizontal" runat="server">
                <asp:ListItem Value="Ok" Text="" />
                <asp:ListItem Value="Bad" Text="" />
                <asp:ListItem Value="N/A" Text="" />
            </asp:RadioButtonList>
        </td>
        <td class="columnText">
            DATA PLATE
        </td>
        <td class="columnRadio">
            <asp:RadioButtonList ID="VC_TiresWheels" RepeatDirection="Horizontal" runat="server">
                <asp:ListItem Value="Ok" Text="" />
                <asp:ListItem Value="Bad" Text="" />
                <asp:ListItem Value="N/A" Text="" />
            </asp:RadioButtonList>
        </td>
        <td class="columnText">
            TIRES/WHEELS
        </td>
    </tr>
    <tr>
        <td class="columnRadio">
            <asp:RadioButtonList ID="VC_WarningDecals" RepeatDirection="Horizontal" runat="server">
                <asp:ListItem Value="Ok" Text="" />
                <asp:ListItem Value="Bad" Text="" />
                <asp:ListItem Value="N/A" Text="" />
            </asp:RadioButtonList>
        </td>
        <td class="columnText">
            WARNING DECALS
        </td>
        <td class="columnRadio">
            <asp:RadioButtonList ID="VC_Mirrors" RepeatDirection="Horizontal" runat="server">
                <asp:ListItem Value="Ok" Text="" />
                <asp:ListItem Value="Bad" Text="" />
                <asp:ListItem Value="N/A" Text="" />
            </asp:RadioButtonList>
        </td>
        <td class="columnText">
            MIRRORS
        </td>
    </tr>
    <tr>
        <td class="columnRadio">
            <asp:RadioButtonList ID="VC_OverheadGuard" RepeatDirection="Horizontal" runat="server">
                <asp:ListItem Value="Ok" Text="" />
                <asp:ListItem Value="Bad" Text="" />
                <asp:ListItem Value="N/A" Text="" />
            </asp:RadioButtonList>
        </td>
        <td class="columnText">
            OVERHEAD GUARD
        </td>
        <td class="columnRadio">
            <asp:RadioButtonList ID="VC_SeatBelt" RepeatDirection="Horizontal" runat="server">
                <asp:ListItem Value="Ok" Text="" />
                <asp:ListItem Value="Bad" Text="" />
                <asp:ListItem Value="N/A" Text="" />
            </asp:RadioButtonList>
        </td>
        <td class="columnText">
            SEAT BELT
        </td>
    </tr>
    <tr>
        <td class="columnRadio">
            <asp:RadioButtonList ID="VC_MastAssembly" RepeatDirection="Horizontal" runat="server">
                <asp:ListItem Value="Ok" Text="" />
                <asp:ListItem Value="Bad" Text="" />
                <asp:ListItem Value="N/A" Text="" />
            </asp:RadioButtonList>
        </td>
        <td class="columnText">
            MAST AND BOOM ASSEMBLY
        </td>
        <td class="columnRadio">
            <asp:RadioButtonList ID="VC_FireExtinguisher" RepeatDirection="Horizontal" runat="server">
                <asp:ListItem Value="Ok" Text="" />
                <asp:ListItem Value="Bad" Text="" />
                <asp:ListItem Value="N/A" Text="" />
            </asp:RadioButtonList>
        </td>
        <td class="columnText">
            FIRE EXTINGUISHER
        </td>
    </tr>
    <tr>
        <td class="columnRadio">
            <asp:RadioButtonList ID="VC_LiftChainRoller" RepeatDirection="Horizontal" runat="server">
                <asp:ListItem Value="Ok" Text="" />
                <asp:ListItem Value="Bad" Text="" />
                <asp:ListItem Value="N/A" Text="" />
            </asp:RadioButtonList>
        </td>
        <td class="columnText">
            LIFT CHAINS/ROLLERS
        </td>
        <td class="columnRadio">
            <asp:RadioButtonList ID="VC_FuelCap" RepeatDirection="Horizontal" runat="server">
                <asp:ListItem Value="Ok" Text="" />
                <asp:ListItem Value="Bad" Text="" />
                <asp:ListItem Value="N/A" Text="" />
            </asp:RadioButtonList>
        </td>
        <td class="columnText">
            FUEL CAP
        </td>
    </tr>
    <tr>
        <td class="columnRadio">
            <asp:RadioButtonList ID="VC_Forks" RepeatDirection="Horizontal" runat="server">
                <asp:ListItem Value="Ok" Text="" />
                <asp:ListItem Value="Bad" Text="" />
                <asp:ListItem Value="N/A" Text="" />
            </asp:RadioButtonList>
        </td>
        <td class="columnText">
            FORKS/RAM
        </td>
        <td class="columnRadio">
            <asp:RadioButtonList ID="VC_PropaneTank" RepeatDirection="Horizontal" runat="server">
                <asp:ListItem Value="Ok" Text="" />
                <asp:ListItem Value="Bad" Text="" />
                <asp:ListItem Value="N/A" Text="" />
            </asp:RadioButtonList>
        </td>
        <td class="columnText">
            PROPANE TANK
        </td>
    </tr>
    <tr>
        <td class="columnRadio">
            <asp:RadioButtonList ID="VC_HydraulicCylinders" RepeatDirection="Horizontal" runat="server">
                <asp:ListItem Value="Ok" Text="" />
                <asp:ListItem Value="Bad" Text="" />
                <asp:ListItem Value="N/A" Text="" />
            </asp:RadioButtonList>
        </td>
        <td class="columnText">
            HYDRAULIC CYLINDERS
        </td>
    </tr>
</table>

I have tried the following code behind but am not reading the value of the radiobutton:

foreach (Control ctrl in checkListTable.Controls)
{
    foreach (Control tblCell in ctrl.Controls)
    {
        foreach (Control radioBtn in tblCell.Controls)
        {
            strCtrl = radioBtn.GetType().ToString();
            if (radioBtn is RadioButtonList)
            {
                RadioButtonList rbl = (RadioButtonList)ctrl;

                for (int i = 0; i < rbl.Items.Count; i++)
                {
                    if (rbl.Items[i].Value.ToString() == "Bad")
                    {
                        //get the text value of the column text

                    }
                }
            }
        }
    }
}

Thanks in advance!

You can use the ControlFinder offered by Jimmy in Better way to find control in ASP.NET to get the ID and the SelectedValue property of each RadioButtonList:

ControlFinder<RadioButtonList> radioButtonLists = new ControlFinder<RadioButtonList>();
radioButtonLists.FindChildControlsRecursive(checkListTable);

foreach (RadioButtonList rbl in radioButtonLists.FoundControls)
{
    string id = rbl.ID;
    string value = rbl.SelectedValue;
    ...
}

The text of the adjacent cell (eg MANUFACTURER'S MANUAL ) is not easily accessible however, and the connection with the corresponding RadioButtonList is not obvious.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM