Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 始终选中顶部(有条件)单选按钮_C#_Asp.net_Radio Button_Selected - Fatal编程技术网

C# 始终选中顶部(有条件)单选按钮

C# 始终选中顶部(有条件)单选按钮,c#,asp.net,radio-button,selected,C#,Asp.net,Radio Button,Selected,我当前将一些值传递给我的一个ascx.cs文件,它会根据发送的值显示某些单选按钮。单选按钮在.ascx一侧实例化: <asp:RadioButton ID="length5yr" GroupName="SubLength" runat="server" Visible ="false" /> <asp:label runat="server" id="Label5yr" Visible="false" style="width

我当前将一些值传递给我的一个ascx.cs文件,它会根据发送的值显示某些单选按钮。单选按钮在.ascx一侧实例化:

<asp:RadioButton ID="length5yr" GroupName="SubLength" runat="server" Visible ="false"
             /> 
            <asp:label runat="server" id="Label5yr" Visible="false" style="width: 300px">
            </asp:label>


             <asp:RadioButton ID="length3yr" GroupName="SubLength" runat="server"  Visible ="false"
             />
            <asp:label runat="server" id="Label3yr" Visible="false" style="width: 300px">
            </asp:label>


     <asp:RadioButton ID="length2yr" GroupName="SubLength" runat="server"  Visible ="false"
             />
            <asp:label runat="server" id="Labelyr2" Visible="false" style="width: 300px">
            </asp:label>


     <asp:RadioButton ID="length1yr" GroupName="SubLength" runat="server"  Visible ="false"
             />
            <asp:label runat="server" id="Label1yr" Visible="false" style="width: 300px">

            </asp:label>

     <asp:RadioButton ID="lengthQuarterly" GroupName="SubLength" runat="server"  Visible ="false"
             />
            <asp:label runat="server" id="labelQuarterly" Visible="false" style="width: 300px">
            </asp:label>

    <asp:RadioButton ID="lengthMonthly" GroupName="SubLength" runat="server"  Visible ="false"
             />
            <asp:label runat="server" id="labelMonthly" Visible="false" style="width: 300px">
            </asp:label>

我希望选中最后显示的第一个/顶部的单选按钮。我已经能够检查最后一个单选按钮(这也给我带来了其他问题)-但是我如何才能检查最上面的那个按钮呢

这是我如何确定显示哪个单选按钮的代码-我只显示了其中一个选项,只是为了让代码更容易阅读

 foreach (Dictionary<string, string> d in orderOptions)
        {
            if (d["length"] == "one")
            {
                length1yr.Visible = true;
                price1yr = d["price"];
                rate1yr = d["rate"];
                issues1yr = d["issues"];
                premium1yr = d["premium"];
                Label1yr.Text = d["description"] + "<br><br>";
                Label1yr.Visible = true;
                SubLength1yr = "1 year";

            }
foreach(orderOptions中的字典d)
{
如果(d[“长度”]=“一”)
{
可见长度1年=真实;
价格1年=d[“价格”];
1年费率=d[“费率”];
发行1年=d[“发行”];
溢价1年=d[“溢价”];
Label1yr.Text=d[“说明”]+“

”; 标签1年可见=真实; 转租1年=“1年”; }
对于服务器端解决方案,最简单的方法是遍历
RadioButton
列表,并使用一些简单的LINQ选择第一个可见按钮

比如:

var buttons = new []{length5yr, ...};
var firstVisibleButton = buttons.Where(b=>b.Visible).First();
firstVisibleButton.Checked = true;
上面的代码可能比任何代码都要假,但如果没有完整的代码示例,我只能这样做。我相信您可以了解详细信息。上面的代码必须在您隐藏/显示
单选按钮后执行


我可能应该指定您创建的列表(按钮)必须遵循按钮的显示顺序才能正常工作。

您是如何创建单选按钮的?是动态创建的,还是它们总是在那里,只是根据某些条件隐藏?啊,它们总是在那里,只是根据条件隐藏。您想要服务器端的客户端解决方案吗?还是不想要这很重要吗?我想这并不太重要-只要它完成了工作,我从中得到了一些东西,我就同意解决方案。你能更新这个问题,包括它吐出的HTML以及你用来确定哪个单选按钮的代码吗显示?问题是,当我在第一个单选按钮下方选择某个内容时,它不会将正确的数据传递到下一页:/n它总是传递附加到显示的“第一个”单选按钮上的数据。我们在这里谈论的是什么数据?模糊-我知道。有一种方法在有人单击“提交”时运行与这些单选按钮位于同一页面上的按钮。根据选择的单选按钮,它会传递某些信息。因此,当按下按钮时,如果选中radiobutton1.checked,它会设置x、y和z变量的某些值,这些值最终会被传递。如果选中radiobutton2.checked,这些值将不同。在这种情况下,单选按钮2(例如)已选中,但它正在传递值,就好像单选按钮1仍处于选中状态一样。我对ASP.net有点生疏,但听起来您在“太早”完成某些工作在ASP页面循环中,按钮的ViewState尚未应用或类似的内容。如果您希望在此处获得更多帮助,您可能必须发布包含更多代码的小代码示例(不要使用实际代码)。创建较小的代码示例还可以通过尝试较小的范围来帮助您找到问题的根源。