0

单击时尝试填充 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
4

1 回答 1

0

我发现如何扩展它的唯一方法是通过仿真。这是一个链接 ( ExpandSelect ),它有一个 JavaScript 函数可以做到这一点。这是另一个参考链接

我将以下(初始宽度参数)添加到 .js 文件中。这可以防止展开的下拉菜单缩小到小于下拉菜单的初始宽度。

function ExpandSelect(select, maxOptionsVisible, initialWidth) {

    //code - see file
    select.style.zIndex = "1000000";

    //Added this right after the zIndex
    if (dropWidth < initialWidth) {
        select.style.width = initialWidth + 'px';
        select2.style.width = initialWidth + 'px';
    }

    //code - see file

    }
于 2012-12-03T14:15:00.573 回答