后台cs
protected void Page_Load(object sender, EventArgs e)
{
string s = ConfigurationManager.ConnectionStrings["stu"].ConnectionString;
SqlConnection con = new SqlConnection(s);
con.Open();
dr.Close();
int i = 1;
string str2 = "select * from T_question1";
SqlCommand cmd2 = new SqlCommand(str2, con);
SqlDataReader sdr = cmd2.ExecuteReader();
while (sdr.Read())
{
Literal littxt = new Literal();
RadioButtonList cbk = new RadioButtonList();
}
}
protected void Button1_Click1(object sender, EventArgs e)
{
在这里写radiobuttonlist的selectedvalue
}
怎么在按钮事件中得到value?
while (sdr.Read()) { Literal littxt = new Literal(); RadioButtonList cbk = new RadioButtonList(); }
啥意思
这个相对完整,数据库里读选择题,然后在按button保存答案到数据库
string s = ConfigurationManager.ConnectionStrings["stu"].ConnectionString;
SqlConnection con = new SqlConnection(s);
con.Open();
int i = 1;
string str2 = "select * from T_question1";
SqlCommand cmd2 = new SqlCommand(str2, con);
SqlDataReader sdr = cmd2.ExecuteReader();
while (sdr.Read())
{
Literal littxt = new Literal();
RadioButtonList cbk = new RadioButtonList();
cbk.ID = "cbk" + i.ToString();
littxt.Text = i.ToString() + "、" + Server.HtmlEncode(sdr["questionname"].ToString()) + "<br>";
cbk.Items.Add("A. " + Server.HtmlEncode(sdr["answer_A"].ToString()));
cbk.Items.Add("B. " + Server.HtmlEncode(sdr["answer_B"].ToString()));
cbk.Items.Add("C. " + Server.HtmlEncode(sdr["answer_B"].ToString()));
cbk.Items.Add("D. " + Server.HtmlEncode(sdr["answer_B"].ToString()));
cbk.Font.Size = 11;
for (int j = 1; j <= 4; j++)
{
cbk.Items[j - 1].Value = j.ToString();
}
Panel1.Controls.Add(littxt);
Panel1.Controls.Add(cbk);
i++;
}
}
protected void Button1_Click1(object sender, EventArgs e)//写入数据库提交sub_ans
{
//这里怎么取到selectedvalue?
}
@mmypdf: cbk.SelectedItem.Text.Trim();
@羽商宫: Button1_Click1里找不到哦啊cbk......
@mmypdf: 因为你用panel把cbk包含了。。。。在panel里用findcontrol方法
@羽商宫: 我aspx页面没有radiobuttonlist,只想顺序的找到cbk.ID=“cbk”+i.tostring();
@mmypdf: Panel1.Controls.Add(littxt);
Panel1.Controls.Add(cbk);你这不是把cbk包含到了前台的panel里了吗
@羽商宫:Panel1.FindControl("cbk").ToString();
未将对象引用设置到对象的实例.....
@mmypdf: 你findcontrol之后得到的是一个radiobuttonlist,能tostring吗。。。。你还是搜一下如何findcontrol吧