繁体   English   中英

下拉列表 selectedindexchanged 事件未触发

[英]Dropdownlist selectedindexchanged event is not firing

只是我在页面上的 UpdatePanel 中有一个带有 RequiredFieldValidatior 的下拉列表,我为下拉列表启用了自动回发。

问题是 Dropdownlist selectedindex 事件没有触发。 当我验证页面并发生 ant 错误时,会发生这种意外行为。

我搜索了很多但无法找到解决方案

我的代码如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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 runat="server">
    <title></title>
    <script type="text/javascript">
        function ValidateMe() {
            if (Page_ClientValidate("vgOption")) {
                alert("valid");
            }

            return false;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="smMain" runat="server">
    </asp:ScriptManager>
    <asp:UpdatePanel ID="pnlMain" runat="server" ChildrenAsTriggers="true">
        <ContentTemplate>
            <table border="1" cellpadding="5" cellspacing="0">
                <tr>
                    <td>
                        Option:
                    </td>
                    <td>
                        <asp:DropDownList runat="server" AutoPostBack="true" ID="Opt" OnSelectedIndexChanged="Opt_SelectedIndexChanged" ValidationGroup="vgOption">
                            <asp:ListItem Text="--Select Option--" Value="0" />
                            <asp:ListItem Text="Upload" />
                            <asp:ListItem Text="Download" />
                        </asp:DropDownList>
                        <asp:RequiredFieldValidator ID="rfv" runat="server" ControlToValidate="Opt" Display="None" InitialValue="0" ValidationGroup="vgOption" ErrorMessage="Please select an option"></asp:RequiredFieldValidator>
                    </td>
                </tr>
                <tr>
                    <td>
                        Postback:
                    </td>
                    <td>
                        <asp:Label Text="" ID="lblMessage" runat="server" />
                    </td>
                </tr>
                <tr>
                    <td>
                    </td>
                    <td>
                        <input type="button" onclick="return ValidateMe();" value="Test" title="Test" />
                        <asp:ValidationSummary ValidationGroup="vgOption" runat="server" ShowMessageBox="true" ShowSummary="false" DisplayMode="List" />
                    </td>
                </tr>
            </table>
        </ContentTemplate>
    </asp:UpdatePanel>
    </form>
</body>
</html>

代码隐藏:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Opt_SelectedIndexChanged(object sender, EventArgs e)
    {
        lblMessage.Text = "Autopostback: " + DateTime.Now.ToString();
    }
}

重新填充问题的步骤:
1. 点击下拉菜单中的第一个选项
2.点击提交按钮
3.更改下拉列表值(这应该触发 selectedindex changed 事件,但它没有)

PS:我不想在点击提交按钮时发生回发,这就是为什么我添加了<input>而不是 asp.net 按钮,
即使我添加了 asp.net 按钮它也不起作用

添加Page_BlockSubmit = false; 在阻止回发的 JS 代码中......

<script type="text/javascript">
    function ValidateMe() {
        if (Page_ClientValidate("vgOption")) {
            alert("valid");
        }
        Page_BlockSubmit = false;
        return false;
    }
</script>

参考: http : //www.techques.com/question/1-2083929/Dropdownlist-doesn%27t-postback-after-Page_ClientValidate%28%29

代替

<input type="button" value="Test" title="Test"  runat="server" validationgroup="vgOption"/>

<asp:Button ID="btn" runat="server" Title="Test" Text="Test"  ValidationGroup="vgOption" OnClientClick="return ValidateMe()"/>

问题解决了。

Add property ViewStateMode="Enabled" and EnableViewState="true"

在下拉下拉列表中

欲了解更多详情, 请点击此处

单击提交按钮时,如果页面验证返回 false,则第一次更改下拉列表的 selected-index 将不起作用。 因为在提交表单时它会进行表单验证。

  • 如果Validation返回false【表示不提交Form】,那么就不能去服务器端代码了。
  • 由于您在下拉菜单中使用了“SelectedstateChanged”事件,因此在表单验证返回为 false 后,事件处理函数中的代码将不会执行。

所以要处理这个问题,添加onchange="Page_BlockSubmit = false;"

    <asp:DropDownList runat="server" AutoPostBack="true" ID="Opt" OnSelectedIndexChanged="Opt_SelectedIndexChanged"
 CausesValidation="false" ValidationGroup="none" onchange="Page_BlockSubmit = false;">
       <asp:ListItem Text="--Select Option--" Value="0" />
       <asp:ListItem Text="Upload" />
       <asp:ListItem Text="Download" />
    </asp:DropDownList>

参考链接http://burnignorance.com/asp-net-developer-tips/dropdownlist-validation-problem-in-asp-net/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM