开发者

Controlling flow in ASP.NET with return;, don't render the rest of the page

开发者 https://www.devze.com 2023-01-16 09:34 出处:网络
this question should be fairly basic. I want to control the flow of an ASP.NET page -- if a certain condition is met, I want to write out an error message and stop drawing the page. However, I also wa

this question should be fairly basic. I want to control the flow of an ASP.NET page -- if a certain condition is met, I want to write out an error message and stop drawing the page. However, I also want ASP.NET to output correct HTML (i.e. not cut off in the middle). Right now I am doing this:

   if (condition != what-i-want) {
     Label_Error.Text = "Sorry, you messed up";
     return;
   }

And the problem with that snippet is that ASP.NET draws the rest of the defined page without cutting off after the error. I really don't want to make the whole page Visible = False an开发者_如何学运维d then undo it when someone is authenticated.

Is there some good way to do this? I have tried Response.End() but that doesn't output clean HTML (or anything actually, since I'm checking in Page_Load). I've had similar experiences with Response.Close(), et al.

Thanks.


Cleanest way would be to create another page that gives message to the user about issues and then use server.transfer (or response.redirect) to transfer control to that page.

Yet another way would be to include entire page content in a panel (or panel) apart from stuff that you want user to see and then turn visibility off for the panel for not to show page content - but mind you, page life cycle would not stop; its just that the content within the panel won't be visible.

0

精彩评论

暂无评论...
验证码 换一张
取 消