2012-11-13 27 views
1

過去兩天我遇到以下問題。基於在下拉列表中選擇的數據填充單選按鈕列表中的數據

我希望創建一個用戶將從下拉列表中選擇輪詢的輪詢。選擇後,選定的民意調查將從數據庫中獲取數據,根據其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> 

我真的很感激它,如果有任何幫助可以提供!

謝謝!

回答

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會有一些條件。希望這會幫助你

+0

感謝您的幫助。我仍然無法將收音機列表按鈕與我的數據庫綁定。還有其他建議嗎? – user1820134

+0

現在你面臨什麼問題?你可以提到上面實現後發現的錯誤或行爲嗎? –

相關問題