2017-01-10 161 views
0

我有幾個複選框的界面。 我希望檢查數據庫中是否有相關值
這是接口我有
Interface如何使用數據庫中的數據將值綁定到複選框?

首先對複選框從不同的表加載和盒子內的其他人應該使用另一臺被加載。
如果你可以請幫我做到這一點。

此表用於第一4個複選框
1st checkboxes

這是較低的複選框
lower checkboxes

我已創建的查詢和數據獲取到的數據集的表。
這也是迄今爲止

  List<int> list = new List<int>(); 
      foreach (DataRow dr in ds.Tables[0].Rows) 
      { 
       list.Add(Convert.ToInt32(dr["Qulif_code"])); 

      } 
      int[] arr = list.ToArray(); 

      for (int i = 0; i < arr.Length; i++) 
      { 
       if (arr[i] == 1) 
       { 
        ck_ol.Checked==true ; 
       } 

      } 
+0

您試過的代碼在哪裏?並可以請詳細說明,因爲它很難弄清楚什麼條件下的值將檢查複選框被檢查什麼他們之間的映射等等。 – Manish

+0

@Manish我已經添加了我到目前爲止所做的代碼 – Mike

+0

隨着複選框的類型,數據庫中的數據應該是布爾值。通常它會自動檢查值是否爲true。 – ThinhLe

回答

1

我已經解決了這個問題,這裏是我的代碼所做的代碼。

  List<int> list = new List<int>(); 
      foreach (DataRow dr in ds.Tables[0].Rows) 
      { 
       list.Add(Convert.ToInt32(dr["Qulif_code"])); 

      } 
      int[] arr = list.ToArray(); 

      for (int i = 0; i < arr.Length; i++) 
      { 
       if (arr[i] == 1) 
       { 
        ck_ol.Checked = true; 
       } 
       else if (arr[i] == 2) 
       { 
        ck_al.Checked = true; 
       } 
       else if (arr[i] == 3) 
       { 
        ck_dip.Checked = true; 
       } 
       else if (arr[i] == 4) 
       { 
        ck_deg.Checked = true; 
       } 

      }   

我希望這可能有助於未來某個人。

2

數據庫


  • 我已經使用了以下表嗜好與模式作爲 如下。啓用已禁用的GridView列CheckBoxField字段在ASP.Net

    enter image description here

  • 我已經在表中插入一些記錄在ASP.Net

enter image description here

啓用GridView中禁用 CheckBoxField字段列
  • HTML標記

下面是ASP.net網頁的HTML標記。

正如你所看到的,我添加了一個CheckBoxList和一個Button,它允許用戶更新數據庫中的選擇。

<asp:CheckBoxList ID="chkHobbies" runat="server"> 
</asp:CheckBoxList> 
<br /> 
<asp:Button ID="btnUpdate" runat="server" Text="Button" OnClick = "UpdateHobbies" /> 

下面的屏幕截圖說明如何在用戶界面看起來

enter image description here

從數據庫


下面的方法被用於填充嗜好的CheckBoxList填充的CheckBoxList來自SQL Server數據庫 C#

private void PopulateHobbies() 
{ 
    using (SqlConnection conn = new SqlConnection()) 
    { 
     conn.ConnectionString = ConfigurationManager 
       .ConnectionStrings["constr"].ConnectionString; 
     using (SqlCommand cmd = new SqlCommand()) 
     { 
      cmd.CommandText = "select * from hobbies"; 
      cmd.Connection = conn; 
      conn.Open(); 
      using (SqlDataReader sdr = cmd.ExecuteReader()) 
      { 
       while (sdr.Read()) 
       { 
        ListItem item = new ListItem(); 
        item.Text = sdr["Hobby"].ToString(); 
        item.Value = sdr["HobbyId"].ToString(); 
        item.Selected = Convert.ToBoolean(sdr["IsSelected"]); 
        chkHobbies.Items.Add(item); 
       } 
      } 
      conn.Close(); 
     } 
    } 
} 

上述方法的以下面的方式被調出在頁面加載事件: -

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!IsPostBack) 
    { 
     this.PopulateHobbies(); 
    } 
} 

保存選擇在數據庫


下面的方法被調用時提交按鈕的Click事件並用於用戶的選擇保存到數據庫

protected void UpdateHobbies(object sender, EventArgs e) 
{ 
    using (SqlConnection conn = new SqlConnection()) 
    { 
     conn.ConnectionString = ConfigurationManager 
       .ConnectionStrings["constr"].ConnectionString; 
     using (SqlCommand cmd = new SqlCommand()) 
     { 
      cmd.CommandText = "update hobbies set IsSelected = @IsSelected" + 
           " where [email protected]"; 
      cmd.Connection = conn; 
      conn.Open(); 
      foreach (ListItem item in chkHobbies.Items) 
      { 
       cmd.Parameters.Clear(); 
       cmd.Parameters.AddWithValue("@IsSelected", item.Selected); 
       cmd.Parameters.AddWithValue("@HobbyId", item.Value); 
       cmd.ExecuteNonQuery(); 
      } 
      conn.Close(); 
     } 
    } 
} 

當程序首次 When program runs for the first time

運行

更新複選框中的值後。

enter image description here

相關問題