Question

ASP.NET restricts using more than 1 runat="server" form.

I have a Master Page, currently with no form.

I have many other pages, some with a form (Register.aspx - Login.aspx), some without a form.

I would like to add a Logout button in the master (After a user has logged in).

This is kind of normal, the Logout button should always be there.

But to use this ASP Button Controller I need to use a form in the Master Page.

Means, I will not be able to access other pages that has forms because I will receive an error.

I already have other ideas to bypass this.

1- Using JQuery to Load a Handler using Ajax, clear the Session.

2- Make the Logout a link which looks like a button, click on it to load another .aspx page, in the Page Load method, clear the Session, then redirect, sounds like the first one...

3- Make it a link which takes me to the same page (Default.aspx?logout=1) <- and check this parameter inside the Page Load method, if it's there, clear the Session.

Are these good approaches? is there any better?

Was it helpful?

Solution 2

You don't need to bypass it, just wrap the entire asp:ContentPlaceHolder inside the form runat="server" so you'll have the form on every page already.

In the master page

<form runat="server">
    <asp:ContentPlaceHolder id="MainBodyContent"></asp:ContentPlaceHolder>
</form>

OTHER TIPS

Care to explain why you can't have the Form in your masterpage and remove all forms in those pages using that masterpage? That's the most common pattern in ASP.NET anyway.

If you for some reason can't do that, I would go with 2.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top