user2289677
user2289677

Reputation: 33

Master Page button triggers validation controls

There are several linkbutton controls in my masterpage, I bind click event to redirect to other pages, it works. But if my content page has Validator, it doesn't works. When I click the linkbutton, the validator will stop the event, what should I do to resolve it? Thanks.

Page.aspx

<asp:LinkButton ID="lnkbtnHomePage" runat="server" Font-Underline="true" ForeColor="White" OnClick="lnkbtnHomePage_Click">HomePage</asp:LinkButton>

Page.aspx.cs

protected void lnkbtnHomePage_Click(object sender, EventArgs e)
{
    Response.Redirect("Index.aspx");
}

Upvotes: 1

Views: 1399

Answers (2)

Tim B James
Tim B James

Reputation: 20364

There are two options which you can use for this.

The first is to add the attribute CausesValidation="false" to the buttons which you do not want to trigger any validation on the page.

The second option is to add the ValidationGroup="[groupname]" attribute to the controls and buttons which you want to trigger the validation.

The first option is good if you have a one or two buttons on the page which shouldn't trigger the validation, however the second options gives you a safeguard against you adding a new controls which could interfere with your validation.

The second option is also very handy when you have different sections of a page which have their own validation groups and shouldn't effect others. For example if you have some standard controls on your master page which are shown throughout your site, but then have individual pages with their own forms. You can easily control their behaviour.

However, as Yassine Houssni has mentioned, you would be better off just using standard <a> tags or even <asp:HyperLink> controls for simple behaviour as changing pages. If your link suddenly changes, then you will need to recompile the project rather than being able to update the link within the .aspx/master page.

Upvotes: 1

Related Questions