2016-08-01 30 views
0

我目前正在使用Visual Studio 2015.現在,我可以使用SqlConnection連接到數據庫並在GridView中顯示數據。此代碼位於結果頁面中。使用單選按鈕和單選按鈕列表創建一個帶有條件的SQL查詢ASP.NET

private void BindGrid() 
    { 
     string strConnString = ConfigurationManager.ConnectionStrings["SQL"].ConnectionString; 
     using (SqlConnection con = new SqlConnection(strConnString)) 
     { 
      if (Interface_trial_1.RadioButtonList1.Checked == true) 
      { 
       using (SqlCommand cmd = new SqlCommand("SELECT * FROM [dbo].[Emp]")) 
      { 
        using (SqlDataAdapter sda = new SqlDataAdapter()) 
        { 
         cmd.Connection = con; 
         sda.SelectCommand = cmd; 
         using (DataTable dt = new DataTable()) 
         { 
          sda.Fill(dt); 
          GridView1.DataSource = dt; 
          GridView1.DataBind(); 
         } 
        } 
       } 
      } 
     } 
    } 

但是,我不知道如何使用單選按鈕和索引頁面中的單選按鈕列表中的條件運行查詢。另外,我不知道如何將單選按鈕或單選按鈕列表的值從索引頁面傳輸到結果頁面。

如果我要在索引頁中運行查詢並在結果頁中顯示數據或從索引頁傳輸值並在結果頁中運行查詢,會更好嗎?

請指教。我需要指導。提前致謝!

回答

0

您可以使用各種ASP.NET state management techniques傳給你可以使用查詢字符串來收集指數頁面上的RadioButtonRadioButtonList輸入的數據,並通過它傳遞給結果頁pages.For例子之間的數據。

索引頁:

protected void btnOrder_Click(object sender, EventArgs e) 
{ 
    bool isHungry = rdbHungry.Checked; 
    string menuItem = rdbMenu.SelectedItem.Text; 

    string url = String.Format("Result.aspx?isHungry={0}&menuItem={1}", isHungry, menuItem); 
    Response.Redirect(url); 
} 

結果頁面:

protected void Page_Load(object sender, EventArgs e) 
{ 
    bool isHungry = Boolean.Parse(Request.QueryString["isHungry"]); 
    string menuItem = Request.QueryString["menuItem"].ToString(); 
    //Write your SQL query here and make sure you check the parameters to prevent SQL injection attacks 
} 
+0

用戶必須選擇按鈕和我有1子鍵約6個按鈕。那麼如何讓代碼知道哪個按鈕被選中並將其轉移到Result頁面? – Brandon

+0

使用命令按鈕 - https://msdn.microsoft.com/zh-cn/library/system.web.ui.webcontrols.button.command(v=vs.110).aspx。指定每個按鈕的CommandName屬性,並分配相同的OnCommand事件到所有按鈕 - >然後在OnCommand事件評估e.CommandName屬性,找出哪個按鈕被點擊 –

+0

您發送給我的鏈接顯示「很抱歉,您請求的頁面無法找到」 – Brandon