在每个页面上登录和注销 [英] login and logout on each page

查看:100
本文介绍了在每个页面上登录和注销的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好.

我正在制作一个asp.net Web应用程序,我想为用户提供在每个页面上登录和注销的功能.

我在母版页上使用了两个图像按钮,一个是登录,另一个是注销.

我有两个页面,第一个是默认值,它是由母版页创建的;第二个是登录名,它不是由母版页创建的.现在,当默认页面打开时,将写入...

欢迎:来宾" .

当我成功登录后单击登录时,它将返回默认页面,写为...

"Welcome:guest" 而不是"Welcome:(username)"


登陆后再次单击登录按钮,我就不会来了

欢迎使用:(用户名)"


这是我的编码,请告诉我哪里出现错误.

1)默认页面

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)登录页面



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)母版页



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

推荐答案

答案不是很明显,但这是(我认为):
您可以使用登录控件的按钮的OnClick事件设置会话变量.这是在PageLoad事件后之后触发的,该事件设置了Login/Logout按钮的可见性.这就是为什么它第一次失败"的原因.
要解决此问题,请将按钮可见性代码移动到OnPreRender,然后将其移动到在登录时被显式调用的新方法.

最后要注意的是,具有良好的控制和变量名的代码将更易于阅读...

希望这会有所帮助!
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!


这篇关于在每个页面上登录和注销的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆