我ShowValue
正在工作。现在,在最后一行,我需要调用函数newFunction
来重新填充dropdownlist
事件上的onClick
对象。
单击dropdownlist
向下箭头时出现错误。
如何让两个人互相握手?
我的代码:
Public Sub ShowValue(ByVal sender As Object, ByVal e As System.EventArgs)
lblupdatePanel.Text = DropDownList1.SelectedValue.ToString
Dim LocationDescription2 As DropDownList = CType(dvContact.FindControl("LocationDescription2"), DropDownList)
Dim LocationLogic As New LocationBLL
LocationDescription2.DataSource = LocationLogic.GetUnitByUnitID(DropDownList1.SelectedValue.ToString)
LocationDescription2.DataTextField = "LocationDescription"
LocationDescription2.DataValueField = "LocationCode"
LocationDescription2.SelectedValue = DropDownList1.SelectedValue.ToString
LocationDescription2.DataBind()
LocationDescription2.Attributes.Add("onclick", "newFunction(what to put here?);")
End Sub
Public Sub newFunction(ByVal sender As Object, ByVal e As System.EventArgs)
Dim LocationDescription2 As DropDownList = CType(dvContact.FindControl("LocationDescription2"), DropDownList)
Dim DivisionDescription1 As DropDownList = CType(dvContact.FindControl("DivisionDescription1"), DropDownList)
Dim LocationLogic As New LocationBLL
LocationDescription2.DataSource = LocationLogic.GetLocationByDivisionCode(DivisionDescription1.SelectedValue.ToString)
LocationDescription2.DataTextField = "LocationDescription"
LocationDescription2.DataValueField = "LocationCode"
LocationDescription2.DataBind()
End Sub
答案 0 :(得分:2)
LocationDescription2.Attributes.Add("onclick", "newFunction(what to put here?);"
添加一个客户端javascript事件处理程序,它不能调用VB.NET代码隐藏函数。您需要在VB.NET中连接事件
因此它将触发回发,此时ASP.NET可以调用代码隐藏事件处理程序。
最好在Page_Load中定义事件处理程序。
答案 1 :(得分:1)
您调用的Attributes.Add
方法仅影响控件的呈现的html 。渲染的HTML只能直接调用javascript函数。您需要在服务器上调用此代码。请记住,将页面的HTML / DOM视图和页面的服务器视图分开。
要在服务器视图中连接事件,vb.net语法如下所示:
AddHandler LocationDescription2.[EventName], AddressOf newFunction
不幸的是,DropDownList控件没有点击事件,因此您需要使用此处显示的事件之一替换我的示例中的EventName
:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.dropdownlist_events.aspx
你必须满足最后一项所包含的事件,或者做一些复杂的javascript工作来模拟Click事件。
此外,重要的是要记住,当您处理服务器端事件时,不仅会运行您的特定事件处理程序,还会重建整个页面。它做了一个完整的回发(暂时搁置ajax)。你需要确保这真的是你想要的。
答案 2 :(得分:1)
ASP.Net Ajax工具包。查找级联下拉列表和相关示例。它也会更好地看待行为。