用户操作
[即时聊天] [发私信] [加为好友]
aaronbaiID:aaronbai
61176次访问,排名1731好友5人,关注者9
aaronbai的文章
原创 76 篇
翻译 0 篇
转载 18 篇
评论 18 篇
最近评论
gaoweijin:我 ok了
zhangkai08111:不ok啊。。
zhangkai08111:不ok啊。。
itzoey:懂了
感谢
longhua3311:我是在my.ini文件里改的
文章分类
收藏
    相册
    好友BLOG
    Aaronbai的猫窝
    学习交流
    存档
    软件项目交易
    订阅我的博客
    XML聚合  FeedSky
    订阅到鲜果
    订阅到Google
    订阅到抓虾
    订阅到BlogLines
    订阅到Yahoo
    订阅到GouGou
    订阅到飞鸽
    订阅到Rojo
    订阅到newsgator
    订阅到netvibes

    原创 使用自定义控件创建可编辑下拉框收藏

    新一篇: SQL Server数据库的表中不能输入汉字的问题 | 旧一篇: ASP.NET中WEB用户控件和自定义控件

     

    目的:创建可编辑(可输入)下拉框

    步骤一:在VS2005中 新建WEB 控件库 WebControlLibrary1 

    步骤二:建立自定义控件类DropDownListExtend

     

    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.ComponentModel;
    using System.Collections;

    namespace WebControlLibrary1
    {

        [ToolboxData(
    "<{0}:DropDownListExtend runat="server" />")]
        
    public class DropDownListExtend : System.Web.UI.WebControls.TextBox
        
    {
            
    private Hashtable _values;
            
    private DropDownList _DropDownList;
           

            
    public Hashtable Values
            
    {
                
    get return _values; }
                
    set { _values = value; }
            }




            
    public DropDownListExtend()
            
    {
                _values 
    = new Hashtable();
                _DropDownList 
    = new DropDownList();
            }





            
    protected override void Render(HtmlTextWriter output)
            
    {

                
    int iWidth = Convert.ToInt32(base.Width.Value);
                
    if (iWidth == 0)
                
    {
                    iWidth 
    = 102;
                    
    base.Width = Unit.Parse("102px");
                }


                
    int sWidth = iWidth + 16;
                
    int spanWidth = sWidth - 18;

                output.Write(
    "<div style="POSITION:relative">");
                output.Write(
    "<span style="MARGIN-LEFT:" + spanWidth.ToString() + "px;OVERFLOW:hidden;WIDTH:18px">");

                _DropDownList.Width 
    = Unit.Parse(sWidth.ToString() + "px");
                _DropDownList.Style.Add(
    "MARGIN-LEFT""-" + spanWidth.ToString() + "px");
                _DropDownList.ID 
    = base.ID + "_Select";
                _DropDownList.Attributes.Add(
    "onchange""this.parentNode.nextSibling.value=this.value");
                _DropDownList.Attributes.Add(
    "onfocus""" + this.getFocusScript() + "");

                
    if (_values.Count > 0)
                
    {
                    
    foreach (string key in _values.Keys)
                    
    {
                        ListItem item 
    = new ListItem();

                        item.Value 
    = _values[key].ToString();
                        item.Text 
    = key;

                        _DropDownList.Items.Add(item);
                    }

                }

                _DropDownList.RenderControl(output);

                output.Write(
    "</span>");

                
    base.Style.Clear();
                
    base.Width = Unit.Parse(iWidth.ToString() + "px");
                
    base.Style.Add("left""0px");
                
    base.Style.Add("POSITION""absolute");
                
    base.Render(output);

                output.Write(
    "</div>");
            }


            
    private string getFocusScript()
            
    {
                
    string strScript = " ";
                strScript 
    += "var isExist = -2; ";
                strScript 
    += "var obj = event.srcElement; ";
                strScript 
    += "var str = this.parentNode.nextSibling.value; ";
                strScript 
    += "var ary = obj.options; ";
                strScript 
    += "for(var i=0;i<ary.length;i++){ ";
                strScript 
    += " if(str == ary[i].text){ ";
                strScript 
    += "  isExist = i; ";
                strScript 
    += "  break; ";
                strScript 
    += " } ";
                strScript 
    += "} ";
                strScript 
    += "if(isExist != -2){ ";
                strScript 
    += " obj.selectedIndex = isExist; ";
                strScript 
    += "} ";
                strScript 
    += "else{ ";
                strScript 
    += " obj.selectedIndex = -1; ";
                strScript 
    += "} ";

                
    return strScript;
            }

        }

    }


     步骤三: 编译工程,生成跟WEB控件库工程名字一样的DLL,如WebControlLibrary1.dll

    步骤四: 在需要使用下拉框的工程添加应用(就是在其BIN目录里添加WebControlLibrary1.dll)

    步骤五:具体应用:

                页面代码如下:

     

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="MasterPage.aspx.cs" Inherits="Module_DataManage_MasterPage" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        
    <title>无标题页</title>
    </head>
    <body>
        
    <form id="form1" runat="server">
        
    <div>
        
             
    <EditDDLPreFix:DropDownListExtend ID="EditableDDL" runat="server" Text="text" Width="93px"></EditDDLPreFix:DropDownListExtend>     
          
        
    </form>
    </body>
    </html>

     

    CS文件如下:

     

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    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;

    public partial class Module_DataManage_MasterPage : System.Web.UI.Page

    {
        
    public Hashtable values;
        
    protected void Page_Load(object sender, EventArgs e)
        
    {
            values 
    = new Hashtable();
            values.Add(
    "a""a");
            values.Add(
    "b""b");
            values.Add(
    "c""c");
            
    this.EditableDDL.Values=values;
        }


        
    public Hashtable Values {

            
    get return values; }
            
    set { values = value; }
        }


       

    }

     

     

    发表于 @ 2007年10月13日 16:49:00|评论(loading...)|编辑

    新一篇: SQL Server数据库的表中不能输入汉字的问题 | 旧一篇: ASP.NET中WEB用户控件和自定义控件

    评论

    #aaronbai 发表于2007-10-15 08:59:43  IP: 219.149.11.*
    一个带有可输入下拉框的HTML页面代码:
    <html>
    <head>
    <title>可输入的下拉框</title>
    </head>
    <body>
    <div style="position:relative;">
    <span style="margin-left:100px;width:18px;overflow:hidden;">
    <select style="width:118px;margin-left:-100px" onchange="this.parentNode.nextSibling.value=this.value">
    <option value="1">选择项1</option>
    <option value="2/">选择项2</option>
    <option value="3">选择项3</option>
    </select>
    </span>
    <input name="box" style="width:100px;position:absolute;left:0px;">
    </div>
    </body>
    </html>
    发表评论  


    当前用户设置只有注册用户才能发表评论。如果你没有登录,请点击登录
    Csdn Blog version 3.1a
    Copyright © aaronbai