在页面notesort的地方动态添加RadioButton后,想根据选择不同的RadioButton来显示不同的内容。
代码如下:
-----------------------------------------------------------------
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
 if(!this.IsPostBack)
 {
  string sql="select id,sortName from T_noteSort where parentid=0";
  Run_P.GetDs(sql,"T_noteSort");
  int cc=1;
//---------------在notesort处动态添加RadioButton   foreach ( DataRow ii in Run_P.ds.Tables["T_noteSort"].Rows ){
    RadioButton myRB = new RadioButton();
    myRB.ID = "RB"+cc;
    myRB.GroupName= "ntSortname";
    myRB.Text = ii["sortName"].Tostring();
    myRB.CheckedChanged +=new EventHandler(myRB_CheckedChanged);
    this.notesort.Controls.Add(myRB);
    cc++;
   }
  }
}
//---------------------------------------------------------------触发的事件
private void myRB_CheckedChanged(object sender, EventArgs e)
{
   string sql="select id,sortname from T_noteSort where id="+e.Tostring();
   Run_P.GetDs(sql,"T_noteSort");
   this.sortname.text=Run_P.ds.Tables["T_noteSort"].Rows[0]["sortname"].Tostring();}
//---------------------------------------------------------------在页面中notesort位置添加动态RadioButton成功,但是事件不能触发,是为什么?
不知道写在触发事件中获取动态生成的RadioButton提交过来的value的方法是不是对的?
请高手指点。还有个问题就是,动态生成的RadioButton在页面中查看源代码看到的是:
<input id="RB1" type="radio" name="ntSortname" value="RB1" /><label for="RB1">科技话题</label>
我怎么才能让id和value不一致了?好像RadioButton控件不能设置value的值一样,要如何设置RadioButton的value值了?

解决方案 »

  1.   

    把 if(!this.IsPostBack)去掉
    动态创建的控件每次页面加载时都要创建,而不仅仅是第一次
      

  2.   

    谢谢,成功了,但是他每次页面会要加载一下(就是会闪一下),可以让他不加载么?
    还有如何指定<input id="RB1" type="radio" name="ntSortname" value="RB1" /><label for="RB1">科技话题</label>
    中的value值了?他老是和id同步
      

  3.   

    闪一下是正常的..因为它是WebControls.每次也会提交的..
    那个VALUE..我也不懂.
      

  4.   

    fancyf(凡瑞) ,JzeroBiao(先知) ,谢谢你们,那个问题你帮我问问可以么?,这100分怎么分了。真的不好处理
      

  5.   

    CheckedChanged是个服务器端事件,要触发必须要重新加载一下,不可避免
    该用js能解决部分问题,不过得自己写不少javascript了