1

只需我在页面上的 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:我不想在单击提交按钮时发生回发,这就是为什么我添加<input>而不是 asp.net 按钮,
即使我添加了 asp.net 按钮它也不起作用

4

4 回答 4

2

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

于 2013-07-17T06:52:48.657 回答
1

代替

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

问题解决了。

于 2013-07-15T10:19:37.627 回答
0
Add property ViewStateMode="Enabled" and EnableViewState="true"

在下拉下拉列表中

更多详情请点击这里

于 2013-10-10T09:26:19.720 回答
0

On clicking submit button, if page validation returns false and then changing the drop-down's selected-index will not work for first time. 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.
  • 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.

So to handle this problem, add 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/

于 2019-12-13T10:53:08.320 回答