CSDN博客

img tiaohh

二级DropDownList控件源码

发表于2004/10/18 16:58:00  1428人阅读

二级DropDownList控件源码

将源码放于此,不过感觉并不是很满意。
有一个小问题,就是获取小类的值,因为控件中加入了一个功能,就是如果在初始时带入小类的值时,控件会自动将小类相应的选项选中,并带动大类的选项,这样就一个冲突:获取小类的值与初始小类的值带来的影响。

又开始忙了,再琢磨一下后修改。

源码如下:
using System.Text;
using System.Globalization;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.UI.Design;
using System;
using System.Drawing.Design;
using System.Drawing;
using System.Data;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections.Specialized;


namespace Flyangel.Component.WebUI
{
    
/// <summary>
    
/// AutoSelectDropList 的摘要说明。
    
/// </summary>

    [ToolboxData("<{0}:AutoSelectDropList runat=server></{0}:AutoSelectDropList>")]
    
public class AutoSelectDropList : Control, INamingContainer,IPostBackEventHandler,IPostBackDataHandler
    
{
        DropDownList _childdropdownlist 
= new DropDownList();
        DropDownList _fatherdropdownlist 
= new DropDownList();
        HtmlInputHidden _hideinput 
= new HtmlInputHidden();

        
#region 属性

        
数据源设置

        
所属FORM的ID名称

        
ChildID设置,用来进行子栏目的选择,并反向控制父栏的选择

        
数据字段设置

        


        
#endregion



        
public void RenderAtDesignTime()
        
{
            
if (!Page.IsPostBack)
            
{
                CreateChildControls();
            }

        }


        
protected override void CreateChildControls()
        
{

            Controls.Clear();

            _hideinput.ID 
= "SCvalue";
            _hideinput.Value 
= "0";
            Controls.Add(_hideinput);

            _fatherdropdownlist.ID 
= "fatherid";
            _fatherdropdownlist.Attributes.Add(
"onChange","changelocation(document." + _formname + "." + this.ClientID + "_fatherid.options[document." + _formname + "." + this.ClientID + "_fatherid.selectedIndex].value)");
            ListItem _newli 
= new ListItem();
            _newli.Text 
= "一级栏目";
            _newli.Value 
= "0";
            _fatherdropdownlist.Items.Add(_newli);

            
if (SetSelectedChildID != "0")
            
{
                GetFatherIDFormChildID();
            }


            
int _tempfatheridstr = 0;
            
for(int i = 0; i<(DataSource.Tables[0].Rows.Count);i++)
            
{
                ListItem _newlii 
= new ListItem();
                _newlii.Value 
= DataSource.Tables[0].Rows[i][_fatherfieldid].ToString();
                _newlii.Text 
= DataSource.Tables[0].Rows[i][_fatherfieldname].ToString();
                _fatherdropdownlist.Items.Add(_newlii);
                
if (SetSelectedFatherID == DataSource.Tables[0].Rows[i][_fatherfieldid].ToString())
                
{
                    _tempfatheridstr 
= i+1;
                }

            }

            Controls.Add(_fatherdropdownlist);
            _fatherdropdownlist.SelectedIndex 
= _tempfatheridstr;

            _childdropdownlist.ID 
= "childid";
            _childdropdownlist.Attributes.Add(
"onChange","changechild(document." + _formname + "." + this.ClientID + "_childid.options[document." + _formname + "." + this.ClientID + "_childid.selectedIndex].value)");

            
int _tempchildidstr = 0;
            
if (SetSelectedChildID != "0")
            
{
                DataView _temdv 
= new DataView(DataSource.Tables[1]);
                
string _filterstr = _parentid + " = " + SetSelectedFatherID;
                _temdv.RowFilter 
= _filterstr;

                
for (int jj=0;jj<_temdv.Count;jj++)
                
{
                    ListItem _newlijai 
= new ListItem();
                    _newlijai.Value 
= _temdv[jj][_childfieldid].ToString();
                    _newlijai.Text 
= _temdv[jj][_childfieldname].ToString();
                    _childdropdownlist.Items.Add(_newlijai);
                    
if (SetSelectedChildID == _temdv[jj][_childfieldid].ToString())
                    
{
                        _tempchildidstr 
= jj;
                    }

                }

            }

            
else
            
{
                ListItem _newliji 
= new ListItem();
                _newliji.Text 
= "二级栏目";
                _newliji.Value 
= "0";
                _childdropdownlist.Items.Add(_newliji);
            }

            Controls.Add(_childdropdownlist);
            _childdropdownlist.SelectedIndex 
= _tempchildidstr;

            AddClientScript();
        }


        
private void GetFatherIDFormChildID()
        
{
            
for(int j = 0; j<(DataSource.Tables[1].Rows.Count);j++)
            
{
                
if (SetSelectedChildID == DataSource.Tables[1].Rows[j][_childfieldid].ToString())
                
{
                    SetSelectedFatherID 
= DataSource.Tables[1].Rows[j][_parentid].ToString();
                }

            }

        }

        
#region IPostBackEventHandler Implementation

        
/// <summary>
        
/// 实现<see cref="IPostBackEventHandler"/> 接口,使控件能够处理将窗体发送到服务器时引发的事件。
        
/// </summary>
        
/// <param name="args"></param>

        public void RaisePostBackEvent(string args)
        
{
        }



        
#endregion


        
IPostBackDataHandler Implementation
        
PageChanged Event

        
OnPageChanged Method

        
private void AddClientScript()
        
{
            
if(!Page.IsClientScriptBlockRegistered("clientScript"))
            
{
                StringBuilder stringScript 
= new StringBuilder() ;
                stringScript.Append(
"<!-- 设计:flash1313699@hotmail.com   QQ:3337002 --> ");
                stringScript.Append(
"<script language="javascript"> ");
                stringScript.Append(
"var onecount; ");
                stringScript.Append(
"subcat = new Array(); ");

                
int _temi = DataSource.Tables[0].Rows.Count;
                
for(int i = 0; i<_temi;i++)
                
{
                    stringScript.Append(
"subcat[" + i + "] = new Array("二级栏目","" + DataSource.Tables[0].Rows[i][_fatherfieldid].ToString() + "","0"); ");
                }

                
int _temj = DataSource.Tables[1].Rows.Count;
                
for(int j = 0; j<_temj;j++)
                
{
                    stringScript.Append(
"subcat[" + (_temi + j) + "] = new Array("" + DataSource.Tables[1].Rows[j][_childfieldname].ToString() + "","" + DataSource.Tables[1].Rows[j][_parentid].ToString() + "","" + DataSource.Tables[1].Rows[j][_childfieldid].ToString() + ""); ");
                }

                stringScript.Append(
"subcat[" + (_temj + _temi) + "] = new Array("二级栏目","0","0"); ");
                stringScript.Append(
"onecount=" + (_temj + _temi + 1+ " ");
                stringScript.Append(
" ");
                stringScript.Append(
"function changelocation(locationid) ");
                stringScript.Append(
"{ ");
                stringScript.Append(
"  document." + _formname + "." + this.ClientID + "_SCvalue.value = '0';  ");
                stringScript.Append(
"  document." + _formname + "." + this.ClientID + "_childid.length = 0;  ");
                stringScript.Append(
"  var locationid=locationid; ");
                stringScript.Append(
"  var i; ");
                stringScript.Append(
"  for (i=0;i < onecount; i++) ");
                stringScript.Append(
"  { ");
                stringScript.Append(
"    if (subcat[i][1] == locationid) ");
                stringScript.Append(
"    { ");
                stringScript.Append(
"      document." + _formname + "." + this.ClientID + "_childid.options[document." + _formname + "." + this.ClientID + "_childid.length] = new Option(subcat[i][0], subcat[i][2]); ");
                stringScript.Append(
"    } ");
                stringScript.Append(
"  } ");
                stringScript.Append(
"} ");

                stringScript.Append(
"function changechild(str) ");
                stringScript.Append(
"{ ");
                stringScript.Append(
"  document." + _formname + "." + this.ClientID + "_SCvalue.value = str;  ");
                stringScript.Append(
"} ");

                stringScript.Append(
"</SCRIPT> ");
                stringScript.Append(
"<!-- 设计:flash1313699@hotmail.com   QQ:3337002 --> ");
                Page.RegisterClientScriptBlock(
"clientScript", stringScript.ToString());
            }

        }

    }

}

posted on 2004-09-24 00:20 flyangel 阅读(390) 评论(2)  编辑 收藏

评论

# re: 二级DropDownList控件源码

代码比较长~~:)
不过是C#的。支持一下
用Js是不是更短或者说方便一些
2004-09-25 00:50 | DEV

# re: 二级DropDownList控件源码

:),其实WEB控件中很多是无法离开JS的。换句话说,WEB本身是无法离开JS的。

其实只是无聊的一种做法。为了让自己方便,而不是去实现一个大而全的万能公式。
2004-09-25 00:59 | flyangel
阅读全文
0 0

相关文章推荐

img
取 消
img