从数据库中获取后绑定在checkboxlist中的项目 [英] Bind item in checkboxlist after fetching from database

查看:88
本文介绍了从数据库中获取后绑定在checkboxlist中的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



我正在使用下拉列表和checkboxlist控件。当我从下拉列表中选择任何项目时,应该分别从数据库中选择复选框列表项目。



请帮帮我。



谢谢。

Hello,

I am using a dropdown list and checkboxlist control. when i select any item from dropdownlist, checkboxlist items should be shown selected respectively from the database.

Please help me.

Thanks.

推荐答案

从DataBase中获取值并在CheckBoxList中显示所选值





Fetching the Value from the DataBase and Showing the Selected Value in CheckBoxList


protected void btnGetRecor_Click(object sender, EventArgs e)
 {
 try
 {
 getCountry();
 getSelectedCountry();
 }
 catch (Exception ex)
 {
 lblerror.Text = ex.Message;
 }
 }
 public void getSelectedCountry()
 {
 IDataReader dr;
 string[] s = new string[50];
 SqlConnection con = new SqlConnection();
 con.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=D:Fech the value from Database and show in selectedtCheckedListBox\\multicheckboxvalue\\App_Data\\Database.mdf;Integrated Security=True;User Instance=True";
 SqlCommand com = new SqlCommand();
 com.Connection = con;
 com.CommandText = "FetchCountry";
 com.CommandType = CommandType.StoredProcedure;
 con.Open();
 dr= com.ExecuteReader();
 while (dr.Read())
 {
 s = dr["countryname"].ToString().Split(',');
 
}
 int length = s.Length;
 for (int i = 0; i <= s.Length - 1; i++)
 {
 string cntry = s[i];
 for (int j = 0; j <= CheckBoxList2.Items.Count - 1; j++)
 {
 if (CheckBoxList2.Items[j].Text == s[i])
 {
 CheckBoxList2.Items[j].Selected = true;
 break;
 }
 }
 }
 }


嘿那里,



这是我填写组合框的代码,我怀疑它对于一个复选框列表应该是相同的:)



hey there,

this is my code from filling a combobox, i suspect it should be the same for a checkboxlist :)

/////////// Load combo boxes with car index/////////////////////


         string connectionString1 = "Provider= ...etc"

            string sqlstring1 = "select distinct (car_index) from sinter_car  where status = 'Active' order by car_index";

           OleDbConnection connection1 = new OleDbConnection(connectionString1);

            OleDbCommand command1 = new OleDbCommand(sqlstring1,Connection1);

            try
            {

             connection1.Open();

                OleDbDataReader reader1 = command1.ExecuteReader();

                while

                    (reader1.Read())
                {
                    comboBox1.Items.Add(reader1["car_index"]);
                                   
                }

                reader1.Close();            }

            catch (Exception)
            {
                MessageBox.Show("An exception error has occured!", "Exception Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }

           connection1.Close();


public void BindChecklist()

{

CheckBoxList1.DataSource = DB.ReviewCategories.Where(p => p.Deleted == true);

CheckBoxList1.DataTextField =" CategoryName" ;;

CheckBoxList1.DataValueField =" ID" ;;

CheckBoxList1.DataBind();

}



foreach(CheckBoxList1.Items中的ListItem li)

{

if(li.Selected)

{

PlaceUserReviewCategory catobj = new PlaceUserReviewCategory();

catobj.UserID =((User)Session [" USER"])。ID;

catobj.PlaceID = PlaceID;

catobj.ReviewCategoryID =转换。 ToInt32(li.Value);



}



}

< br $>




Kishor makwana

insightsoftech
public void BindChecklist()
{
CheckBoxList1.DataSource = DB.ReviewCategories.Where(p => p.Deleted == true);
CheckBoxList1.DataTextField = "CategoryName";
CheckBoxList1.DataValueField = "ID";
CheckBoxList1.DataBind();
}

foreach (ListItem li in CheckBoxList1.Items)
{
if (li.Selected)
{
PlaceUserReviewCategory catobj = new PlaceUserReviewCategory();
catobj.UserID = ((User)Session["USER"]).ID;
catobj.PlaceID = PlaceID;
catobj.ReviewCategoryID = Convert.ToInt32(li.Value);

}

}



Kishor makwana
insightsoftech


这篇关于从数据库中获取后绑定在checkboxlist中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆