using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page 
...{
protected void Page_Load(object sender, EventArgs e)
...{
if (!Page.IsPostBack)
...{
this.BindDataCb();
BindDataGV();
}
}

//绑定多选框#region//绑定多选框
public void BindDataCb()
...{
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=TeacherTest;Integrated Security=True");
SqlCommand objSqlCommand = new SqlCommand("Select * from Love", con);
con.Open();
DataSet ds = new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = objSqlCommand;
adapter.Fill(ds);
con.Close();
this.cb_Love.DataSource = ds;
this.cb_Love.DataTextField = "Love_name";
this.cb_Love.DataValueField = "Love_value";
this.cb_Love.DataBind();
}
#endregion

//绑定GridView#region//绑定GridView
public void BindDataGV()
...{
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=TeacherTest;Integrated Security=True");
SqlCommand objSqlCommand = new SqlCommand("Select * from MoreCheck", con);
con.Open();
DataSet ds = new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = objSqlCommand;
adapter.Fill(ds);
con.Close();
this.GridView1.DataSource = ds;
this.GridView1.DataBind();
}
#endregion

//插入数据#region//插入数据
protected void Button1_Click(object sender, EventArgs e)
...{
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=TeacherTest;Integrated Security=True");
SqlCommand objSqlCommand = new SqlCommand("insert into MoreCheck (name,age,love)values('"+this.txtName.Text+"','"+this.txtAge.Text+"',"+this.calculate(this.cb_Love)+")", con);
con.Open();
objSqlCommand.ExecuteNonQuery();
con.Close();
BindDataGV();
}
#endregion

/计算选择的结果的值#region/计算选择的结果的值
public int calculate(CheckBoxList cb)
...{
int resultFir = 0;//此因子为以下每项选择完后进行“或”运算后得到的结果
int resultSec = 0;//同上
int resultCount = 0; //计数因子
foreach (ListItem everItem in cb.Items)
...{
if (everItem.Selected)
...{
if (resultCount == 0)
...{
resultFir = int.Parse(everItem.Value);
}
else
...{
resultSec = int.Parse(everItem.Value);
resultFir = resultFir | resultSec;
}
}
resultCount++;
}
return resultFir;
}
#endregion

//单击选择,把选择的该行的值赋给文本框#region //单击选择,把选择的该行的值赋给文本框
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
...{
string id = this.GridView1.SelectedRow.Cells[0].Text;
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=TeacherTest;Integrated Security=True");
SqlCommand objSqlCommand = new SqlCommand("Select * from MoreCheck where id="+id, con);
con.Open();
DataSet ds = new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = objSqlCommand;
adapter.Fill(ds);
con.Close();
//数据已经取到DataSet中
DataView dv= ds.Tables[0].DefaultView;
this.txtAge.Text = dv[0]["age"].ToString();
this.txtName.Text = dv[0]["name"].ToString();
this.SetCheckBoxListValue(cb_Love,Convert.ToInt32(dv[0]["love"].ToString()));
}
#endregion

//是否打勾#region //是否打勾
public void SetCheckBoxListValue(CheckBoxList cb, int iValue)
...{
int iItemValue = -1;
foreach (ListItem everItem in cb.Items)
...{
iItemValue = int.Parse(everItem.Value);
everItem.Selected = (iValue & iItemValue) > 0;
}
}
#endregion
}
这篇博客演示了如何在ASP.NET中使用数据库实现多选功能。通过SqlConnection连接数据库,使用DataSet和SqlDataAdapter从TeacherTest数据库的Love和MoreCheck表中获取数据。数据绑定到CheckBoxList(cb_Love)和GridView(GridView1),并实现了当选中CheckBoxList中的项时,将值计算并插入到MoreCheck表中。此外,当GridView选中某行时,会将对应的值赋给文本框。
867

被折叠的 条评论
为什么被折叠?



