Javascript获取DropDownList选项信息
这是一个asp.net普通javascript的练习,是使用javascript去获取DropDownList Selected 选项的信息,如text,value和index等。
Insus.NET在.aspx.cs内准备好一些数据,是有关浏览器相关信息的,并写在泛型Dictionary<T,V>:

private Dictionary<string, string> GetBrowser() { Dictionary<string, string> dict = new Dictionary<string, string>(); dict.Add("google", "chrome"); dict.Add("Mozilla", "firefox"); dict.Add("microsoft", "ie"); dict.Add("opera", "opera"); dict.Add("safari", "apple"); return dict; }
在网页中,拉(PULL)DropDownList控件入网页.aspx中。

<asp:DropDownList ID="DropDownList1" runat="server"> </asp:DropDownList>
去.aspx.cs为DropDownList控件绑定数据:
写Javascript脚本:

function GetSelectedInfo() { var ddl = document.getElementById('<% =DropDownList1.ClientID %>'); var info = " The value is: " + ddl.options[ddl.selectedIndex].value + ".\n\r text is: " + ddl.options[ddl.selectedIndex].text + ".\n\r option index is: " + ddl.selectedIndex; alert(info); }
为了能执行上面的javascript脚本,Insus.NET在网页拉一个Button,然后让这个Button 执行一个OnClientClick客户端事件。
<asp:Button ID="Button1" runat="server" Text="Get info" OnClientClick="GetSelectedInfo();" />
标签:
Alert
, JavaScript
, Button
, js
, Dictionary
, options
, getElementById
, SelectedIndex
, OnClientClick
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
2011-05-28 Gridview前面10行数据显示背景色
2011-05-28 为用户控件(UserControl)写属性
2011-05-28 Reset Image Size 2