二级DropDownList控件源码

2024-04-07 17:48

本文主要是介绍二级DropDownList控件源码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

二级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

这篇关于二级DropDownList控件源码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/883261

相关文章

8种快速易用的Python Matplotlib数据可视化方法汇总(附源码)

《8种快速易用的PythonMatplotlib数据可视化方法汇总(附源码)》你是否曾经面对一堆复杂的数据,却不知道如何让它们变得直观易懂?别慌,Python的Matplotlib库是你数据可视化的... 目录引言1. 折线图(Line Plot)——趋势分析2. 柱状图(Bar Chart)——对比分析3

WinForms中主要控件的详细使用教程

《WinForms中主要控件的详细使用教程》WinForms(WindowsForms)是Microsoft提供的用于构建Windows桌面应用程序的框架,它提供了丰富的控件集合,可以满足各种UI设计... 目录一、基础控件1. Button (按钮)2. Label (标签)3. TextBox (文本框

Android实现一键录屏功能(附源码)

《Android实现一键录屏功能(附源码)》在Android5.0及以上版本,系统提供了MediaProjectionAPI,允许应用在用户授权下录制屏幕内容并输出到视频文件,所以本文将基于此实现一个... 目录一、项目介绍二、相关技术与原理三、系统权限与用户授权四、项目架构与流程五、环境配置与依赖六、完整

Android实现定时任务的几种方式汇总(附源码)

《Android实现定时任务的几种方式汇总(附源码)》在Android应用中,定时任务(ScheduledTask)的需求几乎无处不在:从定时刷新数据、定时备份、定时推送通知,到夜间静默下载、循环执行... 目录一、项目介绍1. 背景与意义二、相关基础知识与系统约束三、方案一:Handler.postDel

Java 正则表达式URL 匹配与源码全解析

《Java正则表达式URL匹配与源码全解析》在Web应用开发中,我们经常需要对URL进行格式验证,今天我们结合Java的Pattern和Matcher类,深入理解正则表达式在实际应用中... 目录1.正则表达式分解:2. 添加域名匹配 (2)3. 添加路径和查询参数匹配 (3) 4. 最终优化版本5.设计思

Qt中QGroupBox控件的实现

《Qt中QGroupBox控件的实现》QGroupBox是Qt框架中一个非常有用的控件,它主要用于组织和管理一组相关的控件,本文主要介绍了Qt中QGroupBox控件的实现,具有一定的参考价值,感兴趣... 目录引言一、基本属性二、常用方法2.1 构造函数 2.2 设置标题2.3 设置复选框模式2.4 是否

Qt中QUndoView控件的具体使用

《Qt中QUndoView控件的具体使用》QUndoView是Qt框架中用于可视化显示QUndoStack内容的控件,本文主要介绍了Qt中QUndoView控件的具体使用,具有一定的参考价值,感兴趣的... 目录引言一、QUndoView 的用途二、工作原理三、 如何与 QUnDOStack 配合使用四、自

Java调用C++动态库超详细步骤讲解(附源码)

《Java调用C++动态库超详细步骤讲解(附源码)》C语言因其高效和接近硬件的特性,时常会被用在性能要求较高或者需要直接操作硬件的场合,:本文主要介绍Java调用C++动态库的相关资料,文中通过代... 目录一、直接调用C++库第一步:动态库生成(vs2017+qt5.12.10)第二步:Java调用C++

Python实现无痛修改第三方库源码的方法详解

《Python实现无痛修改第三方库源码的方法详解》很多时候,我们下载的第三方库是不会有需求不满足的情况,但也有极少的情况,第三方库没有兼顾到需求,本文将介绍几个修改源码的操作,大家可以根据需求进行选择... 目录需求不符合模拟示例 1. 修改源文件2. 继承修改3. 猴子补丁4. 追踪局部变量需求不符合很

Spring 中 BeanFactoryPostProcessor 的作用和示例源码分析

《Spring中BeanFactoryPostProcessor的作用和示例源码分析》Spring的BeanFactoryPostProcessor是容器初始化的扩展接口,允许在Bean实例化前... 目录一、概览1. 核心定位2. 核心功能详解3. 关键特性二、Spring 内置的 BeanFactory