说明:工程中我用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;
                }
            }
        }     }
}

解决方案 »

  1.   

    protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
        {        CheckBoxList CBL = FormView1.FindControl("CheckBoxList1") as CheckBoxList;        string[] strtemp =e.ToString().Split(',');
    }
    //-=---------------、
    str += CBL.Items[i].Value+";";
    2个符号错了..还有就是你的Checkbox不是在Formview 中吗?就在RowDataBound里边就好了.在SelectedIndexChanged中好像有问题
      

  2.   

    string += 效率可能不高,可考虑用 StringBuilder
      

  3.   

    单条数据的绑定我从来不用FormView或者DetailsView之类的控件,不用还简单点,用了反而更麻烦,特别是做里面又用到了可以进行数据绑定的控件的时候。
      

  4.   

    现在网站基本上都是用repeater 作为数据空件 很少用,其他的数据空件了
    因为网页传输速度的问题