在下拉列表打开之前无法调用函数 - onclick无法在aspx页面上运行

问题描述 投票:0回答:1
 <asp:UpdatePanel ID="showsDatalistPanel" runat="server" Visible="false" UpdateMode="Always">
<ContentTemplate>

<div>
    <asp:DropDownList ID="dateTimeFilter" CssClass="dateTimeFilterIdentifierCls" runat="server"
         onclick="dateTimeFilter_SelectedIndexChanged" AutoPostBack="true" Visible="true" ClientIDMode="Static">
    </asp:DropDownList>
    <label id="dateTimeFilterLabel" runat="server" style="padding-left: 15px" visible="false">
        בחירת מופע לפי תאריך
    </label>
</div>

如何在单击下拉列表时调用服务器端功能?谢谢

asp.net
1个回答
0
投票

如果你真的想使用click事件,那么你必须使用JS来模拟它,因为DropDownList没有内置的onclick功能:

<asp:DropDownList ID="dateTimeFilter" CssClass="dateTimeFilterIdentifierCls" runat="server"
    onclick="__doPostBack('dateTimeFilter', null);" AutoPostBack="true" Visible="true" ClientIDMode="Static">
</asp:DropDownList>

然后在您的服务器代码中相应地修改页面加载事件:

protected void Page_Load(object sender, EventArgs e)
{
    if(Page.IsPostBack)
    {
        if(Request.Params.Get("__EVENTTARGET").ToString() == dateTimeFilter.ClientID)
        {
            dateTimeFilter_SelectedIndexChanged(dateTimeFilter, null);
        }
    }       
}

但我很困惑为什么你不能只使用标准的OnSelectedIndexChanged或OnSelectedValueChanged?

© www.soinside.com 2019 - 2024. All rights reserved.