DropDownList如何才能输入

feafa 2010-04-16 02:33:12
问题2
假入重写控件 如何级联

求教
...全文
107 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
皇城龙三 2010-04-16
  • 打赏
  • 举报
回复
ASP.NET中如何让DropDownList控件既能做选择又能做输入用(转载)
<html>  
<head>
<style>
<!--
.cls1 { position:absolute; left:250px; top:89px; width:216px; height:72px; z-index:1; }
.cls2 { position:absolute; left:250px; top:89px; width:95px; height:18px; z-index:2 }
input { font-size: 12px; padding-top: 2px; padding-left: 2px;width:127;}
//-->
</style>
</head>
<script>
<!--
function addOption(pos){

if(event.keyCode==13)
{
var select_obj = document.getElementById("myselect");
var text_value = document.getElementById("test").value;

if (select_obj.length)
{
for (var i=0;i<select_obj.length;i++)
{
// 如果已经存在,不添加,直接退出
if (select_obj.options[i].text==text_value)
alert("选项已存在,请重新输入");
}
}
var the_option= new Option(text_value,text_value);

select_obj.add(the_option);

select_obj.selectedIndex = select_obj.options.length-1
}

}
//-->
</script>
<div class="cls1" style="clip: rect(3 280 21 110)">
<select name="myselect" style="width:127" onchange="document.getElementById('test').value=this.value">
<option value=1>1</option>
<option value=2>2</option>
<option value=3>3</option>
</select>
</div>
<div class="cls2">
<input type="text" name="test" onkeydown="addOption()" size="20" style="width: 127; height: 23">
</div>

</body>
</html>
qqhahazxxqq 2010-04-16
  • 打赏
  • 举报
回复
首先新建一个类库项目,在项目新建一个新建web自定义控件,将上述内容放入到这个CS文件中,然后生成,将生成的debug文件中的DLL文件拖动到工具栏中。这样就可以直接使用了。也可以在自己的项目中新建一个CS文件将上述的代码复制到里面去,然后编译生成,在工具箱中就有这个控件。

注意:

在网页中使用这个控件时,如果网页中使用的是IE6的话,该控件不能实现可输入的功能,可以在IE7.0以上的版本可以正常使用。这也可能是应为我使用的网页标准的原因。我采用的

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">标准

如果换成Html4.0就可以正常使用

//下面是他的cs文件的内容:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Text;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Collections;



namespace Newsease.Common.Web.UI.WebControls

{

/**/

/// <summary>

/// DropDownList 的摘要说明。

/// </summary>

[ToolboxData("<{0}:DropDownListExtend runat=\"server\" />")]

public class DropDownListExtend : System.Web.UI.WebControls.TextBox

{

private Hashtable _values;

public DropDownList _DropDownList;



public DropDownListExtend()

{

_values = new Hashtable();

_DropDownList = new DropDownList();

}



public Hashtable Values

{

get

{

return _values;

}

set

{

_values = value;

}

}



/**/

/// <summary>

/// 将此控件呈现给指定的输出参数。

/// </summary>

/// <param name="output"> 要写出到的HTML 编写器</param>

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 + 20;

int spanWidth = sWidth - 18;



output.Write("<div style=\"POSITION:relative; WIDTH:122px;top:-13px;height:8px\">");

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.Attributes.Add("onchange", "this.parentNode.nextSibling.value=this.options[this.selectedIndex].text");



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);

}

}



if (_DropDownList.Items.Count == 1)

{

ListItem item = new ListItem();

item.Value = "";

item.Text = " ";

_DropDownList.Items.Add(item);

_DropDownList.SelectedIndex = 1;

}



_DropDownList.RenderControl(output);



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



base.Style.Clear();

base.Width = Unit.Parse(iWidth.ToString() + "px");

base.Style.Add("POSITION", "absolute");

base.Style.Add("left", "0px");

base.Render(output);



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

}

}

}
migercai 2010-04-16
  • 打赏
  • 举报
回复
你都重写了 还没有办法级联么
你写个规则就好了

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧