本文介绍了我对asp.net checkboxlist有疑问。未插入复选框选定项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对asp.net checkboxlist有疑问。复选框选中项目未插入.....这是我的代码...
I have doubt in asp.net checkboxlist. the checkbox selected item is not inserted..... this is my code...
protected void Page_Load(object sender, System.EventArgs e)
{
string query;
using (SqlConnection con = new SqlConnection(CS))
{
try
{
query="SELECT distinct[username] FROM [Login]";
con.Open();
SqlDataAdapter da=new SqlDataAdapter(query,con);
DataSet ds=new DataSet();
da.Fill(ds);
if(ds.Tables[0].Rows.Count!=0)
{
CheckBoxList1.DataSource=ds;
CheckBoxList1.DataTextField="username";
CheckBoxList1.DataValueField="username";
CheckBoxList1.DataBind();
}
else
{
Response.Write("No Results found");
}
}
catch(Exception ex)
{
Response.Write("<br>"+ex);
}
finally
{
// con.Close();
}
}
}
protected void send_Click(object sender, EventArgs e)
{
foreach(ListItem item in CheckBoxList1.SelectedValue.ToString())
//foreach (ListItem item in CheckBoxList1.Items)
{
if (item.Selected)
{
using (SqlConnection con = new SqlConnection(CS))
{
con.Open();
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "INSERT INTO Calendar (schdlby, schdldate, schdltime, event, parti ) values (@schdlby, @schdldate, @schdltime, @event, @parti)";
cmd.Parameters.AddWithValue("@schdlby", Page.User.Identity.Name.ToString());
cmd.Parameters.AddWithValue("@schdldate", cl.SelectedDate.ToShortDateString());
cmd.Parameters.AddWithValue("@schdltime", schdltime.Text.Trim());
cmd.Parameters.AddWithValue("@event", txtevent.Text);
cmd.Parameters.AddWithValue("@parti", item.Value.ToString());
// cmd.Parameters.AddWithValue("@parti", item.Selected.ToString());
cmd.ExecuteNonQuery();
}
}
}
}
// Response.Redirect("Second.aspx");
}
推荐答案
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindCheckBoxList();
}
}
private void BindCheckBoxList()
{
using (SqlConnection con = new SqlConnection(CS))
using (SqlCommand command = new SqlCommand("SELECT DISTINCT [username] FROM [Login]", con))
{
SqlDataAdapter da = new SqlDataAdapter(command);
DataSet ds = new DataSet();
da.Fill(ds);
CheckBoxList1.DataSource = ds;
CheckBoxList1.DataTextField = "username";
CheckBoxList1.DataValueField = "username";
CheckBoxList1.DataBind();
}
}
这篇关于我对asp.net checkboxlist有疑问。未插入复选框选定项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!