首页 新闻 会员 周边 捐助

radiobuttonlist的问题

0
悬赏园豆:20 [已解决问题] 解决于 2014-05-11 15:21

后台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?

mmypdf的主页 mmypdf | 初学一级 | 园豆:134
提问于:2014-05-04 17:27
< >
分享
最佳答案
0
while (sdr.Read())
{
Literal littxt = new Literal();
RadioButtonList cbk = new RadioButtonList();

}

啥意思

收获园豆:20
羽商宫 | 老鸟四级 |园豆:2490 | 2014-05-05 10:04

这个相对完整,数据库里读选择题,然后在按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 | 园豆:134 (初学一级) | 2014-05-05 10:37

@mmypdf: cbk.SelectedItem.Text.Trim();

羽商宫 | 园豆:2490 (老鸟四级) | 2014-05-05 10:58

@羽商宫: Button1_Click1里找不到哦啊cbk......

mmypdf | 园豆:134 (初学一级) | 2014-05-05 11:00

@mmypdf: 因为你用panel把cbk包含了。。。。在panel里用findcontrol方法

羽商宫 | 园豆:2490 (老鸟四级) | 2014-05-05 11:02

@羽商宫: 我aspx页面没有radiobuttonlist,只想顺序的找到cbk.ID=“cbk”+i.tostring();

mmypdf | 园豆:134 (初学一级) | 2014-05-05 11:11

@mmypdf: Panel1.Controls.Add(littxt);
Panel1.Controls.Add(cbk);你这不是把cbk包含到了前台的panel里了吗

羽商宫 | 园豆:2490 (老鸟四级) | 2014-05-05 11:45

@羽商宫:Panel1.FindControl("cbk").ToString();

 未将对象引用设置到对象的实例.....

mmypdf | 园豆:134 (初学一级) | 2014-05-05 21:05

@mmypdf: 你findcontrol之后得到的是一个radiobuttonlist,能tostring吗。。。。你还是搜一下如何findcontrol吧

羽商宫 | 园豆:2490 (老鸟四级) | 2014-05-06 10:23
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册