开发者

control sessions in application asp.net/c# [closed]

开发者 https://www.devze.com 2023-03-18 23:54 出处:网络
开发者_如何学编程 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current for
开发者_如何学编程 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

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.";
}
0

精彩评论

暂无评论...
验证码 换一张
取 消