ASP.NET - login,logout on every page

Asked By goldy gupta
30-Jul-10 03:23 AM

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

  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
help
alot var Hi What are Master Pages in ASP.NET? or What is a Master Page? ASP.NET master pages allow you to create a consistent layout for the pages in your application. A single master page defines the look and feel and standard behavior that you want for all of the you want to display. When users request the content pages, they merge with the master page to produce output that combines the layout of the master page with the content from the content page. What are the 2 important parts of a master page? The following are the 2 important parts of a master page 1. The Master Page itself 2. One or more Content Pages Can Master Pages be nested? Yes, Master Pages
Page Directive? What is the purpose of page directive in aspx page? Directive Syntax Directives are instructions used to specify settings (related to how a page should render and processed) used by the page and user control compilers when they process ASP.NET Web Forms page (.aspx) and user control (.ascx) files. These are the essential part of every ASP.NET Page or Control. Directives can be located anywhere in an .aspx or .ascx file, but the values, same as any HTML tag) that are specific to that directive. Special Note: The @ Page directive can be used only in .aspx files, and the @ Control directive can be used
access master page control on noncontent aspx page hello to all i have a master page(masterpage.master) and a simple aspx(login.aspx) page that is not created by master page and now i want to access control of master page on simple aspx page.how can i? No you cannot access the master page control in non-context pages, though you can read in content pages. One way would
image cannot be download from datalist in website for my page of photo, when i right click to save image but they say it has been userr") %> ' / > < br / > < / ItemTemplate > < / asp : DataList > codes behind protected void Page_Load( object sender, EventArgs e) { if (!Page.IsPostBack) { SqlConnection conn = new SqlConnection ( "Data Source = EN12-2-24-WS07 \ SQLEXPRESS; Initial Catalog = SchWeb Data. SqlDbType .NVarChar); ImageID.Value = context.Request.QueryString[ "userr" ]; cmd.Parameters.Add(ImageID); con.Open(); SqlDataReader dReader = cmd.ExecuteReader(); dReader.Read(); context.Response.BinaryWrite(( byte [])dReader[ "pict" ]); dReader.Close(); con.Close(); } Follow these steps- In ASPX Page. < asp : TemplateField HeaderText = "Download" meta : resourcekey = "TemplateFieldResource5"> < ItemStyle HorizontalAlign = "Center" / > < ItemTemplate > < asp ImageButton ID = "ImageButton1" runat = "server" CommandArgument = ' <%# Eval("Picture") %> ' ImageUrl = "~ / images / DownLoads.gif" OnClick = "ImageButton1_Click" CausesValidation = "False Length . ToString ()); Response . ContentType = "application / octet-stream" ; Response . WriteFile ( _file . FullName ); Response . End (); } else { ClientScript . RegisterStartupScript ( Type . GetType ( "System.String" ), "messagebox" , "&lt;script type = \ " text / javascript \ "&gt;alert('Sorry no image found');< / script> " ); } } protected void ImageButton1_Click ( object sender , ImageClickEventArgs e ) { string _Name = (( ImageButton ) sender ). CommandArgument ; DownloadFile ( _Name ); } TRy this and let me know. for the aspx page code