从PostBack上的Dynamic

从PostBack上的Dynamic

本文介绍了从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

推荐答案

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 + "




快乐的编码;)



Happy coding ;)


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中获取选定的单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 12:41