在过去的 2 天里,我一直遇到以下问题。
我希望创建一个民意调查,用户将从下拉列表中选择一个民意调查。选择后,所选投票将根据其 ID 从数据库中获取数据,并使用 3 个选项填充单选按钮列表:日期 1、日期 2、日期 3。
我有一个数据库表,用于存储创建的投票的 ID、到期数据、日期 1、日期 2、日期 3。
I am able to generate the drop down list of polls created, however i am unable to make the selected poll respond when selected to fill the radi button list with the different date options. 以下是我编写的代码片段:
protected void Page_Load(object sender, EventArgs e)
{
fillPollOptions();
if(pollDropDownList.SelectedIndex > 0){
MultiView1.Visible = true;
btnListPollOptions.Visible = true;
pollDateCreated();
}
}
public void fillPollOptions()
{
pollDropDownList.Items.Clear();
string selectSQL = "SELECT id, dateExpired FROM tblPolls";
SqlConnection con = new SqlConnection(strConnString);
SqlCommand cmd = new SqlCommand(selectSQL, con);
SqlDataReader reader;
try
{
con.Open();
reader = cmd.ExecuteReader();
while (reader.Read())
{
ListItem newItem = new ListItem();
newItem.Text = reader["id"].ToString();
newItem.Value = reader["dateExpired"].ToString();
pollDropDownList.Items.Add(newItem);
}
reader.Close();
}
catch (Exception err)
{
lblListError.Text = "Error reading list of names. ";
lblListError.Text += err.Message;
}
finally
{
con.Close();
}
}
public void pollDateCreated()
{
string selectSQL = "SELECT dateExpired, dateCreated FROM tblPolls";
SqlConnection con = new SqlConnection(strConnString);
SqlCommand cmd = new SqlCommand(selectSQL, con);
SqlDataReader reader;
try
{
con.Open();
reader = cmd.ExecuteReader();
reader.Read();
lblQuestionDateCreated.Text = reader["dateCreated"].ToString();
lblQuestionDateExpires.Text = reader["dateExpired"].ToString();
reader.Close();
}
catch (Exception err)
{
lblListError.Text = "Error reading list of names. ";
lblListError.Text += err.Message;
}
finally
{
con.Close();
}
}
protected void btnPollList()
{
string selectSQL;
selectSQL = "SELECT firstDate,secondDate,thirdDate FROM tblPolls ";
SqlConnection con = new SqlConnection(strConnString);
SqlCommand cmd = new SqlCommand(selectSQL, con);
SqlDataReader reader;
try
{
con.Open();
reader = cmd.ExecuteReader();
reader.Read();
btnListPollOptions.DataSource = reader;
btnListPollOptions.DataBind();
reader.Close();
}
catch(Exception ex){
lblMessage.Text = "Error displaying poll";
}
}
我不确定前端是否需要任何数据源绑定。我的 .asp 代码是:
<asp:DropDownList ID="pollDropDownList" runat="server" Height="16px"
Width="132px" >
</asp:DropDownList>
<asp:RadioButtonList ID="btnListPollOptions" runat="server"
DataTextField="firstDate, secondDate, thirdDate" DataValueField="id">
<asp:ListItem>Not Available</asp:ListItem>
</asp:RadioButtonList>
如果能提供任何帮助,我将不胜感激!!
谢谢!