如何从ASP.NET面板控件中获取单选按钮? [英] How to acess the radio button from the ASP.NET panel control ?

查看:71
本文介绍了如何从ASP.NET面板控件中获取单选按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个单选按钮列表,这些按钮是在asp.net面板控件中动态创建的。Panel控件也将以dyanamically方式创建。

,面板将添加到用户在设计时设计的面板控件。







注意 - pnlBox是我在设计时设计的面板控件



设计

面板

||

包含多个运行时面板

||

每个运行时面板包含5个运行时单选按钮。 br />


现在我想获取用户选择的radiobutton值。



请给我一些解决方案frnds ...........



我的尝试:



我试图在运行时添加控件的代码在c#

Suppose i have a list of radio button which are created dyanamically inside the asp.net panel control .The Panel control will also be created dyanamically .
and the panel will be added to a Panel control which is designed by user at design time.



Note - pnlBox is the panel control which is designed by me at design time

Designed
Panel
||
contain Multiple Runtime Panels
||
Each Runtime Panel contain 5 runtime radio buttons .

Now i want to acess the the radiobutton value which is selected by user .

please give me some solutions frnds ...........

What I have tried:

the code which i have tried to add the control at run time in c#

Panel pnl = new Panel();
         pnl.Height = 25;

         RadioButton rb1 = new RadioButton();
         rb1.Text = "Excellent"
         pnl.Controls.Add(rb1);

         RadioButton rb2 = new RadioButton();
         rb2.Text = "Very Good";
         pnl.Controls.Add(rb2);

         RadioButton rb3 = new RadioButton();
         rb3.Text = "Good";
         pnl.Controls.Add(rb3);

         pnlBox.Controls.Add(pnl)

推荐答案

<form id="form1" runat="server">
 
    <asp:Panel ID="pnlBox" runat="server">

    </asp:Panel>
    <asp:Button ID="ButtonSubmit" OnClick="ButtonSubmit_Click" Text="Submit" runat="server" />
</form>





代码落后





code behind

protected void Page_Load(object sender, EventArgs e)
{
    Panel pnl = new Panel();
    pnl.ID = "MyPanel";
    pnl.Height = 25;
 
    RadioButton rb1 = new RadioButton();
    rb1.Text = "Excellent";
    rb1.GroupName = "MyRadio";
    pnl.Controls.Add(rb1);
 
    RadioButton rb2 = new RadioButton();
    rb2.Text = "Very Good";
    rb2.GroupName = "MyRadio";
    pnl.Controls.Add(rb2);
 
    RadioButton rb3 = new RadioButton();
    rb3.Text = "Good";
    rb3.GroupName = "MyRadio";
    pnl.Controls.Add(rb3);

    pnlBox.Controls.Add(pnl);

}

protected void ButtonSubmit_Click(object sender, EventArgs e)
{
    Panel myPanel = (Panel) pnlBox.FindControl("MyPanel");
    RadioButton selected = myPanel.Controls.OfType<RadioButton>().Where(b => b.Checked).FirstOrDefault();

}


这篇关于如何从ASP.NET面板控件中获取单选按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆