#region "SaveUDCandParents"
protected void SaveUDCandParents(UDC oUDC, CheckBoxList cstUDCParent)
{
//有选项
if (cstUDCParent.Items.Count > 0) {
//'获取checkbox里面的Parents
//'判断是否有选中项
int nCount = 0;
foreach (ListItem oListItem in cstUDCParent.Items) {
if (oListItem.Selected) {
nCount = nCount + 1;
}
}
//有选项但是没有勾选
if (nCount == 0) {
if (oUDC.UDCParent != null) {
oUDC.UDCParent.Clear();
} else {
oUDC.UDCParent = new DomainList<UDCParent>();
}
Save(oUDC);
//有选项且勾选了
} else if (nCount > 0) {
if (oUDC.UDCParent != null) {
oUDC.UDCParent.Clear();
} else {
oUDC.UDCParent = new DomainList<UDCParent>();
}
foreach (ListItem oListItem in cstUDCParent.Items) {
if (oListItem.Selected) {
UDCParent oUDCParent = new UDCParent();
oUDCParent.ParentUDC = moUDCBD.Load(oListItem.Value);
oUDCParent.UDC = oUDC;
oUDC.UDCParent.Add(oUDCParent);
}
}
Save(oUDC);
}
}
}
protected void SaveUDCandParents(UDC oUDC, RadioButtonList rstUDCParent)
{
//有选项
if (rstUDCParent.Items.Count > 0) {
//'获取checkbox里面的Parents
//'判断是否有选中项
int nCount = 0;
foreach (ListItem oListItem in rstUDCParent.Items) {
if (oListItem.Selected) {
nCount = nCount + 1;
}
}
//有选项但是没有勾选
if (nCount == 0) {
if (oUDC.UDCParent != null) {
oUDC.UDCParent.Clear();
} else {
oUDC.UDCParent = new DomainList<UDCParent>();
}
Save(oUDC);
//有选项且勾选了
} else if (nCount > 0) { if (oUDC.UDCParent != null) {
oUDC.UDCParent.Clear();
} else {
oUDC.UDCParent = new DomainList<UDCParent>();
}
foreach (ListItem oListItem in rstUDCParent.Items) {
if (oListItem.Selected) {
UDCParent oUDCParent = new UDCParent();
oUDCParent.ParentUDC = moUDCBD.Load(oListItem.Value);
oUDCParent.UDC = oUDC;
oUDC.UDCParent.Add(oUDCParent);
}
}
Save(oUDC);
}
}
}
#endregion#region "BindControlList"
protected void BindControlList(CheckBoxList ctlControl, List<UDC> arrDataSource, List<UDCParent> arrParentDataSource)
{
ctlControl.DataSource = arrDataSource;
ctlControl.DataValueField = "UDCID";
ctlControl.DataTextField = "Description";
ctlControl.DataBind();
//点击编辑时 已有父类要选中                             
IList<UDCParent> arrParentUDC = arrParentDataSource;
for (int i = 0; i <= arrDataSource.Count - 1; i++) { foreach (ListItem oListItem in ctlControl.Items) {
for (int j = 0; j <= arrParentUDC.Count - 1; j++) {
if (oListItem.Value == arrParentUDC[j].ParentUDC.UDCID) {
oListItem.Selected = true;
}
}
}
}
}
protected void BindControlList(RadioButtonList ctlControl, List<UDC> arrDataSource, List<UDCParent> arrParentDataSource)
{
ctlControl.DataSource = arrDataSource;
ctlControl.DataValueField = "UDCID";
ctlControl.DataTextField = "Description";
ctlControl.DataBind();
//点击编辑时 已有父类要选中                             
IList<UDCParent> arrParentUDC = arrParentDataSource;
for (int i = 0; i <= arrDataSource.Count - 1; i++) {
foreach (ListItem oListItem in ctlControl.Items) {
for (int j = 0; j <= arrParentUDC.Count - 1; j++) {
if (oListItem.Value == arrParentUDC[j].ParentUDC.UDCID) {
oListItem.Selected = true;
}
}
}
}
}
#endregionVB.net的也行 只要能实现就行 VB的代码如下#Region "SaveUDCandParents"
        Protected Sub SaveUDCandParents(ByVal oUDC As UDC, ByVal cstUDCParent As CheckBoxList)
            If cstUDCParent.Items.Count > 0 Then '有选项
                ''获取checkbox里面的Parents
                ''判断是否有选中项
                Dim nCount As Integer = 0
                For Each oListItem As ListItem In cstUDCParent.Items
                    If oListItem.Selected Then
                        nCount = nCount + 1
                    End If
                Next
                If nCount = 0 Then   '有选项但是没有勾选
                    If oUDC.UDCParent IsNot Nothing Then
                        oUDC.UDCParent.Clear()
                    Else
                        oUDC.UDCParent = New DomainList(Of UDCParent)
                    End If
                    Save(oUDC)
                ElseIf nCount > 0 Then '有选项且勾选了
                    If oUDC.UDCParent IsNot Nothing Then
                        oUDC.UDCParent.Clear()
                    Else
                        oUDC.UDCParent = New DomainList(Of UDCParent)
                    End If
                    For Each oListItem As ListItem In cstUDCParent.Items
                        If oListItem.Selected Then
                            Dim oUDCParent As New UDCParent
                            oUDCParent.ParentUDC = moUDCBD.Load(oListItem.Value)
                            oUDCParent.UDC = oUDC
                            oUDC.UDCParent.Add(oUDCParent)
                        End If
                    Next
                    Save(oUDC)
                End If
            End If
        End Sub
        Protected Sub SaveUDCandParents(ByVal oUDC As UDC, ByVal rstUDCParent As RadioButtonList)
            If rstUDCParent.Items.Count > 0 Then '有选项
                ''获取checkbox里面的Parents
                ''判断是否有选中项
                Dim nCount As Integer = 0
                For Each oListItem As ListItem In rstUDCParent.Items
                    If oListItem.Selected Then
                        nCount = nCount + 1
                    End If
                Next
                If nCount = 0 Then   '有选项但是没有勾选
                    If oUDC.UDCParent IsNot Nothing Then
                        oUDC.UDCParent.Clear()
                    Else
                        oUDC.UDCParent = New DomainList(Of UDCParent)
                    End If
                    Save(oUDC)
                ElseIf nCount > 0 Then '有选项且勾选了                    If oUDC.UDCParent IsNot Nothing Then
                        oUDC.UDCParent.Clear()
                    Else
                        oUDC.UDCParent = New DomainList(Of UDCParent)
                    End If
                    For Each oListItem As ListItem In rstUDCParent.Items
                        If oListItem.Selected Then
                            Dim oUDCParent As New UDCParent
                            oUDCParent.ParentUDC = moUDCBD.Load(oListItem.Value)
                            oUDCParent.UDC = oUDC
                            oUDC.UDCParent.Add(oUDCParent)
                        End If
                    Next
                    Save(oUDC)
                End If
            End If
        End Sub
#End Region#Region "BindControlList"
        Protected Sub BindControlList(ByVal ctlControl As CheckBoxList, ByVal arrDataSource As List(Of UDC), ByVal arrParentDataSource As List(Of UDCParent))
            ctlControl.DataSource = arrDataSource
            ctlControl.DataValueField = "UDCID"
            ctlControl.DataTextField = "Description"
            ctlControl.DataBind()
            '点击编辑时 已有父类要选中                             
            Dim arrParentUDC As IList(Of UDCParent) = arrParentDataSource
            For i As Integer = 0 To arrDataSource.Count - 1
                For Each oListItem As ListItem In ctlControl.Items                    For j As Integer = 0 To arrParentUDC.Count - 1
                        If oListItem.Value = arrParentUDC.Item(j).ParentUDC.UDCID Then
                            oListItem.Selected = True
                        End If
                    Next
                Next
            Next
        End Sub
        Protected Sub BindControlList(ByVal ctlControl As RadioButtonList, ByVal arrDataSource As List(Of UDC), ByVal arrParentDataSource As List(Of UDCParent))
            ctlControl.DataSource = arrDataSource
            ctlControl.DataValueField = "UDCID"
            ctlControl.DataTextField = "Description"
            ctlControl.DataBind()
            '点击编辑时 已有父类要选中                             
            Dim arrParentUDC As IList(Of UDCParent) = arrParentDataSource
            For i As Integer = 0 To arrDataSource.Count - 1
                For Each oListItem As ListItem In ctlControl.Items
                    For j As Integer = 0 To arrParentUDC.Count - 1
                        If oListItem.Value = arrParentUDC.Item(j).ParentUDC.UDCID Then
                            oListItem.Selected = True
                        End If
                    Next
                Next
            Next
        End Sub
#End Region哪个语言的都行 只要能程序优化一下 ,其实我根本不懂什么程序优化 经理说我写的太罗嗦太繁杂,让我自己去优化一下,小弟真的不太会搞 请教一下

解决方案 »

  1.   

    太长了 前面两个方法可以
    传入一个控件然后判断控件的类型 来进行绑定。。后面两个没仔细看。。给你一个控件辅助类吧。。直接调用里面的方法就可以实现绑定这些。。具体的自己在学着封装一下.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.HtmlControls;
    using System.Text;
    using CommonUtilities.DataBase;
    #endregion namespace CommonUtilities.BS
    {
        /// <summary>
        /// Web控件的辅助工具类
        /// </summary>    
        public static class ControlHelper
        {
            #region 绑定DropDownList
            /// <summary>
            /// 对DropDownList控件进行数据绑定
            /// </summary>
            /// <param name="ddl">要绑定的DropDownList控件</param>
            /// <param name="sql">执行的select语句,形式为:"select Text字段,Value字段 from 表";</param>
            public static void DataBind_DDL( DropDownList ddl, string sql )
            {
                try
                {
                    //实例化数据库公共操作类
                    IDataBase db = DataFactory.CreateProvider();
                    DataTable dt = db.GetDataTable( sql );                //绑定控件
                    ddl.DataSource = dt.DefaultView;
                    ddl.DataTextField = dt.Columns[0].ToString();
                    ddl.DataValueField = dt.Columns[1].ToString();
                    ddl.DataBind();
                }
                catch ( Exception ex )
                {
                    throw ex;
                }
            }        /// <summary>
            /// 对DropDownList控件进行数据绑定
            /// </summary>
            /// <param name="ddl">要绑定的DropDownList控件</param>
            /// <param name="sql">执行的select语句,形式为:"select Text字段,Value字段 from 表";</param>
            /// <param name="text">在DropDownList控件的顶部增加一行文本</param>
            public static void DataBind_DDL( DropDownList ddl, string sql, string text )
            {
                try
                {
                    //实例化数据库公共操作类
                    IDataBase db = DataFactory.CreateProvider();
                    DataTable dt = db.GetDataTable( sql );                //在结果集上新增一行文本                
                    DataRow dr = dt.NewRow();
                    dr[0] = text;
                    dr[1] = "-1";                
                    dt.Rows.InsertAt( dr,0 );                //绑定控件
                    ddl.DataSource = dt.DefaultView;
                    ddl.DataTextField = dt.Columns[0].ToString();
                    ddl.DataValueField = dt.Columns[1].ToString();
                    ddl.DataBind();
                }
                catch ( Exception ex )
                {
                    throw ex;
                }
            }
            #endregion        #region 绑定ListBox
            /// <summary>
            /// 对ListBox控件进行数据绑定
            /// </summary>
            /// <param name="listBox">要绑定的ListBox控件</param>
            /// <param name="sql">执行的select语句,形式为:"select Text字段,Value字段 from 表";</param>
            public static void DataBind_ListBox( ListBox listBox, string sql )
            {
                try
                {
                    //实例化数据库公共操作类
                    IDataBase db = DataFactory.CreateProvider();
                    DataTable dt = db.GetDataTable( sql );                //绑定控件                
                    listBox.DataSource = dt.DefaultView;
                    listBox.DataTextField = dt.Columns[0].ToString();
                    listBox.DataValueField = dt.Columns[1].ToString();                
                    listBox.DataBind();
                }
                catch ( Exception ex )
                {
                    throw ex;
                }
            }
            #endregion        #region 为控件添加一个询问框
            /// <summary>
            /// 为控件添加一个询问框
            /// </summary>
            /// <param name="btn">按钮控件对象</param>
            /// <param name="message">要显示在询问框中的文字</param>
            public static void AddConfirmBox( Button btn, string message )
            {
                //对消息进行过滤处理
                string validMsg = StringHelper.GetValidScriptMsg( message );            StringBuilder sb = new StringBuilder();
                sb.AppendFormat( "javascript:return confirm('{0}')", validMsg );
                btn.Attributes.Add( "onclick", sb.ToString() );
            }        /// <summary>
            /// 为控件添加一个询问框
            /// </summary>
            /// <param name="btn">按钮控件对象</param>
            /// <param name="message">要显示在询问框中的文字</param>
            public static void AddConfirmBox( LinkButton btn, string message )
            {
                //对消息进行过滤处理
                string validMsg = StringHelper.GetValidScriptMsg( message );            StringBuilder sb = new StringBuilder();
                sb.AppendFormat( "javascript:return confirm('{0}')", validMsg );
                btn.Attributes.Add( "onclick", sb.ToString() );
            }
            #endregion        #region 获取第一个客户端文件控件
            /// <summary>
            /// 获取第一个客户端文件控件.
            /// 注意:Form标记必须加入属性 enctype="multipart/form-data",服务器端才能获取客户端file控件.
            /// </summary>
            public static HttpPostedFile GetFileControl()
            {
                //客户端文件控件集合
                HttpFileCollection fileControls = HttpContext.Current.Request.Files;            //返回第一个客户端文件控件
                if ( fileControls.Count == 0 )
                {
                    return null;
                }
                else
                {
                    for ( int i = 0; i < fileControls.Count; i++ )
                    {
                        //获取文件控件
                        HttpPostedFile fileControl = fileControls[i];
                        if ( fileControl.ContentLength == 0 )
                        {
                            continue;
                        }
                        else
                        {
                            return fileControls[i];
                        }
                    }
                    return null;
                }
            }
            #endregion        #region 获取客户端文件控件的集合
            /// <summary>
            /// 获取客户端文件控件的集合.
            /// 注意:Form标记必须加入属性 enctype="multipart/form-data",服务器端才能获取客户端file控件.
            /// </summary>
            public static HttpFileCollection GetFileControls()
            {
                return HttpContext.Current.Request.Files;
            }
            #endregion        #region 创建用户控件的实例
            /// <summary>
            /// 创建用户控件的实例
            /// </summary>
            /// <typeparam name="T">用户控件的类名</typeparam>
            /// <param name="ucFilePath">用户控件的相对路径,比如 @"~/UserControl/HotelArrange.ascx"</param>
            public static T CreateUC<T>( string ucFilePath ) where T : class
            {
                //获取Page对象
                Page page = HttpContext.Current.Handler as Page;            //创建实例
                return page.LoadControl( ucFilePath ) as T;
            }
            #endregion        
        }
    }
      

  2.   

    你就不应该这么去做,页面的操作用js去实现,然后把最终所需的数据 通过post 异步传递到相应的 处理页。
      

  3.   

    呵呵 关键是不让用JS 如果能用的话 我就用JS控制Checkboxlist的单复选了 根本就不用两个控件了