根据下拉列表中选择的数据填充radiobuttonlist上的数据

时间:2012-11-13 07:23:02

标签: sql drop-down-menu radiobuttonlist populate

过去两天我一直遇到以下问题。

我希望创建一个民意调查,用户将从下拉列表中选择民意调查。选择后,所选轮询将根据其ID从数据库中获取数据,并使用3个选项填充单选按钮列表:日期1,日期2,日期3。

我有一个数据库表,存储ID,到期数据,日期1,日期2,创建投票的日期3。

我能够生成创建的民意调查的下拉列表,但是当我选择使用不同的日期选项填充辐射按钮列表时,我无法使所选的民意调查响应。以下是我编写的代码片段:

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>

如果可以提供任何帮助,我将非常感激!!

谢谢!

1 个答案:

答案 0 :(得分:0)

更改selectSQL =“SELECT firstDate,secondDate,thirdDate FROM tblPolls”;查询selectSQL =“SELECT firstDate opdate FROM tblPolls union all SELECT secondDate FROM tblPolls union all SELECT thirdDate FROM tblPolls“ 并将此DataTextField =“firstDate,secondDate,thirdDate”DataValueField =“id”更改为 DataTextField =“opdate”DataValueField =“opdate”

我认为上面的slectSQL会有一些条件。希望这会对你有所帮助