如何获得动态radiobuttonlist项目选择值? [英] How to get dynamic radiobuttonlist item selected value?
问题描述
我想做调查。
在调查用户控制中有一个占位符(占位符有动态创建的标签和动态创建的radiobuttonlist)。我检查了有多少问题。
usercontrol - >占位符 - >标签和radiobuttonlist(动态创建,因为我们不知道有多少问题)
没有问题可达这里我得到问题并创建标签和radiobuttonlist
MySqlCommand command = new MySqlCommand(xxxxx,conn);
command.CommandType = System.Data.CommandType.StoredProcedure;
command.Parameters.Add(@ surveyno,MySqlDbType.VarChar).Value = yyy;
conn.Open();
MySqlDataAdapter dAdapter = new MySqlDataAdapter(command);
DataSet dset = new DataSet();
dAdapter.Fill(dset);
MySq lDataReader reader = command.ExecuteReader();
while(reader.Read())
{
标签myLabel = new Label();
RadioButtonList rdr = new RadioButtonList();
rdr.Items.Add(new ListItem(是,1));
rdr.Items.Add(new ListItem(No,2));
rdr.RepeatDirection = System。 Web.UI.WebControls.RepeatDirection.Horizontal;
rdr.DataBind();
myLabel。 Text = reader [sorular]。ToString();
PlaceHolder1.Controls.Add(myLabel);
PlaceHolder1.Controls.Add(rdr);
PlaceHolder1.Controls.Add(new LiteralControl(
));
< br $>
}
但我选择答案问题一是问题二不等等点击保存按钮它找不到占位符中的radiobuttonlist。
这样的代码
Iwant to do survey.
there is an placeholder(place holder has dynamic created label and dynamic created radiobuttonlist) inside survey usercontrol.I checked how many question are there.
usercontrol-->placeholder-->label and radiobuttonlist(created dynamicly because we dont know how many question are there)
there is no problem up to here Iam getting questions and create label and radiobuttonlist
MySqlCommand command = new MySqlCommand("xxxxx", conn);
command.CommandType = System.Data.CommandType.StoredProcedure;
command.Parameters.Add("@surveyno", MySqlDbType.VarChar).Value = yyy;
conn.Open();
MySqlDataAdapter dAdapter = new MySqlDataAdapter(command);
DataSet dset = new DataSet();
dAdapter.Fill(dset);
MySqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
Label myLabel = new Label();
RadioButtonList rdr = new RadioButtonList();
rdr.Items.Add(new ListItem("Yes", "1"));
rdr.Items.Add(new ListItem("No", "2"));
rdr.RepeatDirection=System.Web.UI.WebControls.RepeatDirection.Horizontal;
rdr.DataBind();
myLabel.Text = reader["sorular"].ToString();
PlaceHolder1.Controls.Add(myLabel);
PlaceHolder1.Controls.Add(rdr);
PlaceHolder1.Controls.Add(new LiteralControl("
"));
}
but I select answers question one yes question two no etc. click save button it dont find radiobuttonlist in the placeholder.
Code like this
foreach (Control ctrl in this.PlaceHolder1.Controls)
{
if (ctrl is RadioButtonList)
{
string soruid = ((RadioButtonList)ctrl).ID;
string puan = ((RadioButtonList)ctrl).SelectedValue;
if (puan == null || puan == "")
puan = "0";
}
}
推荐答案
首先给radiobuttonlistID如:rdr.ID =rdlAnswer
然后使用FindControl函数通过ID找到控件
Firstly give the radiobuttonlist "ID" Such as : rdr.ID = "rdlAnswer"
then use "FindControl" function to find the control by "ID"
RadioButtonList rdr = (PlaceHolder1.FindControl("rdlAnswer")) as RadioButtonList;
请试试.....
Try it please.....
这篇关于如何获得动态radiobuttonlist项目选择值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!