Exit from a C# if/else loop in ASP.NET web app

How do i exit from a if/else loop (break away) in my C# page_load method
jedistarAsked:
Who is Participating?
 
REA_ANDREWConnect With a Mentor Commented:
Can you show the code please and also have you tried

break;
0
 
jedistarAuthor Commented:
oops didnt work.

I meant break from the main routine.

private void btnSubmit_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{

    if (...
              -> how do i break/stop running this click function..
    )
0
 
jedistarAuthor Commented:
i know in vb.net, is application.exit or somethin..

what abt c#
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
REA_ANDREWCommented:
return false;

try that
0
 
jedistarAuthor Commented:
Err:

Since 'app.btnSubmit_Click(object, System.Web.UI.ImageClickEventArgs)' returns void, a return keyword must not be followed by an object expression
0
 
REA_ANDREWCommented:
ok then I thing in VB it will be this

Exit Sub
0
 
jedistarAuthor Commented:
I need it in C#
0
 
REA_ANDREWCommented:
jedistar, this works for me, it will output 49 and stop, due to the break, therefore, stopping the onclick event

    protected void Button1_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < 200; i++)
        {
            if(i==50)
            {
                break;
            }
            else{
                Response.Write(i.ToString());
            }
        }
    }
0
 
jedistarAuthor Commented:
but i have some more code below the

if else..

i don't wish to run the code there.

protected void Button1_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < 200; i++)
        {
            if(i==50)
            {
                break;
            }
            else{
                Response.Write(i.ToString());
            }
        }
    }
0
 
jedistarAuthor Commented:
(133): No enclosing loop out of which to break or continue
0
 
REA_ANDREWCommented:
Post the whole event then so I can see please and I will take a look

//comment in where you would like your break;
0
 
jedistarAuthor Commented:

            private void Login_Click(object sender, System.Web.UI.ImageClickEventArgs e)
            {
                  if (......)
                  {
                        break;
                  }
               }


Error: - No enclosing loop out of which to break or continue

Cannot compile
0
 
jedistarAuthor Commented:
note that this is asp.net C# -> .aspx.cs
0
 
REA_ANDREWCommented:
I have a small hack for this cause I cannot think of another way, follow this structure

          private void Login_Click(object sender, System.Web.UI.ImageClickEventArgs e)
          {
for(int i = 0;i<1;i++)
{
//Put all your code in this loop which will only loop once, there fore is the same if it was not there, although brak will work.
               if (......)
               {
                    break;
               }
}
               }
0
 
jedistarAuthor Commented:
this is redundant code.
0
 
REA_ANDREWCommented:
what do you mean by redundant
0
 
REA_ANDREWCommented:
It will use the code once
0
 
jedistarAuthor Commented:
i'm sure theres a way to exit.
0
 
REA_ANDREWCommented:
ok found it.  This is the C# equivalent of the VB Exit Sub

simply

return;
0
 
jedistarAuthor Commented:
u r cool!
thanks.
0
 
REA_ANDREWCommented:
no worries, phew was worried then lol, always the simple things we forget. doh!!!!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.