我尝试开发一个基于Web的调查创建和填充ASP.NET和C#上的应用程序从Sql Server获取数据。我使用 DataBind 来绑定我的问题名称和答案。在图片下方,我需要在RadioButtonList中显示仅问题编号(如1,2,3等)(不包含问题名称)和仅显示问题名称在标签lblQuestion as;
protected void rbtnQuestions_SelectedIndexChanged(object sender, EventArgs e)
{
General.myquestionid = System.Convert.ToInt32(rbtnQuestions.SelectedValue);
lblQuestion.Text = rbtnQuestions.SelectedItem.Text;
}
我还需要" id" survey_question表显示问题下面的答案,因为我在 SelectedIndexChanged 事件中使用它。
但是由于我使用 DataTextField , RadioButtonList文本和 lblQuestion.Text 都显示相同的内容。我的数据绑定方式如下:
string sqlStr = "SELECT id, question_no, question, survey_answer_type_id FROM survey_question WHERE survey_id = '" + General.mysurveyid + "'";
cmd = new SqlCommand(sqlStr, connection);
da.SelectCommand = cmd;
da.Fill(data);
rbtnQuestions.DataSource = data;
rbtnQuestions.DataTextField = "question";
rbtnQuestions.DataValueField = "id";
rbtnQuestions.DataBind();
cmd.Dispose();
我怎样才能做到这一点? 谢谢。
答案 0 :(得分:1)
在页面加载事件集
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string sqlStr = "SELECT id,question_no, CONVERT(varchar(10),(ROW_NUMBER() over (order by question desc))) AS QueNo,question, survey_answer_type_id FROM survey_question WHERE survey_id = '" + General.mysurveyid + "'";
cmd = new SqlCommand(sqlStr, connection);
da.SelectCommand = cmd;
da.Fill(data);
ViewState["data"] = data;
rbtnQuestions.DataSource = data;
rbtnQuestions.DataTextField = "QueNo";
rbtnQuestions.DataValueField = "id";
rbtnQuestions.DataBind();
cmd.Dispose();
}
}
在SelectedIndexChanged事件上设置问题
protected void rbtnQuestions_SelectedIndexChanged(object sender, EventArgs e)
{
DataSet ds = (DataSet)ViewState["data"];
DataRow[] row = ds.Tables[0].Select("id = '" + rbtnQuestions.SelectedValue + "'");
lblQuestion.Text = row[0]["question"].ToString();
}