【问题标题】:Populating data on radiobuttonlist based on data selected in dropdownlist根据下拉列表中选择的数据在单选按钮列表上填充数据
【发布时间】:2012-11-01 15:43:23
【问题描述】:

过去 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.以下是我编写的代码的 sn-ps:

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>

如果能提供任何帮助,我将不胜感激!

谢谢!

【问题讨论】:

    标签: sql drop-down-menu radiobuttonlist populate


    【解决方案1】:

    改变你的 selectSQL = "SELECT firstDate,secondDate,thirdDate FROM tblPolls ";查询 selectSQL = "SELECT firstDate opdate FROM tblPolls union all SELECT secondDate FROM tblPolls union all 从 tblPolls 中选择第三个日期” 并将此 DataTextField="firstDate, secondDate,thirdDate" DataValueField="id" 更改为 DataTextField="opdate" DataValueField="opdate"

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

    【讨论】:

    • 感谢您的帮助。我仍然无法将单选列表按钮与我的数据库绑定。还有其他建议吗?
    • 现在你面临什么问题?您能否提及上述实施后发现的错误或行为
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-23
    • 1970-01-01
    • 2012-09-11
    • 2015-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多