c# - transfer login information to different pages using Session asp.net -
i'm working in visual studios 2013 , making simple website project in school. i'm using c# asp.net. have sign form works , login bootstrap modal. want when user logs in username written on navbar, in form of "hello, 'username' ".
i wrote in masterpage:
<div id="sessionuser" runat="server" style="color:black; font-size: 25px; position: fixed; right: 350px; top: 15px;"></div>
the login modal in masterpage , c# code:
//login if (request.form["submitlogin"] != null) { string usernamel = request.form["usernamelogin"]; string passwordl = request.form["passwordlogin"]; string sqll; sqll = "select * users username='" + usernamel + "' , password='" + passwordl + "'"; if (eitan.isexist(filename, sqll)) { session["user"] = usernamel; if (session["user"] != null) { string loginmsg = session["user"] string; sessionuser.innerhtml = "hello, " + loginmsg; } } else { response.write("<script>alert('this user not exist!');</script>"); } }
the class 'eitan' contains simple functions interacting sql database. 'eitan.isexist' function is:
public static bool isexist(string filename, string sql) { sqlconnection conn = connecttodb(filename); conn.open(); sqlcommand com = new sqlcommand(sql, conn); sqldatareader data = com.executereader(); bool found; found = (bool)data.read();// אם יש נתונים לקריאה יושם אמת אחרת שקר - הערך קיים במסד הנתונים conn.close(); return found; }
if logs in incorrrect information there alert user doesnt exist. when log in correct information username written on navbar ('hello, username'), when navigate different page message disappears. how can fix this?
sorry if isn't written or not understandable.. i'm beginner :)
you have username in session, need in code behind of master page pretty same thing setting contents of element value stored in session.
so (excuse me if method below isn't 100% correct, find onload method master page):
public override pageload() { if (session["user"] != null) { string loginmsg = session["user"] string; sessionuser.innerhtml = "hello, " + loginmsg; } }
Comments
Post a Comment