Link to home
Start Free TrialLog in
Avatar of dvplayltd
dvplayltdFlag for Bulgaria

asked on

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?
Avatar of Todd Gerbert
Todd Gerbert
Flag of United States of America image

protected void Page_Load(object sender, EventArgs e)
{
  if (sRememberMe == "1")
  {
    txtLogin.Text = sLogin;
    btnLogin_Click(null, null);
  }
}
It is OK to call Response.Redirect in Page_Load event handler.
Please, provide error that you received.
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....
Avatar of dvplayltd

ASKER

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);
        }
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
My login panel is with ordinary ASP components, it is not ASP Login form from Toolbox of VS 2008.
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); }
                }
Avatar of renilcc
renilcc

try btnLoginClick(sender,e) instead of btnLoginClick(true,true )
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.
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 ...
ASKER CERTIFIED SOLUTION
Avatar of Todd Gerbert
Todd Gerbert
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
// 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
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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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...
                }
          }
      }
@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.
Yes it will, misread. ;)
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 ...
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.
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.
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.
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.