[英]Programmatically set OnSelectedIndexChanged for a DropDownList in VB.NET
I have tried below code but it didn't work: 我已经尝试了下面的代码,但是没有用:
DropDownList1.SelectedIndexChanged += new EventHandler ( doSub );
I'm trying to make ddls dynamically based on SelectedValue of each other, so I need to set the OnSelectedIndexChanged values programmatically instead of declarativly like this: 我试图基于彼此的SelectedValue动态地使ddls动态变化,所以我需要以编程方式设置OnSelectedIndexChanged值,而不是像这样声明:
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DoSub">
</asp:DropDownList>
Thanks in advance. 提前致谢。
Tip: Remove the AutoEventWireup="false" in @Page section to get solution to work. 提示:删除@Page部分中的AutoEventWireup =“ false”以获取解决方案。
Use AddHandler to attach an event handler. 使用AddHandler附加事件处理程序。
Syntax: 句法:
AddHandler obj.EventName, AddressOf HandlerName
Example: 例:
<script runat="server">
Protected Sub Page_Load(sender As Object, e As System.EventArgs)
AddHandler DropDownList1.SelectedIndexChanged, AddressOf DoSub
End Sub
Sub DoSub(sender As Object, e As System.EventArgs)
Response.Write(DropDownList1.SelectedValue)
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">
<asp:ListItem>Foo</asp:ListItem>
<asp:ListItem>Bar</asp:ListItem>
</asp:DropDownList>
</div>
</form>
</body>
AddHandler DropDownList1.SelectedIndexChanged, AddressOf FunctionName
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.