• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

画情画心画影

--寒冬玉
  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

可输入下拉框(完美版),asp.net

这个控件和别人不同有以下两点:
 1,一般的dropdownlist都会默认选中第一条,这样的话,在选择第一条的时候就没有事件,而往往这时候我们需要触发ongchange事件,于是我在它的foucus事件中强行改变它的selectedIndex。
2,输入的内容如果和下拉框中的某项相同时,会自动选中下拉框中那一条。

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

namespace ExtendWebControls
{

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

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


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


  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 = key;
     item.Text = _values[key].ToString();

     _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 = "\n";
   strScript += "var isExist = -2;\n";
   strScript += "var obj = event.srcElement;\n";
   strScript += "var str = this.parentNode.nextSibling.value;\n";
   strScript += "var ary = obj.options;\n";
   strScript += "for(var i=0;i<ary.length;i++){\n";
   strScript += " if(str == ary[i].text){\n";
   strScript += "  isExist = i;\n";
   strScript += "  break;\n";
   strScript += " }\n";
   strScript += "}\n";
   strScript += "if(isExist != -2){\n";
   strScript += " obj.selectedIndex = isExist;\n";
   strScript += "}\n";
   strScript += "else{\n";
   strScript += " obj.selectedIndex = -1;\n";
   strScript += "}\n";
    
   return strScript;
  }
 }
}

 

建一个webcontrol libery 工程,用上面的代码生成一个dll
然后在使用该控件的工程里面导入dll,之后的事情就和使用textBox等
服务器控件一样了

posted on 2008-08-21 14:17  Winter001  阅读(424)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3