【问题标题】:Dropdownlist selectedindexchanged event is not firing下拉列表 selectedindexchanged 事件未触发
【发布时间】:2013-07-15 09:21:28
【问题描述】:

我只是在页面上的 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 更改事件,但不会)

PS:我不想在点击提交按钮时发生回发,这就是我添加&lt;input&gt;而不是asp.net按钮的原因,
即使我添加了 asp.net 按钮,它也不起作用

【问题讨论】:

    标签: c# asp.net ajaxcontroltoolkit


    【解决方案1】:

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

    <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

    【讨论】:

      【解决方案2】:

      替换

      <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()"/>
      

      问题解决了。

      【讨论】:

        【解决方案3】:
        Add property ViewStateMode="Enabled" and EnableViewState="true"
        

        在下拉下拉列表中

        更多详情click here

        【讨论】:

          【解决方案4】:

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

          • 如果 Validation 返回 false [表示不提交表单],则无法转到服务器端代码。
          • 由于您为下拉菜单使用了“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/

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2010-10-03
            • 2017-06-08
            相关资源
            最近更新 更多