我正在写一个asp.net网站,在数据库中,我有一个问题表和答案选项表。我想根据数据库条目动态生成单选按钮或复选框服务器控件。
还允许用户点击提交按钮,将用户选择的答案保存到数据库中。
关于如何实现这一目标,有什么建议吗?
提前谢谢。
void getAnswers(){ SqlDataReader dr;
SqlConnection con = new SqlConnection();
con.ConnectionString = constring;
SqlCommand Answers = new SqlCommand();
con.ConnectionString = constring;
Answers = new SqlCommand("YourStoredProcedure'" + questionID + "'", con);
try
{
con.Open();
dr = Answers.ExecuteReader();
while (dr.Read())
{
CheckBox answer = new CheckBox();
answer.Text = dr[0].ToString(); // your datarow cosist of your stored procedures return state
}
finally
{
if (dr != null)
{
dr.Close();
}
if (con != null)
{
con.Close();
}
}
}
}
在Page_Load上调用函数getAnswers();我认为这是的主要思想