在Page_Load方法里
          RadioButtonList objRadioButtonList = new RadioButtonList();
          VoteArray为从别处取到值的一个数组
            for (int i = 0; i < VoteArray.Length; i++)
            {
                objRadioButtonList.Items.Add(VoteArray[i]);
                //InAll = InAll + Convert.ToInt32(votenum[i]);
            }
            objRadioButtonList.ID = "radio1";
            PlaceHolder1.Controls.Add(objRadioButtonList);
在一个按钮的单击事件里buttonsend_click
        for (int j = 0; j < VoteArray.Length; j++)       
        {
                if (radio1.SelectedIndex== VoteArray[j])
                {
                 VotenumArray[j] = Convert.ToString(Convert.ToInt32(VotenumArray[j]) + 1);
                }
         }
提示“当前上下文不存在‘radio1’”弄了好几个小时了,请前辈赐教,在线等,谢谢

解决方案 »

  1.   

    动态添加控件的问题.到博客堂的思归的blog上看看,他讲过这个问题.
      

  2.   

    要保证 回发的时候该控件也要在叶面上see:http://forums.asp.net/thread/1254870.aspx
      

  3.   

    楼主:
      你该在页面的class里面申明个变量 protected RadioButtonList objRadioButtonList;
    这样才能算是Page的成员 要不你怎么找到控件呢?下面的代码我测试成功
    public class CSDN : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.PlaceHolder PlaceHolder1;
    protected System.Web.UI.WebControls.Button Button1;
    protected Button btnTest;
    private void Page_Load(object sender, System.EventArgs e)
    {
    btnTest = new Button();
    btnTest.Text = "您好";
    PlaceHolder1.Controls.Add(btnTest);
    }         private void Button1_Click(object sender, System.EventArgs e)
    {
    btnTest.Enabled = false;
    }
    }
      

  4.   

    在一个按钮的单击事件里buttonsend_click
            for (int j = 0; j < VoteArray.Length; j++)       
            {
                    if (radio1.SelectedIndex== VoteArray[j])
                    {
                     VotenumArray[j] = Convert.ToString(Convert.ToInt32(VotenumArray[j]) + 1);
                    }
             }
    try:        RadioButtonList radio1=(RadioButtonList)PlaceHolder1.FindControl("radio1");        for (int j = 0; j < VoteArray.Length; j++)       
            {
                    if (radio1.SelectedIndex== VoteArray[j])
                    {
                     VotenumArray[j] = Convert.ToString(Convert.ToInt32(VotenumArray[j]) + 1);
                    }
             }
      

  5.   

    http://community.csdn.net/Expert/topic/4900/4900971.xml?temp=.303158
    我回答过了,不知道可不可以用啊
      

  6.   

    动态添加的控件,不能直接通过其ID调用,必须通过FindControl获取,例如:
    (RadioButtonList)PlaceHolder1.FindControl("radio1");
      

  7.   

    private Int32[] VoteArray ={ 1,2,3,4};
        protected void Page_Load(object sender, EventArgs e)
        {
            RadioButtonList objRadioButtonList = new RadioButtonList();
            for (int i = 0; i < VoteArray.Length; i++)
                {
                    objRadioButtonList.Items.Add(VoteArray[i].ToString());
                    //InAll = InAll + Convert.ToInt32(votenum[i]);
                }
            objRadioButtonList.ID = "radio1";
            PlaceHolder1.Controls.Add(objRadioButtonList);    }
        protected void btn_Vote_Click(object sender, EventArgs e)
        {
            RadioButtonList radio1 = (RadioButtonList)PlaceHolder1.FindControl("radio1");
            for (int j = 0; j < VoteArray.Length; j++)
            {
                if (radio1.SelectedIndex == VoteArray[j])
                {
                    //VotenumArray[j] = Convert.ToString(Convert.ToInt32(VotenumArray[j]) + 1);
                    //Response.Write("<script language='javascript'>alter(" + radio1.SelectedIndex + ");</script>");
                    Vote.Text = radio1.SelectedIndex.ToString();
                }
            }
        }
      

  8.   

    谢谢tangxuehua(netfocus)的回答,用你的方法已经实现了
    同时感谢Brookes(边走边唱),cat_hsfz(我的Blog在http://purl.oclc.org/NET/cathsfz)的提点