单击时尝试填充 DropDownList (Asp.Net)。我在下面有一个示例,但我无法让 DropDownList 在加载后保持展开状态。除了使用 3rd 方控件之外,有没有人有从服务器按需加载的解决方案?
JavaScript
<script type="text/javascript">
function LoadOnDemand(ddl) {
if (ddl != "") {
var control = document.getElementById(ddl.id);
__doPostBack(ddl.id, "LoadOnDemand");
}
}
function AfterLoadOnDemand(ddl) {
}
</script>
标记
<body>
<form id="form1" runat="server">
<div>
<asp:Panel ID="DynamicPanel" runat="server" Width="200">
</asp:Panel>
</div>
</form>
</body>
代码隐藏
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim target As String = Page.Request.Params.Get("__EVENTTARGET")
Dim eventarg As String = Page.Request.Params.Get("__EVENTARGUMENT")
Call CreateDynamicControls()
If IsPostBack Then
If target <> "" Then
If eventarg = "LoadOnDemand" Then
Dim ctrl As Control = FindControl(target)
If ctrl IsNot Nothing Then
If TypeOf ctrl Is DropDownList Then
'Note: values would be from a database or table query.
' and some other logic. Simplified here.
Dim ddl As DropDownList = CType(ctrl, DropDownList)
ddl.Items.Clear()
ddl.Items.Add("one")
ddl.Items.Add("two")
ddl.Items.Add("three")
'remove onfocus from this control so it doen't fire again
'if they click it immediatly after loading
ddl.Attributes.Add("onfocus", "AfterLoadOnDemand(this)")
End If
'reset the LoadOnDemand for all the other DropDownList controls
For Each notFocusedControl As Control In DynamicPanel.Controls
If TypeOf notFocusedControl Is DropDownList Then
Dim ddl As DropDownList = CType(notFocusedControl, DropDownList)
If ddl.ID <> target Then
ddl.Attributes.Add("onfocus", "LoadOnDemand(this)")
End If
End If
Next
End If
End If
End If
End If
End Sub
Protected Sub CreateDynamicControls()
For i As Integer = 0 To 2
Dim ddl As New DropDownList
ddl.ID = "DropDownList" & i
ddl.Width = 150
ddl.Items.Add("Browse me..")
ddl.Attributes.Add("onfocus", "LoadOnDemand(this)")
ddl.AutoPostBack = True
ddl.EnableViewState = True
DynamicPanel.Controls.Add(ddl)
DynamicPanel.Controls.Add(New LiteralControl("<br/><br/>"))
Next
End Sub