强制asp.net的DropDownList扩大 [英] Force asp.net dropdownlist to expand

查看:112
本文介绍了强制asp.net的DropDownList扩大的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.NET的数据绑定的DropDownList这是基于一个文本框的内容填充。它填充后,我想自动展开的DropDownList,让用户意识到选择需要进行,不需要点击下拉将其展开。似乎没有成为一个属性或方法不做到这一点。

编辑:尝试埃德B的例子之后,我仍然坚持。我DDL的ID是ctl00_ContentPlaceHolder9_ddlContact。如果我把下面的一个按钮的onclick事件,它工作正常,下拉很好的扩展:

 的document.getElementById('ctl00_ContentPlaceHolder9_ddlContact')大小= 10;

然而,在DDL的数据绑定事件以下code显示了警报,但没有展开下拉:

 字符串脚本=< SCRIPT LANGUAGE =JavaScript的'>中;
    脚本+ =警报('扩大');的document.getElementById('ctl00_ContentPlaceHolder9_ddlContact')大小= 10 LT; / SCRIPT>中;
    ClientScript.RegisterClientScriptBlock(的GetType(),下拉,脚本);


解决方案

摘要::你不能展开香草下拉列表。更多信息请参见本次讨论:<一href=\"http://stackoverflow.com/questions/360431/can-i-open-a-dropdownlist-using-jquery\">http://stackoverflow.com/questions/360431/can-i-open-a-dropdownlist-using-jquery.然而,也有一些解决方法是可以接受的。

一种方法(虽然有点厚脸皮)是有下拉列表中展开一次以显示更多的内容。默认情况下, 元素显示在同一时间只有一个列表项,但您可以使用它的尺寸属性,把它显示一比在同一时间。通过这种方法,你可以调整的尺寸属性时,在DDL用户鼠标(模拟扩展它),然后恢复到1的尺寸时,鼠标关闭(将其返回到正常的DDL)。下面是一个例子:

 &LT; ASP:DropDownList的=服务器ID =ddlColors
                  的onmouseover =this.size = 3;
                  的onmouseout =this.size = 1&GT;
    &LT; ASP:ListItem的&GT;红色和LT; / ASP:ListItem的&GT;
    &LT; ASP:ListItem的&GT;绿色&LT; / ASP:ListItem的&GT;
    &LT; ASP:ListItem的&GT;蓝色&LT; / ASP:ListItem的&GT;
&LT; / ASP:DropDownList的&GT;

另一种选择是使用JavaScript来创建一个伪选择。总之,您可以使用脚本和DOM操作和CSS的组合来获得能满足你需求的用户界面。

快乐编程!

I have an ASP.NET data bound dropdownlist which is populated based on the contents of a textbox. After it is populated I would like to expand the dropdownlist automatically, so that the user realizes that a choice needs to be made and doesn't need to click on the dropdown to expand it. There doesn't seem to be a property or method do do this.

EDIT: After trying out Ed B's example, I am still stuck. The id of my ddl is 'ctl00_ContentPlaceHolder9_ddlContact'. If I put the following in the onclick event of a button, it works fine, the dropdown expands nicely:

    document.getElementById('ctl00_ContentPlaceHolder9_ddlContact').size=10;

However, the following code in the Databound event of the ddl shows the alert but doesn't expand the dropdown:

string script = "<SCRIPT LANGUAGE='JavaScript'> ";
    script += "alert('expanding');document.getElementById('ctl00_ContentPlaceHolder9_ddlContact').size=10 </SCRIPT>";
    ClientScript.RegisterClientScriptBlock(GetType(), "Dropdown", script);

解决方案

Summary: You can't expand a vanilla drop-down list. See this discussion for more information: http://stackoverflow.com/questions/360431/can-i-open-a-dropdownlist-using-jquery. However, there are some workarounds that may be acceptable.

One approach (albeit a bit cheeky) is to have the drop-down list expand to show more items at once. By default, a element shows just one list item at a time, but you can use its size attribute to have it show more than one at a time. With this approach you could adjust the size attribute when the user mouses over the DDL (to simulate expanding it) and then revert back to a size of 1 when they mouse off (to return it to a "normal" DDL). Here is an example:

<asp:DropDownList runat="server" ID="ddlColors" 
                  onmouseover="this.size=3;" 
                  onmouseout="this.size=1">
    <asp:ListItem>Red</asp:ListItem>
    <asp:ListItem>Green</asp:ListItem>
    <asp:ListItem>Blue</asp:ListItem>
</asp:DropDownList>

Another option is to use JavaScript to create a pseudo-select. In short, you use a combination of script and DOM manipulation and CSS to get a user interface that meets your requirements.

Happy Programming!

这篇关于强制asp.net的DropDownList扩大的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆