简体   繁体   English

下拉列表 selectedindexchanged 事件未触发

[英]Dropdownlist selectedindexchanged event is not firing

Simply I have a Dropdownlist with RequiredFieldValidatior in UpdatePanel on a page, I have enabled autopostback for the dropdownlist.只是我在页面上的 UpdatePanel 中有一个带有 RequiredFieldValidatior 的下拉列表,我为下拉列表启用了自动回发。

The problem is that Dropdownlist selectedindex event is not firing.问题是 Dropdownlist selectedindex 事件没有触发。 This unexpected behavior happens when I validate the page and ant error occurs.当我验证页面并发生 ant 错误时,会发生这种意外行为。

I searched a lot but unable to find the solution我搜索了很多但无法找到解决方案

my code is as follows:我的代码如下:

<%@ 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>

Codebehind:代码隐藏:

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();
    }
}

Steps to repopulate the issue:重新填充问题的步骤:
1. Click first option in dropdown 1. 点击下拉菜单中的第一个选项
2. click on submit button 2.点击提交按钮
3. change dropdownlist value (this should fire selectedindex changed event, but it doesn't) 3.更改下拉列表值(这应该触发 selectedindex changed 事件,但它没有)

PS: I do not want to postback to happen when the submit button is clicked that is why I added <input> instead of asp.net button, PS:我不想在点击提交按钮时发生回发,这就是为什么我添加了<input>而不是 asp.net 按钮,
even if I add asp.net button it doesnt work即使我添加了 asp.net 按钮它也不起作用

Added Page_BlockSubmit = false;添加Page_BlockSubmit = false; in the JS code which was preventing the postback...在阻止回发的 JS 代码中......

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

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

replace代替

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

with

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

the issue is solved.问题解决了。

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

in drop DropDownList在下拉下拉列表中

For more details click here欲了解更多详情, 请点击此处

On clicking submit button, if page validation returns false and then changing the drop-down's selected-index will not work for first time.单击提交按钮时,如果页面验证返回 false,则第一次更改下拉列表的 selected-index 将不起作用。 Because on submitting the form it will do Form validation.因为在提交表单时它会进行表单验证。

  • If Validation returns false [indicates not to submit the Form], then you can can't go to server side code.如果Validation返回false【表示不提交Form】,那么就不能去服务器端代码了。
  • Since you have used “SelectedstateChanged” event for the Dropdown, the code inside the event handler function will not execute after form validation is returned as false.由于您在下拉菜单中使用了“SelectedstateChanged”事件,因此在表单验证返回为 false 后,事件处理函数中的代码将不会执行。

So to handle this problem, add onchange="Page_BlockSubmit = 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>

Reference Link http://burnignorance.com/asp-net-developer-tips/dropdownlist-validation-problem-in-asp-net/参考链接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