简体   繁体   中英

Pre-select checkBox from database fields in asp.net using c#

I have a group checkbook list which i want to preselect from the database table. I amusing following code to for the selection of this but it is not working

Data in table is store in following format example:-

1,2,
1,5,7,
1,2,3,4,

HTML

<asp:checkboxlist id="chkBoxDaysList" runat="server">
 <asp:listitem  runat="server" value="1"  Text="Sunday" />
 <asp:listitem  runat="server" value="2" Text="Monday" />
 <asp:listitem  runat="server" value="3" Text="Tuesday" />
 <asp:listitem  runat="server" value="4" Text="Wednesday" />
 <asp:listitem  runat="server" value="5" Text="Thrusday" />
 <asp:listitem  runat="server" value="6" Text="Friday" />
 <asp:listitem  runat="server" value="7" Text="Saturday" />
</asp:checkboxlist>

C# Code to preselect checkbox based on data saved previously

public void getSelectedDays() { IDataReader dr;

String strSqlDays = "SELECT * FROM EventCalender WHERE rowID = 6";
DataSet ds = new DataSet();
ds = DataProvider.Connect_Select(strSqlDays);
dr = ds.Tables[0].CreateDataReader();

string[] s = new string[50];
while (dr.Read())
{
    s = dr["EventDays"].ToString().Split(',');

}
int length = s.Length;
for (int i = 0; i <= s.Length - 1; i++)
{
    string cntry = s[i];
    for (int j = 0; j <= chkBoxDaysList.Items.Count - 1; j++)
    {
        if (chkBoxDaysList.Items[j].Text == s[i])
        {
            chkBoxDaysList.Items[j].Selected = true;
            break;
        }
    }
}

}

Right now code doesn't generate any error but doesn't select any checkbox also

You are checking text of checkbox with value from DB. Please check value of the checkboxlist with value from DB record.Corrected code is shown below-->

 if (chkBoxDaysList.Items[j].Value == s[i])
        {
            chkBoxDaysList.Items[j].Selected = true;
            break;
        }
protected void Button2_Click(object sender, EventArgs e)
    {
        try
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from empreg where empid=@empid", con);
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.AddWithValue("@empid", txtid.Text);
            SqlDataAdapter adpt = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            adpt.Fill(ds);
            if (ds.Tables[0].Rows.Count > 0)
            {
                IDataReader dr = ds.Tables[0].CreateDataReader();
                txtfname.Text = ds.Tables[0].Rows[0][1].ToString();
                txtlname.Text = ds.Tables[0].Rows[0][2].ToString();
                RadioButtonList1.SelectedValue = ds.Tables[0].Rows[0][3].ToString();
                txtdob.Text = ds.Tables[0].Rows[0][4].ToString();
                txtdoj.Text = ds.Tables[0].Rows[0][5].ToString();
                txtsal.Text = ds.Tables[0].Rows[0][6].ToString();
                txtadd.Text = ds.Tables[0].Rows[0][7].ToString();
                DropDownList1.SelectedItem.Text = ds.Tables[0].Rows[0][8].ToString();
                //checkbox1
                string[] b = new string[50];
                while (dr.Read())
                {
                    b = dr["Dept"].ToString().Split(',');
                }

                for (int i = 0; i <= b.Length - 1; i++)
                {
                    foreach (ListItem item in this.CheckBoxList1.Items)
                        if (item.Value == b[i])
                        {
                            item.Selected = true;
                            i++;
                        }
                }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM