2013-06-28 43 views
1

我通過從數據庫中爲相同用戶提取值來預填充複選框列表時出現了一些問題需要檢查多個複選框。這是一個更新配置文件頁面,我希望當這個頁面加載多個複選框時,根據數據庫檢查用戶在註冊時已經完成的檢查。所以,如何做到這一點,我想了很多次,但只有最後一個值得到檢查其他所有沒有得到檢查。單用戶可以有多種興趣愛好。所以checkboxlist是用於興趣愛好的,當更新頁面加載多個愛好時,已經檢查過,然後他可以更新和增加更多的愛好。更新部分我已經完成了,我只是希望checkboxlist預填充用戶以前給出的所有值。提前致謝。從數據庫中的值預填充複選框列表

這是我update.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Update.aspx.cs" Inherits="Update" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
    <table class="style1"> 
     <tr> 
      <td class="style3"> 
       <asp:Label ID="Label2" runat="server" Text="Login Name"></asp:Label> 
      </td> 
      <td colspan="2"> 
       <asp:TextBox ID="txtlogin" runat="server" ReadOnly="True"></asp:TextBox> 
      </td> 
      <td> 
       &nbsp;</td> 
     </tr> 
     <tr> 
      <td class="style3"> 
       <asp:Label ID="Label4" runat="server" Text="EmailId"></asp:Label> 
      </td> 
      <td colspan="2"> 
       <asp:TextBox ID="txtemail" runat="server" ReadOnly="True"></asp:TextBox> 
      </td> 
      <td> 
       <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" 
        ControlToValidate="txtemail" Display="Dynamic" ErrorMessage="Invalid" 
        ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator> 
      </td> 
     </tr> 
     <tr> 
      <td class="style3"> 
       <asp:Label ID="Label5" runat="server" Text="Address"></asp:Label> 
      </td> 
      <td colspan="2"> 
       <asp:TextBox ID="txtadd" runat="server" TextMode="MultiLine" Width="142px" 
        ReadOnly="True"></asp:TextBox> 
      </td> 
      <td> 
       &nbsp;</td> 
     </tr> 
     <tr> 
      <td class="style6"> 
       <asp:Label ID="Label6" runat="server" Text="City"></asp:Label> 
      </td> 
      <td colspan="2" class="style7"> 
       <asp:DropDownList ID="ddlcity" runat="server" Height="21px" Width="142px" 
        Enabled="False"> 
       </asp:DropDownList> 
      </td> 
      <td class="style7"> 
       &nbsp;</td> 
     </tr> 
     <tr> 
      <td class="style3"> 
       <asp:Label ID="Label7" runat="server" Text="Pincode"></asp:Label> 
      </td> 
      <td colspan="2"> 
       <asp:TextBox ID="txtpin" runat="server" ReadOnly="True" ></asp:TextBox> 
      </td> 
      <td> 
       <asp:RegularExpressionValidator ID="regexpin" runat="server" 
        ControlToValidate="txtpin" ErrorMessage="Numbers only(six digits)" 
        ValidationExpression="[0-9]{6}"></asp:RegularExpressionValidator> 
      </td> 
     </tr> 
     <tr> 
      <td class="style3"> 
       <asp:Label ID="Label8" runat="server" Text="PaymentMode"></asp:Label> 
      </td> 
      <td colspan="2"> 
       <asp:RadioButtonList ID="rbtnlstpay" runat="server" Height="28px" Width="225px" 
        Enabled="False"> 
       </asp:RadioButtonList> 
      </td> 
      <td> 
       &nbsp;</td> 
     </tr> 
     <tr> 
      <td class="style3"> 
       <asp:Label ID="Label9" runat="server" Text="Hobbies"></asp:Label> 
      </td> 
      <td colspan="2"> 
       <asp:CheckBoxList ID="cblsthobbies" runat="server" Width="128px" 
        Enabled="False"> 
       </asp:CheckBoxList> 
      </td> 
      <td> 
       &nbsp;</td> 
     </tr> 
     <tr> 
      <td class="style3"> 
       &nbsp;</td> 
      <td class="style5" colspan="2"> 
       &nbsp;</td> 
      <td> 
       &nbsp;</td> 
     </tr> 
     <tr> 
      <td class="style3"> 
       &nbsp;</td> 
      <td> 
       <asp:Button ID="btnedit" runat="server" Text="Edit" 
        onclick="btnedit_Click" /> 
      </td> 
      <td> 
       &nbsp;</td> 
      <td> 
       &nbsp;</td> 
     </tr> 
     <tr> 
      <td class="style4"> 
       &nbsp;</td> 
      <td class="style2" colspan="2"> 
       <asp:Label ID="lblresult" runat="server"></asp:Label> 
      </td> 
      <td class="style2"> 
      </td> 
     </tr> 
     <tr> 
      <td class="style4"> 
       &nbsp;</td> 
      <td class="style2" colspan="2"> 
       &nbsp;</td> 
      <td class="style2"> 
       &nbsp;</td> 
     </tr> 
     <tr> 
      <td class="style3"> 
       &nbsp;</td> 
      <td colspan="2"> 
       &nbsp;</td> 
      <td> 
       &nbsp;</td> 
     </tr> 
     <tr> 
      <td class="style3"> 
       &nbsp;</td> 
      <td colspan="2"> 
       &nbsp;</td> 
      <td> 
       &nbsp;</td> 
     </tr> 
     <tr> 
      <td class="style3"> 
       &nbsp;</td> 
      <td colspan="2"> 
       &nbsp;</td> 
      <td> 
       &nbsp;</td> 
     </tr> 
     <tr> 
      <td class="style3"> 
       &nbsp;</td> 
      <td colspan="2"> 
       &nbsp;</td> 
      <td> 
       &nbsp;</td> 
     </tr> 
     <tr> 
      <td class="style3"> 
       &nbsp;</td> 
      <td colspan="2"> 
       &nbsp;</td> 
      <td> 
       &nbsp;</td> 
     </tr> 
    </table> 
    </div> 
    </form> 
</body> 
</html> 

這是我update.aspx.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Data.SqlClient; 
using System.Data; 

public partial class Update : System.Web.UI.Page 
{ 
    DataSet ds = new DataSet(); 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!IsPostBack) 
     { 
      SqlConnection cn = new SqlConnection(); 
      cn.ConnectionString = @"Data Source=.;Initial Catalog=UserRecords;Integrated Security=True"; 

      SqlCommand cmd = new SqlCommand(); 
      cmd.Connection = cn; 
      cmd.CommandText = "select * from CityMaster;select * from HobbyMaster;select * from PaymentModeMaster;select * from Users where LoginId = @Loginid;select * from UsersHobbies where LoginId = @Loginid"; 
      cmd.CommandType = CommandType.Text; 
      cmd.Parameters.AddWithValue("@Loginid",Session["loginid"].ToString()); 
      cn.Open(); 
      SqlDataReader dr = cmd.ExecuteReader(); 

      while (dr.Read()) 
      { 
       ddlcity.Items.Add(new ListItem(dr["CityName"].ToString(), dr["CityId"].ToString())); 
      } 

      dr.NextResult(); 

      while (dr.Read()) 
      { 
       cblsthobbies.Items.Add(new ListItem(dr["Hobbyname"].ToString(), dr["HobbyId"].ToString())); 
      } 

      dr.NextResult(); 

      while (dr.Read()) 
      { 
       rbtnlstpay.Items.Add(new ListItem(dr["PaymentModename"].ToString(), dr["PaymentModeId"].ToString())); 
      } 

      dr.NextResult(); 

      while (dr.Read()) 
      { 
       txtlogin.Text = dr["LoginId"].ToString(); 
       txtemail.Text = dr["EmailId"].ToString(); 
       txtadd.Text = dr["Address"].ToString(); 
       txtpin.Text = dr["Pincode"].ToString(); 
       ddlcity.SelectedValue = dr["CityId"].ToString(); 
       rbtnlstpay.SelectedValue = dr["PaymentModeId"].ToString(); 
      } 

      dr.NextResult(); 

      while (dr.Read()) 
      { 
       for(int i = 0; i < dr.FieldCount; i++) 
       { 
        cblsthobbies.SelectedValue = dr["HobbyId"].ToString(); 
        //cblsthobbies.SelectedValue = dr.GetValue(1).ToString(); 
       } 
      } 
      cn.Close(); 
     } 
    } 

    protected void btnedit_Click(object sender, EventArgs e) 
    { 
     txtemail.ReadOnly = false; 
     txtadd.ReadOnly = false; 
     ddlcity.Enabled = true; 
     txtpin.ReadOnly = false; 
     rbtnlstpay.Enabled = true; 
     cblsthobbies.Enabled = true; 
     btnedit.Text = "Update"; 
    } 
} 
+0

上的StackOverflow的問題[ASP.NET CheckBoxList的數據綁定(http://stackoverflow.com/questions/879434/ ASP淨的CheckBoxList,綁定個問題) –

回答

2

看起來像這是你試圖設置檢查狀態的地方:

for(int i = 0; i < dr.FieldCount; i++) 
{ 
    cblsthobbies.SelectedValue = dr["HobbyId"].ToString(); 
    //cblsthobbies.SelectedValue = dr.GetValue(1).ToString(); 
} 

但是,這並不是爲CheckBoxList中的單個項目設置已檢查的狀態。這只是爲列表本身設置整體SelectedValue。而且由於只有一個這樣的值,所以循環中的每次迭代都會覆蓋前一個值。因此,只剩下最後一個。

爲了設置選中的狀態,您必須爲上一個記錄集中的每條記錄找到單獨的複選框。也許是這樣的:

for(int i = 0; i < dr.FieldCount; i++) 
{ 
    cblsthobbies.Items.FindByValue(dr["HobbyId"].ToString()).Selected = true; 
} 
0

循環遍歷cblsthobbies.Items收集和這樣設置每個項目的選定的屬性值

foreach (var i in cblsthobbies.Items) 
{ 
    i.Selected = i.SelectedValue == dr["HobbyId"].ToString(); 
} 
0

不是有兩個嵌套循環 - 一個爲你的所有可能的選擇,然後嵌套一個尋找每一個在你之前選擇的選擇 - 我想通過以前讀首先選擇選擇並將它們存儲在Hashtable中,然後只遍歷CheckBoxList中已有的所有項目,並在Hashtable中找到它。這有兩個優點:

  • 相反循環O(m * n個)倍,其中m是可能的項目的數量,n是先前選擇的數量的,它只需要循環O(M + N )乘以(或我想O(2M + N)計數的CheckBoxList建設)
  • 的代碼看起來更清晰一點沒有嵌套循環

首先,我在自主籌建的複選框列表。 aspx

  <asp:SqlDataSource runat="server" ID="dsHobbies" 
       SelectCommand="SELECT iHobbyID, sHobbyName FROM tblHobby"> 
      </asp:SqlDataSource> 
      <asp:CheckBoxList runat="server" ID="cblHobbies" DataSourceID="dsHobbies" DataValueField="iHobbyID" DataTextField="sHobbyName "></asp:CheckBoxList> 

然後代碼讀取先前保存的選擇和填充的CheckBoxList看起來是這樣的:

 // read previously chosen items from database 
     string sql = "SELECT * FROM tblSelectedHobbies WHERE [email protected]"; 
     SqlConnection conn = new SqlConnection(); 
     conn.ConnectionString = myConnStr; 
     conn.Open(); 
     SqlCommand cmd = new SqlCommand(sql, conn); 
     cmd.Parameters.AddWithValue("pPersonID", id); // id from query string, or wherever 
     SqlDataReader reader = cmd.ExecuteReader(); 

     // iterate through saved entries and add to Hashtable 
     Hashtable savedEntries = new Hashtable(); 
     while (reader.Read()) 
     { 
      string hobbyID= reader["iHobbyID"].ToString(); 
      savedEntries[hobbyID] = true; 
     } 
     conn.Close(); 

     // check the corresponding boxes 
     dsHobbies.DataBind(); 
     cblHobbies.DataBind(); 
     foreach (ListItem li in cblHobbies.Items) 
     { 
      if (savedEntries.ContainsKey(li.Value)) 
      { 
       li.Selected = true; 
      } 
     }