说明:工程中我用FormView做数据插入、修改、删除,其中经常用到CheckBoxList,绑定数据只能取得ListItem中的第一个选定项。
现打算CheckBoxList绑定后插入数据库时自动获取所有选定项的值(以“;”相隔)写入相应表的char列值。查询和修改时自动检索相应值绑定CheckBoxList中CheckBox。
我的想法:本人是新手,业余自学,没有电脑基础,当时的想法是在FormView1_ItemInserting中直接修改绑定值,将所选项Value值相加,再插入数据库,这一步可见下贴:
http://topic.csdn.net/u/20090524/18/da97971c-d00d-49d5-ae8e-7ee3a4843d87.html
但还需要查询时Item模板、Edit模板的代码。做来做去发现CheckBoxList实在太多了点,一个个写太累。写函数我也很陌生。所以求代码严谨的专业高手提供解决方案。要求:1.复用性高,代码简洁,本人菜鸟,请附带说明。
2.解决方案需要方法复用,如何用请详细说明。
3.是否已经有成熟的解决方案推荐?还是象我这样的菜鸟解决思路比较差,方向不对?请顺便帮我看下我的代码的解决思路是不是特别笨?环境:VS2008+.NET3.5+SQL SERVER2005我的代码页:.aspx:
上面一个GirdView,检索出此公司所有安全培训内容(简单列表,其中CompanyID用的是Porfile存储)
选择详细可在下面的FormView中列出详细内容。可添加、删除、修改。(其中经常有CheckBoxList控件)
具体代码比较多,如果用得着可以问我要。
.cs:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
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.Xml.Linq;public partial class Company_SafeTrain : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ } //用来刷新GirdView的数据绑定。 还有别的方法吗?这个方法很可笑吧? protected void FormView1_ItemInserted(object sender, FormViewInsertedEventArgs e)
{
Response.Redirect("SafeTrain.aspx");
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{ //判断是否是DataRow,以防止鼠标经过Header也有效果
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor; this.style.backgroundColor='#ebebeb';");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c;");
} } //检索所以CheckBoxList中的选项值: protected void FormView1_ItemInserting(object sender, FormViewInsertEventArgs e)
{
string str = ""; CheckBoxList CBL = FormView1.FindControl("CheckBoxList1") as CheckBoxList; if (CBL != null)
{
for (int i = 0; i < CBL.Items.Count; i++)
{
if (CBL.Items[i].Selected)
{ str += CBL.Items[i].Value+";";
}
}
} e.Values["TrainContent"] = str; } //CheckBoxList在FormView控件Item模板的呈现。 protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
{ CheckBoxList CBL = FormView1.FindControl("CheckBoxList1") as CheckBoxList; string[] strtemp =e.ToString().Split(','); foreach (string str in strtemp)
{
for (int i = 0; i < CBL.Items.Count; i++)
{
if (CBL.Items[i].Value == str)
{
CBL.Items[i].Selected = true;
}
}
} }
}
现打算CheckBoxList绑定后插入数据库时自动获取所有选定项的值(以“;”相隔)写入相应表的char列值。查询和修改时自动检索相应值绑定CheckBoxList中CheckBox。
我的想法:本人是新手,业余自学,没有电脑基础,当时的想法是在FormView1_ItemInserting中直接修改绑定值,将所选项Value值相加,再插入数据库,这一步可见下贴:
http://topic.csdn.net/u/20090524/18/da97971c-d00d-49d5-ae8e-7ee3a4843d87.html
但还需要查询时Item模板、Edit模板的代码。做来做去发现CheckBoxList实在太多了点,一个个写太累。写函数我也很陌生。所以求代码严谨的专业高手提供解决方案。要求:1.复用性高,代码简洁,本人菜鸟,请附带说明。
2.解决方案需要方法复用,如何用请详细说明。
3.是否已经有成熟的解决方案推荐?还是象我这样的菜鸟解决思路比较差,方向不对?请顺便帮我看下我的代码的解决思路是不是特别笨?环境:VS2008+.NET3.5+SQL SERVER2005我的代码页:.aspx:
上面一个GirdView,检索出此公司所有安全培训内容(简单列表,其中CompanyID用的是Porfile存储)
选择详细可在下面的FormView中列出详细内容。可添加、删除、修改。(其中经常有CheckBoxList控件)
具体代码比较多,如果用得着可以问我要。
.cs:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
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.Xml.Linq;public partial class Company_SafeTrain : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ } //用来刷新GirdView的数据绑定。 还有别的方法吗?这个方法很可笑吧? protected void FormView1_ItemInserted(object sender, FormViewInsertedEventArgs e)
{
Response.Redirect("SafeTrain.aspx");
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{ //判断是否是DataRow,以防止鼠标经过Header也有效果
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor; this.style.backgroundColor='#ebebeb';");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c;");
} } //检索所以CheckBoxList中的选项值: protected void FormView1_ItemInserting(object sender, FormViewInsertEventArgs e)
{
string str = ""; CheckBoxList CBL = FormView1.FindControl("CheckBoxList1") as CheckBoxList; if (CBL != null)
{
for (int i = 0; i < CBL.Items.Count; i++)
{
if (CBL.Items[i].Selected)
{ str += CBL.Items[i].Value+";";
}
}
} e.Values["TrainContent"] = str; } //CheckBoxList在FormView控件Item模板的呈现。 protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
{ CheckBoxList CBL = FormView1.FindControl("CheckBoxList1") as CheckBoxList; string[] strtemp =e.ToString().Split(','); foreach (string str in strtemp)
{
for (int i = 0; i < CBL.Items.Count; i++)
{
if (CBL.Items[i].Value == str)
{
CBL.Items[i].Selected = true;
}
}
} }
}
解决方案 »
- iis配置问题——用vs2008在三层架构模式下写的网站!
- WebChart1控件问题、
- .net迅雷下载的时候把网页下载下来了,没有把资料下载下来
- sql语名空白补齐的问题
- windows 2003 X64 下注册vb编译的dll,提示成功,调用时报“不能创建对象”,将dll在该操作系统上重写用vb编译后,注册(注:使用syswow64文件夹下regsvr32)后也不行
- 如何用键盘控制Gridview 的行选择.
- 用户控件由一个个LinkButton组成,而且有子菜单,在页面上时占的空间太大怎么办?
- 请教在SQL中如何提取日期中的年月日部分和时分秒部分
- Web中UI界面设计工具
- 替好朋友,高分求救简单问题。
- 【新手疑问】在读取数据库的时候,是用控件自带的读取好,还是自己写个读取好?
- 如何使用javascript,获取到FileUpload中的文件名
{ CheckBoxList CBL = FormView1.FindControl("CheckBoxList1") as CheckBoxList; string[] strtemp =e.ToString().Split(',');
}
//-=---------------、
str += CBL.Items[i].Value+";";
2个符号错了..还有就是你的Checkbox不是在Formview 中吗?就在RowDataBound里边就好了.在SelectedIndexChanged中好像有问题
因为网页传输速度的问题