我尝试了下面的代码,但它不起作用:
DropDownList1.SelectedIndexChanged += new EventHandler ( doSub );
我正在尝试基于彼此的SelectedValue动态生成ddls,所以我需要以编程方式设置OnSelectedIndexChanged值,而不是像这样声明:
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DoSub">
</asp:DropDownList>
提前致谢。
提示:删除@Page部分中的AutoEventWireup =“false”以使解决方案正常工作。
答案 0 :(得分:3)
使用AddHandler附加事件处理程序。
语法:
AddHandler obj.EventName, AddressOf HandlerName
示例:
<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>
答案 1 :(得分:1)
AddHandler DropDownList1.SelectedIndexChanged, AddressOf FunctionName