login,logout on every page

Asked By goldy gupta
30-Jul-10 03:23 AM
Earn up to 0 extra points for answering this tough question.

hello to all i am making an asp.net web application.. i wants user to give 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 login it returns to default page written as "Welcome:guest" it should

come "Welcome:(username)",its not coming when i click again on login button after that it comes

Welcome:(username)"...This is my coding plz tell me where i am geting error..

!.)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");
      }
    }

  re: login,logout on every page

Indu P replied to goldy gupta
30-Jul-10 07:11 AM
try giving 

  if (Session["cname"].ToString() != null||Session["cname"].ToString() != "")
    {
}

instead of if (Session["cname"]!= null)

I think this will solve the issue.

Create New Account