从PostBack上的Dynamic RadioButtonlist中获取选定的单选按钮 [英] Get selected radio butoon from Dynamical RadioButtonlist on PostBack

查看:67
本文介绍了从PostBack上的Dynamic RadioButtonlist中获取选定的单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我提出问题之前,我将先介绍一下该senario:
我使用动态方式对单选按钮列表进行控制,此列表包含许多单选按钮
问题是我无法在POSTBACK上捕获所选的值

我需要使用Request.Form [id];->解决方案这返回没有选择值的单选按钮列表的ID.

I will describe the senario before i give the problem:
i use dynamical way to radio button list control this list contain many radio button
the problem is that i can''t catch the selected value on the POSTBACK

I NEED THE SOLUTION USING Request.Form[id];--> this return the id of radiobutton list without the selected value

推荐答案

我通过使用Request.FOrm [ID]找到了解决方案,我所需要的就是定义eadio按钮同一组中具有相同组名但单选按钮值不相同的示例,例如:
I found the solution by using Request.FOrm[ID] all what i need is to define the eadio button in the same group having same groupname but not the same radiobutton value ex:
public RadioButtonControl (String Question, String[] tabRadioButton)
           {

           Label ques = new Label();
           ques.Text = Question;

           Table tabl = new Table();
           TableRow tablRow = new TableRow();
           TableCell tablCell = new TableCell();
           tablCell.Controls.Add(ques);

           tablRow.Cells.Add(tablCell);
           tabl.Rows.Add(tablRow);

           this.Controls.Add(tabl);

           RadioButtonList list1 = new RadioButtonList();
           list1.RepeatLayout = RepeatLayout.Table;
           list1.BorderWidth = 2 ;

           foreach(string s in tabRadioButton)
           {
               ListItem li = new ListItem();
               RadioButton tb = new RadioButton();
                   tb.Text = s;
               tb.GroupName = Question;
                   li.Text = tb.Text;
                   li.Value = tb.Text;
                   list1.Items.Add(li);

           }

           list1.RepeatDirection =  RepeatDirection.Horizontal;
           list1.RepeatColumns = 3;
           UsedRadioButtonList = list1;//for set and get

           this.Controls.Add(list1);
       }


      //this method will get the selected value
      public String GetSelectedValueById(string Id,HttpRequest Request)
       {
          string d ="";
          String[] st = Id.Split('_');
          d = Id + "


ctl01" ; 返回 Request.Form [d]; }
ctl01"; return Request.Form[d]; }



快乐的编码;)



Happy coding ;)



要绑定RaidButton,您必须实现Radiolist_DataBound并遍历所有项目.我提出了解决方案,但是我不需要使用Request.form,因为我创建了Radiolist并将其绑定到一个步骤中.
背后的代码:
Hi ,
To bind the RaidButton u have to implement Radiolist_DataBound and loop through the items . I made solution but i didnt need use Request.form coz i create the Radiolist and bind it in one step .
Code behind :
protected System.Web.UI.WebControls.RadioButtonList raid;
   DataClassesDataContext db = new DataClassesDataContext();
   protected void Page_Load(object sender, EventArgs e)
   {
   }
   int countTimes = 0;
   protected void Button1_Click(object sender, EventArgs e)
   {
       if (ViewState["countTimes"] == null)
       {
           countTimes = 1;
       }
       else
       {
           countTimes = Convert.ToInt32(ViewState["countTimes"]);
       }

       for (int i = 0; i < countTimes; i++)
       {
           raid = new RadioButtonList();
           raid.ID = "raid" + i;



           form1.Controls.Add(raid);


       }
       countTimes = countTimes + 1;

       ViewState.Add("countTimes", countTimes);
       var result = from x in db.cates
                    select new { x.chk, x.id };
       raid.DataSource = result;
       raid.DataTextField = "chk";
       raid.DataValueField = "chk";

       raid.DataBind();
       raid_DataBound(raid, new EventArgs());
   }

   void raid_DataBound(object sender, EventArgs e)
   {
       foreach (ListItem item in raid.Items)
       {
           item.Selected = bool.Parse(item.Value);
       }
   }


<div>
          <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /><br />
 </div>


最好的问候
M.mitwalli


Best Regards
M.mitwalli


这篇关于从PostBack上的Dynamic RadioButtonlist中获取选定的单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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