How to redirect in Page_Load without produce error message?

Hi experts!

I’m new to ASP.NET and need a little help.
I have a MasterPage and log in dialog box. If user are choose remember me options I need to execute directly btnLogin.

Look my code:


        protected void Page_Load(object sender, EventArgs e)
        {

             // … other code
              if (sRememberMe == "1")
                {
                    txtLogin.Text = sLogin; btnLoginClick(true,true );
                }

         }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            // check user name / psw          …
            Response.Redirect("Wellcome_CO.aspx");
        }

 It produce strange error and I find that problem is that in Page_Load I can not call Redirect/ So – how to change code ?

 I try, but not find list with other events of this form. May be I need to put this code in some events like LoadPageComplate ? Or I need to use Timer and after 3-5 sec. to do redirect?
dvplayltdAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Todd GerbertIT ConsultantCommented:
protected void Page_Load(object sender, EventArgs e)
{
  if (sRememberMe == "1")
  {
    txtLogin.Text = sLogin;
    btnLogin_Click(null, null);
  }
}
0
lazyberezovskyCommented:
It is OK to call Response.Redirect in Page_Load event handler.
Please, provide error that you received.
0
guru_samiCommented:
From where do you get "sRememberMe" ? If you are using FormsAuthentication and asp.net Login control, Upon selecting Remember Me checkbox ...you don't have to do any login stuff it will be done automatically...
So not sure what exactly you are trying to achieve....
0
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

dvplayltdAuthor Commented:
Thanks to all answer. I will provide more from my code.

I see 2 types of error with this code.
Page to which I make redirect has AJAX with Ajax  ControlToolkit I use Extender.

First error is - somethink I get message while I debug that I do not have ExtenderBase.cs. This is from IDE of VS2008 while I trace it.  Then without change nothing in next run I stop to get these error. My explain is that this is due to lack of time to load this class mode ?!? Really strange, that is why I think that I should give some seconds AFTER Page_Load . I was searching for this error and find that this is due to AJAX, do somethink and finally stop give me this error.

Now, again without change code it simple make contunies redirect, produce some error and application is on non end loop ...

 What is interesting - all is function if I do not make a call directly to Login button in Page_Load.

 Look on // in the end of this procedure
 Look that I need to make some change in code to understand what is happen.

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {
               
                   
                 // check for remember me and if is need - log on system directly!

                    HttpCookie aCookieR = Request.Cookies["xxx"];
                    string sRememberMe = "0"; string sLogin = "";
                    try { sRememberMe = aCookieR["bRememberMe"]; sLogin = aCookieR["LoginName"].ToString(); }
                    catch { }
                    // if i REMOVE THIS // THEN APPLICATION GOES TO LOOP WITHOUT END
                  //  if (sRememberMe == "1") { txtLogin.Text = sLogin; btnLoginClick(true,true ); }
               
            }
        }


    // This is function that actally do all. I need it , beacuse in Form_Load I call it directly and it allow user to not provide PSW beacouse it use RememberMe in his Last Log in system
        protected void btnLoginClick(bool bFromStart, bool bRememberMe )
        {
            //HttpCookie aCookieR = Request.Cookies["DVPlaySystemR"];
            //string sRememberMe = "0"; string sLoginName = "";
            //try { sRememberMe = aCookieR["bRememberMe"];   }
            //catch { }

            bool bLogOnWithoutPSWSupply = false;
            if (bFromStart && bRememberMe) bLogOnWithoutPSWSupply = true;
              .. other code
   }

// this is event of real button Log in ASPX page.
        protected void Button1_Click(object sender, EventArgs e)
        {
            btnLoginClick(false,false);
        }
0
dvplayltdAuthor Commented:
Hyym. After I set real logs to timer and execute it after 4 sec. I get message show on pic.
After I show where is this file (I find it via Internet)  I get into debug mode.Aaa. This is only to show me where is error, however error is in AJAX and I can not fix it as long I'm epxert of it which I'm not.

Look that problem is with AJAX. But I want to use it - Extender is very fine, so how to do ?
I see some post in Internet that this is some problem of AJAX with MASTER Page, I use Master page.
My version is:  3.0.31106.0 , may be in last version this problem is solved?

Can somebady tell ?

AJAX.JPG
0
dvplayltdAuthor Commented:
My login panel is with ordinary ASP components, it is not ASP Login form from Toolbox of VS 2008.
0
dvplayltdAuthor Commented:
I was update to latest ASP Ajax and then find that problem is not with it.

Problem is with me .. that I'm new to ASP.NET
I find why I goes to loop without no end.

This is beacouse I have master page in which form_Load I redirect to other page, Wellcome_DC.aspx page, and result is that I execute again MasterPage_Load events and again.

So to solve question - I need to execute only once in first start this RememberMe section. But please help me how to understand which is first execution, and which is second and third and so on ? I try with this code, with Session, but it now work well. How can I undersatnd in form_Load of MAtserPage that this reload are invoke from chaild page in fact ???

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {
                 // ... other code

                 // check for remember me and if is need - log on system!
                 bool bFirst=true;
                 try { bFirst= Convert.ToBoolean( Session["bFirst"]) ; } catch {}

                if (bFirst)
                {
                    HttpCookie aCookieR = Request.Cookies["DVPlaySystemR"];
                    string sRememberMe = "0"; string sLogin = "";
                    try { sRememberMe = aCookieR["bRememberMe"]; sLogin = aCookieR["LoginName"].ToString(); }
                    catch { }

                //    if (sRememberMe == "1") { txtLogin.Text = sLogin; tmrLogin.Enabled = true; }
                    if (sRememberMe == "1"  ) { Session["bFirst"] = false;   txtLogin.Text = sLogin; btnLoginClick(true, true); }
                }
0
renilccCommented:
try btnLoginClick(sender,e) instead of btnLoginClick(true,true )
0
Todd GerbertIT ConsultantCommented:
Solution 1: Do not use a Master Page for the first page.
Solution 2: Move your code out of the master page, and into the content page.
0
dvplayltdAuthor Commented:
To tgerbert

Not is real, desing of site is ready and
I'm, sure I can make it work, in fact I already did with Session in which remember first call and in second call.

But I want to avoid using session ...
0
Todd GerbertIT ConsultantCommented:
The fact is that if this code is only intended to run for the Login page, then it should exist only in the login page. The Master page, ideally, should contain only code that applies to every page - the login code does not.
I highly recommend re-working your website so that it is more "correct," otherwise you'll need to use your Session-based method...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
guru_samiCommented:
// if i REMOVE THIS // THEN APPLICATION GOES TO LOOP WITHOUT END
 //  if (sRememberMe == "1") { txtLogin.Text = sLogin; btnLoginClick(true,true );

Did you set breakpoints and see why is it going into infinite loop....may be if that gives any idea
0
Todd GerbertIT ConsultantCommented:
Did you set breakpoints and see why is it going into infinite loop
I'm guessing the infinite loop is occurring because the code he's provided is in the master page's Page_Load, and since the it includes a Reponse.Redirect to another page using the same Master a loop is created.
0
guru_samiCommented:
If you are using FormsAuthentication you can use this instead of Session....

//If request is not authenticated and remember is set
if (sRememberMe == "1" && !Request.IsAuthenticated )
 {  
 txtLogin.Text = sLogin; btnLoginClick(true, true);
 }
0
guru_samiCommented:
or better try this:

    protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {
                 // ... other code
                 // user has not yet login manually or automatically
                 if(!Request.IsAuthenticated)
                 {
                         //put all your first time access code here...
                }
          }
      }
0
Todd GerbertIT ConsultantCommented:
@guru_sami
That won't stop his infinite loop, since this code is in the Master Page, when btnLoginClick() is called it redirects to another page which uses the same Master, so this code block will be executed again, calling btnLoginClick which redirects to another page, causing this code to be executed again, and so on.
0
Todd GerbertIT ConsultantCommented:
Yes it will, misread. ;)
0
dvplayltdAuthor Commented:
To Guru_sami

Yes, this with FormsAuthentication  could be solution. But .. what is method inside of this which actally remember
Request.IsAuthenticated ? I guest that this will be session again ... or cookie?

Look that I have 2 solution:
 1. To try to change place of login form - to be not in Master.Page - but this will demand change in design of site.
 2. To use my Session method. In fact .. now I think .. it will work. If for any bad reason this state is loose it not so bid deal .. user simple will be ask to log in again.

Will see in 1-2 days what will do ... and I guest that will give point to all of person aswer me ...
0
Todd GerbertIT ConsultantCommented:
Solution 3: move just the login code out of the master page's Page_Load, and put it in the Login page's Page_Load.
0
dvplayltdAuthor Commented:
to tgerbert

You are wrong, name of solution 3 if I follow you recomend should be:

Solution 3: Cancel using RememberMe options

:-).

I can not move it. remember, all system is working fine, question is that when user load page first time I should check and make automatic login, so there no change this to be in diffrent than Page_Login page.
0
Todd GerbertIT ConsultantCommented:
So you're saying the line "Response.Redirect("Wellcome_CO.aspx")" needs to be on EVERY page in your site? As I explained in my earlier comment, http:#a30940943, you were getting stuck in a loop because the code in Master's Page_Load was calling Response.Redirect.
0
dvplayltdAuthor Commented:
thanks very much for you help. I really I really appraise it.

In general – problem is in design of Matser Page .. what to say. Because I’m new to ASP.NET I pay to a person to make design and first version of code and this person is also programmer with 5+ years in ASP.Net … but look he  is not so good as is expected. However, money are paid, project is almost complete .. so I need to handle it fast. You are boys are professional and I’m sure you know this situation from personal experience :-)

Finally I will use my solution with Session.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.