I am working on a project which contains a master page. On that master page, I have buttons (Home, About) and Login drop-down control. After login I want to show login username in dropdown list control and log out at the same time.
Register user
string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(cs))
{
SqlCommand cmd = new SqlCommand("spRegisterUser", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter Name = new SqlParameter("@Name", txtName.Text);
SqlParameter Email = new SqlParameter("@Email", txtEmail.Text);
string encryptedPassword = FormsAuthentication.HashPasswordForStoringInConfigFile(txtPassword.Text, "SHA1");
SqlParameter password = new SqlParameter("@Password", encryptedPassword);
SqlParameter Mobile = new SqlParameter("@MobileNo", txtMobile.Text);
cmd.Parameters.Add(Name);
cmd.Parameters.Add(Email);
cmd.Parameters.Add(password);
cmd.Parameters.Add(Mobile);
con.Open();
int ReturnCode = (int)cmd.ExecuteScalar();
if (ReturnCode == -1)
{
lblmessage.ForeColor = System.Drawing.Color.Red;
lblmessage.Text = "Email Already In Use";
}
else
{
SendEmail();
Response.Redirect("https://localhost/JustEat.com/Login.aspx");
}
}
dropdown list Control at Home.Master
<asp:DropDownList ID="ddlLogin" CssClass="btn" runat="server"
AutoPostBack="True" OnSelectedIndexChanged="ddlLogin_SelectedIndexChanged">
<asp:ListItem Value="-1">User</asp:ListItem>
<asp:ListItem Value="1">SignUp</asp:ListItem>
<asp:ListItem Value="2">Login</asp:ListItem>
</asp:DropDownList>
Home.Master.aspx.cs
protected void ddlLogin_SelectedIndexChanged(object sender, EventArgs e)
{
if (ddlLogin.SelectedValue == "-1")
{
}
else if (ddlLogin.SelectedValue == "1")
{
Response.Redirect("https://localhost/JustEat.com/Signup.aspx");
}
else if (ddlLogin.SelectedValue == "2")
{
Response.Redirect("https://localhost/JustEat.com/Login.aspx");
}
}
Method to Authenticate User
private bool AuthenticateUser(string username, string password)
{
string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
SqlCommand cmd = new SqlCommand("spAuthenticateUser", con);
cmd.CommandType = CommandType.StoredProcedure;
string EncryptedPassword = FormsAuthentication.HashPasswordForStoringInConfigFile(password, "SHA1");
SqlParameter paramEmail = new SqlParameter("@Email", username);
SqlParameter paramPassword = new SqlParameter("@Password", EncryptedPassword);
cmd.Parameters.Add(paramEmail);
cmd.Parameters.Add(paramPassword);
con.Open();
int ReturnCode = (int)cmd.ExecuteScalar();
return ReturnCode == 1;
}
}
Login Button Click Event
if (AuthenticateUser(txtEmail.Text, txtPassword.Text))
{
Session["UserName"] = txtEmail.Text;
Response.Redirect("https://localhost/JustEat.com/Home.aspx");
}
else
{
lblmessage.ForeColor = System.Drawing.Color.Red;
lblmessage.Text = "Invalid User Name and/or Password";
}
After User Entered their Credentials
if (Session["UserName"] != null)
{
string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(cs))
{
SqlDataAdapter da = new SqlDataAdapter("Select * from tblRegistration where Email='" + Session["UserName"] + "'", con);
DataSet ds = new DataSet();
da.Fill(ds);
ddlLogin.Items.Clear();
ddlLogin.Items.Add(ds.Tables[0].Rows[0]["Name"].ToString());
ddlLogin.Items.Add(new ListItem() { Value = "Log", Text = "Log out" });
}
}
now the problem is that how can i make ddlLogin.Items.Add(new ListItem() { Value = "Log", Text = "Log out" }); this clickable.....
You can put this in the Page_Load of the master page. When the request is authenticated and there is no PostBack it clears the current items in the DropDownList and adds new ones.
if (Request.IsAuthenticated && !IsPostBack)
{
ddlLogin.Items.Clear();
ddlLogin.Items.Add(new ListItem() { Value = "-1", Text = "UserName" });
ddlLogin.Items.Add(new ListItem() { Value = "1", Text = "Log out" });
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.