?
Solved

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

Posted on 2006-05-16
21
Medium Priority
?
529 Views
Last Modified: 2012-06-27
How do i exit from a if/else loop (break away) in my C# page_load method
0
Comment
Question by:jedistar
  • 11
  • 10
21 Comments
 
LVL 20

Accepted Solution

by:
REA_ANDREW earned 1000 total points
ID: 16689173
Can you show the code please and also have you tried

break;
0
 

Author Comment

by:jedistar
ID: 16689200
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
 

Author Comment

by:jedistar
ID: 16689205
i know in vb.net, is application.exit or somethin..

what abt c#
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 20

Expert Comment

by:REA_ANDREW
ID: 16689208
return false;

try that
0
 

Author Comment

by:jedistar
ID: 16689212
Err:

Since 'app.btnSubmit_Click(object, System.Web.UI.ImageClickEventArgs)' returns void, a return keyword must not be followed by an object expression
0
 
LVL 20

Expert Comment

by:REA_ANDREW
ID: 16689240
ok then I thing in VB it will be this

Exit Sub
0
 

Author Comment

by:jedistar
ID: 16689293
I need it in C#
0
 
LVL 20

Expert Comment

by:REA_ANDREW
ID: 16689331
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
 

Author Comment

by:jedistar
ID: 16689378
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
 

Author Comment

by:jedistar
ID: 16689388
(133): No enclosing loop out of which to break or continue
0
 
LVL 20

Expert Comment

by:REA_ANDREW
ID: 16689461
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
 

Author Comment

by:jedistar
ID: 16699081

            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
 

Author Comment

by:jedistar
ID: 16699140
note that this is asp.net C# -> .aspx.cs
0
 
LVL 20

Expert Comment

by:REA_ANDREW
ID: 16699168
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
 

Author Comment

by:jedistar
ID: 16699191
this is redundant code.
0
 
LVL 20

Expert Comment

by:REA_ANDREW
ID: 16699196
what do you mean by redundant
0
 
LVL 20

Expert Comment

by:REA_ANDREW
ID: 16699205
It will use the code once
0
 

Author Comment

by:jedistar
ID: 16699275
i'm sure theres a way to exit.
0
 
LVL 20

Expert Comment

by:REA_ANDREW
ID: 16699286
ok found it.  This is the C# equivalent of the VB Exit Sub

simply

return;
0
 

Author Comment

by:jedistar
ID: 16699423
u r cool!
thanks.
0
 
LVL 20

Expert Comment

by:REA_ANDREW
ID: 16699442
no worries, phew was worried then lol, always the simple things we forget. doh!!!!!
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Loops Section Overview
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question