.net c#编程中,不在.aspx中加入RadioButtonList,而是在.aspx.cs中,怎么加列表内容?
RadioButtonListRbl1=newRadioButtonList();Rbl1.AutoPostBack=true;Rbl1.Items[0].Text="1...
RadioButtonList Rbl1 = new RadioButtonList();
Rbl1.AutoPostBack = true;
Rbl1.Items[0].Text = "10岁以下";
Rbl1.Items[1].Text = "10-20岁";
Rbl1.Items[2].Text = "20-30岁";
Rbl1.Items[3].Text = "30-40岁";
Rbl1.Items[4].Text = "40-50岁";
this.form1.Controls.Add(Rbl1);
Label lab1 = new Label();
for (int i = 0; i < Rbl1.Items.Count; i++)
{
if (Rbl1.Items[i].Selected == true)
{
lab1.Text = Rbl1.Items[i].Text;
}
}
this.form1.Controls.Add(lab1);
这样是不对的,但是也不知道哪错.该怎么写? 展开
Rbl1.AutoPostBack = true;
Rbl1.Items[0].Text = "10岁以下";
Rbl1.Items[1].Text = "10-20岁";
Rbl1.Items[2].Text = "20-30岁";
Rbl1.Items[3].Text = "30-40岁";
Rbl1.Items[4].Text = "40-50岁";
this.form1.Controls.Add(Rbl1);
Label lab1 = new Label();
for (int i = 0; i < Rbl1.Items.Count; i++)
{
if (Rbl1.Items[i].Selected == true)
{
lab1.Text = Rbl1.Items[i].Text;
}
}
this.form1.Controls.Add(lab1);
这样是不对的,但是也不知道哪错.该怎么写? 展开
2013-09-23
展开全部
加一个委托,代码如下
RadioButtonList Rbl1 = new RadioButtonList();
Rbl1.AutoPostBack = true;
Rbl1.Items.Add("10岁以下");
Rbl1.Items.Add("10-20岁");
Rbl1.Items.Add("20-30岁");
Rbl1.Items.Add("30-40岁");
Rbl1.Items.Add("40-50岁");
this.form1.Controls.Add(Rbl1);
Rbl1.AutoPostBack = true;
Rbl1.SelectedIndexChanged += new EventHandler(this.SelectChange);
Label lab1 = new Label();
lab1.ID = "labShow";
lab1.ForeColor = System.Drawing.Color.Red;
lab1.Text = Rbl1.SelectedValue;
this.form1.Controls.Add(lab1);
private void SelectChange(object sender, EventArgs e)
{
Label lab = (Label)this.form1.FindControl("labShow");
RadioButtonList rblist=(RadioButtonList)sender;
lab.Text = rblist.SelectedValue;
}
RadioButtonList Rbl1 = new RadioButtonList();
Rbl1.AutoPostBack = true;
Rbl1.Items.Add("10岁以下");
Rbl1.Items.Add("10-20岁");
Rbl1.Items.Add("20-30岁");
Rbl1.Items.Add("30-40岁");
Rbl1.Items.Add("40-50岁");
this.form1.Controls.Add(Rbl1);
Rbl1.AutoPostBack = true;
Rbl1.SelectedIndexChanged += new EventHandler(this.SelectChange);
Label lab1 = new Label();
lab1.ID = "labShow";
lab1.ForeColor = System.Drawing.Color.Red;
lab1.Text = Rbl1.SelectedValue;
this.form1.Controls.Add(lab1);
private void SelectChange(object sender, EventArgs e)
{
Label lab = (Label)this.form1.FindControl("labShow");
RadioButtonList rblist=(RadioButtonList)sender;
lab.Text = rblist.SelectedValue;
}
2013-09-23
展开全部
修改成这样就行了,我自己通过测试了!
if (!IsPostBack)
{
RadioButtonList Rbl1 = new RadioButtonList();
//Rbl1.AutoPostBack = true;
Rbl1.Items.Add("10");
Rbl1.Items.Add("10-20");
Rbl1.Items.Add("20-30");
Rbl1.Items.Add("30-40");
Rbl1.Items.Add("40-50");
Rbl1.SelectedIndex = 2;
this.form1.Controls.Add(Rbl1);
Label lab1 = new Label();
lab1.ForeColor = System.Drawing.Color.Red;
lab1.Text = Rbl1.SelectedValue;
this.form1.Controls.Add(lab1);
}
if (!IsPostBack)
{
RadioButtonList Rbl1 = new RadioButtonList();
//Rbl1.AutoPostBack = true;
Rbl1.Items.Add("10");
Rbl1.Items.Add("10-20");
Rbl1.Items.Add("20-30");
Rbl1.Items.Add("30-40");
Rbl1.Items.Add("40-50");
Rbl1.SelectedIndex = 2;
this.form1.Controls.Add(Rbl1);
Label lab1 = new Label();
lab1.ForeColor = System.Drawing.Color.Red;
lab1.Text = Rbl1.SelectedValue;
this.form1.Controls.Add(lab1);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-09-23
展开全部
请你在:Page_Load事件中加入如下的代码
protected void Page_Load(object sender, EventArgs e)
{
ListItem lst1 = new ListItem();
ListItem lst2 = new ListItem();
ListItem lst3 = new ListItem();
ListItem lst4 = new ListItem();
ListItem lst5 = new ListItem();
lst1.Value = "10岁以下";
lst2.Value = "10-20岁";
lst3.Value = "20-30岁";
lst4.Value = "30-40岁";
lst5.Value = "40-50岁";
this.RadioButtonList1.Items.Add(lst1);
this.RadioButtonList1.Items.Add(lst2);
this.RadioButtonList1.Items.Add(lst3);
this.RadioButtonList1.Items.Add(lst4);
this.RadioButtonList1.Items.Add(lst5);
}
protected void Page_Load(object sender, EventArgs e)
{
ListItem lst1 = new ListItem();
ListItem lst2 = new ListItem();
ListItem lst3 = new ListItem();
ListItem lst4 = new ListItem();
ListItem lst5 = new ListItem();
lst1.Value = "10岁以下";
lst2.Value = "10-20岁";
lst3.Value = "20-30岁";
lst4.Value = "30-40岁";
lst5.Value = "40-50岁";
this.RadioButtonList1.Items.Add(lst1);
this.RadioButtonList1.Items.Add(lst2);
this.RadioButtonList1.Items.Add(lst3);
this.RadioButtonList1.Items.Add(lst4);
this.RadioButtonList1.Items.Add(lst5);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询