English 中文(简体)
不按多功能推进器第二步执行 页 次
原标题:CustomValidator not executing on second step of a multi-panel Page

I am experiencing a problem with an ASPX page not executing a CustomValidator. The Page consists of 3 ASP Panels which swap visibility for each step in a 3 step process. The first step/panel functions as expected, executing all CustomValidators when I click the submit button. If valid, the button click hides its panel and shows the second panel for step #2 which contains another CustomValidator. When clicking the submit button on this second panel, the CustomValidator never executes and the Page always reports that it IsValid.

我以小的榜样照搬了这一行为。 这里是相关法典......

Default.aspx

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <h2>
        Welcome to ASP.NET!
    </h2>
    <p>
        To learn more about ASP.NET visit <a href="http://www.asp.net" title="ASP.NET Website">www.asp.net</a>.
    </p>
    <p>
        You can also find <a href="http://go.microsoft.com/fwlink/?LinkID=152368&amp;clcid=0x409"
            title="MSDN ASP.NET Docs">documentation on ASP.NET at MSDN</a>.
    </p>
    <asp:Panel ID="Panel1" runat="server" Visible="true">
        <div>
            <asp:CustomValidator 
                ID="CustomValidator1" 
                runat="server" 
                ControlToValidate="TextBox1"
                ValidateEmptyText="true"
                Display="Dynamic" 
                OnServerValidate="CustomValidator1_ServerValidate">
            </asp:CustomValidator>
        </div>
        <div>
            <asp:Label ID="Label1" runat="server" Text="Type in anything:" AssociatedControlID="TextBox1" />
            &nbsp;
            <asp:TextBox ID="TextBox1" runat="server" />
        </div>
        <div>
            <asp:Button ID="Button1" runat="server" Text="Show Panel #2" OnClick="Button1_Click" />
        </div>
    </asp:Panel>
    <asp:Panel ID="Panel2" runat="server" Visible="false">
        <div>
            <asp:CustomValidator 
                ID="CustomValidator2" 
                runat="server" 
                Display="Dynamic" 
                OnServerValidate="CustomValidator2_ServerValidate">
            </asp:CustomValidator>
        </div>
        <div>
            <asp:Button ID="Button2" runat="server" Text="I should cause an Exception..." OnClick="Button2_Click" />
        </div>
    </asp:Panel>
    <asp:Panel ID="Panel3" runat="server" Visible="false">
        <p>An exception should have been thrown. :(</p>
    </asp:Panel>
</asp:Content>

Default.aspx.cs

public partial class Default : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        Panel1.Visible = true;
        Panel2.Visible = false;
        Panel3.Visible = false;
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            Panel1.Visible = false;
            Panel2.Visible = true;
            Panel3.Visible = false;
        }
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            Panel1.Visible = false;
            Panel2.Visible = false;
            Panel3.Visible = true;
        }
    }

    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        string userEnteredText = TextBox1.Text;
        if (string.IsNullOrEmpty(userEnteredText))
        {
            CustomValidator1.Text = "Text is required!";
            args.IsValid = false;
        }
        else if (!userEnteredText.ToLower().Equals("anything"))
        {
            CustomValidator1.Text = "You didn t type  anything ! ;)";
            TextBox1.Text = null;
            args.IsValid = false;
        }
        else
        {
            args.IsValid = true;
        }
    }

    protected void CustomValidator2_ServerValidate(object source, ServerValidateEventArgs args)
    {
        throw new Exception("This ServerValidate() method never triggers!");
    }
}

我不理解为什么习惯Validator2方法从未执行。 谁能解释这种行为?

最佳回答

由于您不再将控制权设定在您的假想中,将财产归结为习俗者。

除非ValidateWhenEmpty是真实的,否则,在控制网状空白时,不会对海关人员进行评价。

<><>UPDATE:

Ok this was wrong. But do you really need to set visibility of the panels in the Page_Load? You ve done it already declaratively in the .aspx. If you remove it from Page_Load, the validator works. I suppose it does not work if the validator is Visible=false or is inside a containing control that is Visible=false.

问题回答

你们应当利用各州的瓦莱德集团财产以及每个小组的相应验证控制。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签