Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello to all.

I am making an asp.net web application an I want to give the user the facility to login and logout on every page.

I have taken two image buttons on masterpage one is of login and second of logout.

I have two pages first is default which is created by master page and second is login which is not created by master page. Now when default page open it is written...

"Welcome:guest".

When I click on login after successfully logging in it returns to default page written as...

"Welcome:guest" instead of "Welcome:(username)"


I'ts not coming when I click again on login button after that it comes

Welcome:(username)"


This is my coding please tell me where I am getting the error.

1)Default page
protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["cname"] != null)
        {
            Label lb = new Label();
            lb = (Label)Master.FindControl("Label2");
            lb.Text = Session["cname"].ToString();
            ImageButton ib = new ImageButton();
            ib = (ImageButton)Master.FindControl("ImageButton1");
            ib.Visible = false;
            ImageButton img = new ImageButton();
            img = (ImageButton)Master.FindControl("ImageButton2");
            img.Visible = true;

        }
    }


2)Login page
protected void imglogin_Click(object sender, ImageClickEventArgs e)
    {
        if (rb1.SelectedIndex == 1)
        {
            string uname,upas;
            uname= txtuname.Text;
            upas = txtpas.Text;
            SqlCommand cmd = con.CreateCommand();

            cmd.CommandText = "select * from customer where email="+"'"+uname+"'";

            SqlDataReader dr = cmd.ExecuteReader();

            if (dr.Read())
            {
                Session["cname"] = dr.GetString(1);

                 string script = "<script>RowDblClick1()</" + "script>";
                ScriptManager.RegisterStartupScript(this, this.GetType(), "RowDblClick1", script,

false);

            }
            else
            {
                Response.Write("<script>alert('Invalid username or password.')</script>");
            }

        }
        if (rb1.SelectedIndex == 2)
        {

            if ((txtuname.Text == "kshama") && (txtpas.Text == "123"))
            {
                Session["admin"] = "Admin";
                string script = "<script>RowDblClick1()</" + "script>";
                ScriptManager.RegisterStartupScript(this, this.GetType(), "RowDblClick1", script,

false);


            }


        }


    }


3)Master Page
protected void Page_Load(object sender, EventArgs e)
   {
       ViewState["pageurl"] = Request.CurrentExecutionFilePath;
       Label lb = new Label();
       lb= (Label)Master.FindControl("Label2");
       Session["ab"] = lb;
   }
   protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
   {
           if (Session["cname"] != null)
           {
               ImageButton1.Visible = false;
               ImageButton2.Visible = true;
               Label2.Text = Session["cname"].ToString();
               Response.Redirect(ViewState["pageurl"].ToString());
           }
           if (Session["admin"] != null)
           {
               ImageButton2.Visible = true;
               ImageButton1.Visible = false;
               Response.Redirect("admin.aspx");
           }
           else
           {
               RadWindowManager RadWindowManager2 = new RadWindowManager();
               RadWindowManager2.VisibleStatusbar = false;
               RadWindowManager2.Skin = "Sunset";
               RadWindowManager2.Behaviors = WindowBehaviors.Close;
               RadWindowManager2.Animation = WindowAnimation.FlyIn;
               RadWindow rd = new RadWindow();
               rd.ID = "a1";
               rd.VisibleOnPageLoad = true;
               rd.NavigateUrl = "logins.aspx";
               RadWindowManager2.Windows.Add(rd);
               Panel1.Controls.Add(RadWindowManager2);
           }

   }
   protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
   {
       if (Session["cname"] != null)
       {
           //ImageButton1.Visible = true;
           Session.Remove("cname");
           Response.Redirect(ViewState["pageurl"].ToString());
       }
       if (Session["admin"] != null)
       {
           //ImageButton1.Visible = true;
           Session.Remove("admin");
           Response.Redirect("default.aspx");
       }
   }
Posted
Updated 29-Jul-10 22:14pm
v2
Comments
Dalek Dave 30-Jul-10 4:14am    
Edited for Readability, Spelling, Sytax, Grammar and Code Blocking.

1 solution

The answer is not obvious, but here it is (I think):
You set the session variables using the Login Control's Button's OnClick event. This is fired after the PageLoad event which sets the Login/Logout button visibility. This is why it "fails" the first time.

To fix, either move the button visibility code to OnPreRender, move it to a new method that gets called explicitly at login.

As a final note, the code would be much easier to read with good control and variable names...

Hope this helps!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900