Bypassing ASP.NET's One RUNAT SERVER FORM per page

陌路散爱 提交于 2019-12-24 11:06:41

问题


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?


回答1:


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>



回答2:


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.



来源:https://stackoverflow.com/questions/14295720/bypassing-asp-nets-one-runat-server-form-per-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!