control sessions in application asp.net/c# [closed]
I need to control sessions in my application for example; when user typed sam in text box, the next user can not type sam as sam already in use! Any idea?
Application["UserName"] = user.Text;
if (Application["UserName"] == "sam")
{
}
A Session is specific to the current user. If another user accesses your site he will not be able to read the session values of other users. So you would be better of storing this information into the Application state which is shared between all users. The documentation contains many examples.
I will answer according to what i understood .it 's not that clear question.
Firstly : as said before the session
is per client
.so this not the answer ..
I make a sample :
aspx:
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txt_name" runat="server" ontextchanged="txt_name_TextChanged"></asp:TextBox>
<asp:Label ID="lbl_message" runat="server"></asp:Label>
</div>
</form>
</body>
.cs:
public partial class _Default : System.Web.UI.Page
{
static int i =0;
protected void Page_Load(object sender, EventArgs e)
{
if (i > 0 && txt_name.Text == "sam")
{
txt_name.Enabled = false;
txt_name.Text = string.Empty;
lbl_message.Text = "In use";
}
else
{
txt_name.Enabled = true;
lbl_message.Text = string.Empty;
}
}
protected void txt_name_TextChanged(object sender, EventArgs e)
{
if (txt_name.Text == "sam")
{
i++;
}
}
}
you can use Application variable instead of static variable as said before.
You have to use an Application level variable instead of a Session variable,
because an application variable value will persist across the user . e.g.
Application["UserName"] = user.Text;
if (Application["UserName"] == "sam")
{
labelMessage.Text = "This user has been already selected.";
}
精彩评论