【问题标题】:asp:DropdownList not firing for SelectedIndexChanged eventasp:DropdownList 未触发 SelectedIndexChanged 事件
【发布时间】:2015-04-30 19:38:05
【问题描述】:

当我更改下拉菜单的选择时,不会触发 Change 事件。我在方法中放了一个断点,程序并没有停止。

这是我的标记:

<form id="form1" runat="server">  
        <div style='text-align:center;'>
        <a style='text-decoration:none;font-size:16px;color:blue;background-color:white;width:200px;padding:4px;' href='LocationDetails.aspx?Location_ID=0' target='detailPanel'> Add Location
        </a></div>
        &nbsp
        <div style='text-align:left;'>
            <asp:Label ID="FacilityTypeLbl" runat="server">Facility Type:</asp:Label>
            <asp:DropDownList ID="FacilityTypeDDL" runat="server" AutoPostBack="true" EnableViewState="true" OnSelectedIndexChanged="FacilityTypeDDL_SelectedIndexChanged">
            </asp:DropDownList>
        </div>
        <hr/>
     <%ListLocations()%>   
 </form>

这是我用来填充列表的 Page_Load 方法,它工作正常。

Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load

        If Not Page.IsPostBack Then
            GetServiceTypes()
            FacilityTypeDDL.DataSource = dtServiceTypes
            FacilityTypeDDL.DataTextField = dtServiceTypes.Columns("Title").ToString()
            FacilityTypeDDL.DataValueField = dtServiceTypes.Columns("ID").ToString()
            FacilityTypeDDL.DataBind()
        End If
    End Sub

这是我的更改事件:

 Protected Sub FacilityTypeDDL_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles FacilityTypeDDL.SelectedIndexChanged
        strFacilityValue = FacilityTypeDDL.SelectedValue
        ListLocations()
End Sub

我在代码的第一行放置了一个断点,并且在更改下拉选择后它不会在此事件处停止。

我做错了什么?

更新 这是我的全部标记。这有什么问题吗?

<%@ Page Language="VB" AutoEventWireup="true" CodeFile="Locations.aspx.vb" Inherits="Editor_Locations" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="LocationHead" runat="server">
    <title>Locations</title>
    <meta http-equiv="Content-Language" content="en-us"/>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"/>   
</head>
<body>
 <form id="form1" runat="server">        
        <div style='text-align:left;'>
            <asp:Label ID="FacilityTypeLbl" runat="server">Facility Type:</asp:Label>
            <asp:DropDownList ID="FacilityTypeDDL" runat="server" AutoPostBack="true" EnableViewState="true" OnSelectedIndexChanged="FacilityTypeDDL_SelectedIndexChanged">
            <asp:ListItem>Test1</asp:ListItem>
            <asp:ListItem>test2</asp:ListItem>
            <asp:ListItem>Test34</asp:ListItem>
            </asp:DropDownList>
        </div>
        <hr/>
 </form>
</body>
</html>

更新 此应用程序是一个网站,而不是一个 Web 应用程序。 (我不知道有什么不同,但现在我知道了。)为了弄清楚 SelectedIndexChange 事件不会触发的方式,我添加了一个按钮和一个单击事件。当我更改下拉列表中的选择时,不会触发任何事件。当我单击按钮时,会触发 click 事件,然后触发 selectedindexchange 事件。

我认为 SelectedIndexChanged 事件不会起作用。

当下拉列表更改时,还有其他方法可以连接回发吗? 当列表更改时,我可以以某种方式从 javascript 函数调用中使用 __doPostback 吗?

任何建议将不胜感激!

【问题讨论】:

  • 您的 .aspx 页面指令上是否设置了 AutoEventWireup="true"?
  • 它被设置为“假”。我将它设置为“true”,但它仍然没有触发事件。
  • 你试过ViewStateMode="Enabled"这个DropdownList吗?
  • 是的。我添加了该属性,但仍然没有工作
  • 当我在 aspx 页面中添加 ListItem 时,上述标记对我有用。注释 Page_Load 代码并尝试在 html 标记中添加一些项目并再次检查。

标签: asp.net vb.net


【解决方案1】:

我无法让 asp:DropdownList 触发 SelectedIndexChanged 事件。所以我就这样完成了任务。

如上所示动态添加下拉菜单并添加调用javascript的更改事件:

 onchange='UpdateLocationList()'

这个函数必须放在一个 .js 文件中。这个网站已经有一个 .js 文件。它还有一些我用来完成任务的 AJAX 调用。 在 UpdateLocationList() 中,我获得了在下拉列表中设置为值属性的服务类型的 ID。然后,我使用已经是 .js 文件的一部分的函数到 LocationDetails 页面,使用服务类型 ID 仅显示该服务类型的设施。这是函数:

    function updateLocationList() {
    var ddlFacilityType = document.getElementById("FacilityTypeDDL");
    var facilityValue = ddlFacilityType.options[ddlFacilityType.selectedIndex].value;
    processAjax("/editor/Locations.aspx?Location_ID=" + facilityValue, "navPanel");
}

像魅力一样工作。

感谢您的所有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-15
    相关资源
    最近更新 更多