【问题标题】:Best way to find the value of the selected radio button in a group of radio buttons (associated with GroupName attribute) in ASP.NET在 ASP.NET 中的一组单选按钮(与 GroupName 属性相关联)中查找选定单选按钮的值的最佳方法
【发布时间】:2020-05-02 11:23:51
【问题描述】:

我正在从共享相同 GroupName 属性的三个单选按钮中检索所选单选按钮的值。当我在我的应用程序中使用 MVP 模式时,我需要使用 C# 代码隐藏找到这个值,并将这个值从代码隐藏传递给演示者。

在 .aspx 文件中:

<table>
   <tr>
      <td style="width: 25px">
         <asp:Label ID="rmd" runat="server" Text="*" CssClass="RequiredField" />
      </td>
      <td align="left" style="width: 350px">
         <asp:Label ID="rmd2" runat="server" Text="Raw material dimensions (width, length, thickness)" CssClass="FieldHeader"></asp:Label>
      </td>
      <td valign="bottom">
         <asp:RadioButton ID="rbRawMaterialDimensions" runat="server" TextAlign="Left" CssClass="FieldHeader"
                                GroupName="PlatesManufacturedInHouse" />
      </td>
   </tr>
   <tr>
      <td style="width: 25px">
         <asp:Label ID="hpn" runat="server" Text="*" CssClass="RequiredField" />
      </td>
      <td align="left" style="width: 350px">
         <asp:Label ID="hpn2" runat="server" Text="New HPN Created (main OR raw material item)" CssClass="FieldHeader"></asp:Label>
      </td>
      <td valign="bottom">
         <asp:RadioButton ID="rbNewHPN" runat="server" TextAlign="Left" CssClass="FieldHeader"
                                GroupName="PlatesManufacturedInHouse" />
      </td>
   </tr>
   <tr>
      <td style="width: 25px">
         <asp:Label ID="na" runat="server" Text="*" CssClass="RequiredField" />
      </td>
      <td align="left" style="width: 350px">
         <asp:Label ID="na2" runat="server" Text="Not Applicable" CssClass="FieldHeader"></asp:Label>
      </td>
      <td valign="bottom">
         <asp:RadioButton ID="rbNotApplicable" runat="server" TextAlign="Left" CssClass="FieldHeader"
                                GroupName="PlatesManufacturedInHouse" />
      </td>
   </tr>
   <tr>
         <asp:Label ID="Label24" runat="server" Text="Required Field Missing: An item classification is required."
                                TextAlign="Left" CssClass="Warning" Visible="False" />
   </tr>
</table>

我曾尝试使用&lt;asp:RadioButtonList&gt; 标签,但由于我需要为每个按钮添加标签(以及将来添加验证),因此使用GroupName 属性似乎是目前最好的解决方案,因为就我在网上看到的。

我检查了其他 StackOverflow 问题和答案,以找到找到所选单选按钮及其相关值的最佳方法,但我似乎无法在我的场景中找到最佳解决方案。与我的问题最接近的情况是this question I found on here.

我对问题的answer 的含义感到困惑:

radioButtonsContainer 是单选按钮的容器

我在三个单选按钮周围创建了一个&lt;asp:Panel ID="PanelPlatesManufacturedInHouse" runat="server" CssClass="outercontainer"&gt; 标记,但不确定这是否是使用容器的正确方法。

我现在似乎被困在我的实施中,如果有人知道最好的方法,我将不胜感激:)!

感谢阅读!

【问题讨论】:

    标签: c# asp.net .net radio-button


    【解决方案1】:

    您可以为您的表格指定一个类或 ID,然后循环该表格中的所有单选按钮以查找值。

    所以添加一个ID

    <table id="myTable">
    

    然后找到正确的值

    <script>
        function findRadioValue() {
            $('#myTable input[type=radio]').each(function () {
                if ($(this).prop('checked')) {
                    alert($(this).val());
                }
            });
        }
    </script>
    

    更新

    我不知何故虽然它是 jQuery,但我的错。但是我创建了一种方法来根据 GroupName 获取正确的控件 ID。请注意,这使用母版页。如果你不使用你使用foreach (var control in Page.Controls)

    public string getRadioButtonIdByGroupName(string groupName)
    {
        //loop all controls on the page
        foreach (var control in (Master.FindControl("ContentPlaceHolder1") as ContentPlaceHolder).Controls)
        {
            //check if the control is a radiobutton
            if (control is RadioButton)
            {
                //cast the control to a radiobutton
                var radioButton = control as RadioButton;
    
                //check if it is the correct group name and if it's checked
                if (radioButton.GroupName == groupName && radioButton.Checked)
                {
                    //return the value
                    return radioButton.ID;
                }
            }
        }
    
        return "NotFound";
    }
    

    【讨论】:

    • 感谢您的回答,但我在后端使用 MVP 模式,需要使用 C# 代码隐藏检索信息以传递给演示者。我将编辑我的问题以更清楚地说明这一点,但如果我使用的是 JS,我肯定会尝试一下!
    • 更新了我的答案
    • 完美,这正是我想要的。我通过在我的单选按钮部分周围包含一个面板并解析单选按钮的面板来使用这个答案。这与您的逻辑解决了我的问题。再次感谢!!
    猜你喜欢
    • 1970-01-01
    • 2011-05-21
    • 1970-01-01
    • 2010-11-14
    • 2011-07-01
    • 2021-05-21
    • 1970-01-01
    • 2011-04-27
    相关资源
    最近更新 更多