Home All Groups Group Topic Archive Search About

Newbie Question - Master Page triggering validator

Author
17 Dec 2005 6:35 AM
damiensawyer@yahoo.com.au
Hello all,

I have set up a very simple master page (a table, a button and a
ContentPlaceHolder) and have set a simple web form to use it.

The web form (code below) has a textbox, a simple validator and a
button on it. When I press the button on the 'sub' form, I wish for
validation to take place and redirection as appropriate.

The problem is that, when the button on the master form is pressed, it
too triggers validation. I am guessing that it is not doing it via my
button press code, however just as the default behaviour of the
validator.

Can someone please tell me what I'm doing wrong?

Thanks very much in advance,


Damien Sawyer



<%@ Page trace="true" Language="VB"
MasterPageFile="~/MasterPage.master" Title="Untitled Page" %>

<script runat="server">

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs)
        'Me.Validate()
        If Me.IsValid Then Me.Response.Redirect("~/SQL.aspx")

    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs)
        Trace.Write("Hello from Trace")
    End Sub


</script>

<asp:Content ID="Content1"
ContentPlaceHolderID="bodyContentPlaceHolder" Runat="Server">
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click"
Text="Login" />
    <asp:RequiredFieldValidator ID="RequiredFieldValidator1"
runat="server" ControlToValidate="TextBox1"
        ErrorMessage="Name Required" ></asp:RequiredFieldValidator>
</asp:Content>

Author
17 Dec 2005 11:15 AM
Daniel TIZON
Hi Damien,
If you don't whant a button to participate to the page validation, you can
set his "CauseValidation" property to false.
Another solution, now available with ASP.NET2.0 is to isolate validators and
buttons by groups.
For an example, you can set the property "ValidationGroup" of the button and
validators of your content page to "group1", so your button will only verify
the validtity on the validators in the same ValidationGroup

Hope this helps,

--
Daniel TIZON
MCP - MCSD.NET - MCT


<damiensaw***@yahoo.com.au> a écrit dans le message de news:
1134801351.246642.315***@g43g2000cwa.googlegroups.com...
Show quote
> Hello all,
>
> I have set up a very simple master page (a table, a button and a
> ContentPlaceHolder) and have set a simple web form to use it.
>
> The web form (code below) has a textbox, a simple validator and a
> button on it. When I press the button on the 'sub' form, I wish for
> validation to take place and redirection as appropriate.
>
> The problem is that, when the button on the master form is pressed, it
> too triggers validation. I am guessing that it is not doing it via my
> button press code, however just as the default behaviour of the
> validator.
>
> Can someone please tell me what I'm doing wrong?
>
> Thanks very much in advance,
>
>
> Damien Sawyer
>
>
>
> <%@ Page trace="true" Language="VB"
> MasterPageFile="~/MasterPage.master" Title="Untitled Page" %>
>
> <script runat="server">
>
>    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
> System.EventArgs)
>        'Me.Validate()
>        If Me.IsValid Then Me.Response.Redirect("~/SQL.aspx")
>
>    End Sub
>
>    Protected Sub Page_Load(ByVal sender As Object, ByVal e As
> System.EventArgs)
>        Trace.Write("Hello from Trace")
>    End Sub
>
>
> </script>
>
> <asp:Content ID="Content1"
> ContentPlaceHolderID="bodyContentPlaceHolder" Runat="Server">
>    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
>    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click"
> Text="Login" />
>    <asp:RequiredFieldValidator ID="RequiredFieldValidator1"
> runat="server" ControlToValidate="TextBox1"
>        ErrorMessage="Name Required" ></asp:RequiredFieldValidator>
> </asp:Content>
>

AddThis Social Bookmark Button