asp.net C#中的共享变量 [英] shared variable in asp.net C#

查看:85
本文介绍了asp.net C#中的共享变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友,

在我的网站中,我想要一个共享变量,在其中存储任何值,并且该值应可供该网站的所有网页访问.

我想在登录时为该共享变量分配一个值,并且每当请求新页面时,我都会将查询字符串中的该变量值发送到所请求的页面.

在这里,我可以使用会话来做到这一点,但我不想使用会话和cookie.

有人可以指导我如何解决同样的问题.

提前谢谢.
Shailesh J.

解决方案

您会发现许多CodeProject文章和其他文章将对此进行解释.请参阅 http://www.codeproject.com/search. aspx?q = store + in + session + ASP.NET& sbo = kw [ <%@ 母版 语言 =" C#" ClassName MyMaster" AutoEventWireup =" CodeFile =" 继承 =" MainMaster" %> < !DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 过渡//EN" " http://www.w3. org/TR/xhtml1/DTD/xhtml1-transitional.dtd" < html =" http://www.w3.org/1999/xhtml" > < runat =" > < 标题 > < /title > < 链接 runat =" rel =" 快捷方式图标" href =" 类型 =" / >> < 链接 runat =" rel =" 图标" href =" 类型 =" / > < asp:ContentPlaceHolder ID =" runat 服务器" < /asp:ContentPlaceHolder > < /head > < 正文 > < 表单 =" form1" =" 服务器" < div > < div =" Header" 样式 宽度:100 %" < div =" 标题" < /div > < div > < asp:Label ID =" runat 服务器" < > < /div > < /div > < asp:ContentPlaceHolder ID =" runat 服务器" < /asp:ContentPlaceHolder > < /div > < /form > < /body > < /html >



第2步:在页面后面的代码中,将page_load事件中的值设置为上面的标签或文本框,如下所示

 ...
受保护的 无效 Page_Load(对象发​​件人,EventArgs e)
    {
       Query.Text = " ;
    }
... 



附加步骤:现在,如果要将数据作为查询字符串添加到每个页面请求中,可以将以下代码添加到母版页的page_load事件中.

 ...
受保护的 无效 Page_Load(对象发​​件人,EventArgs e)
    {
       Query.Text = " ;
       如果(!IsPostBack)
       {
          validatePath( this  .Request.Url.AbsolutePath);
       }
    }

    私有 无效 validatePath(字符串 url)
    {
       Response.Redirect(URL + "  + Query.Text);
    }
... 


Hello friends,

In my web site i want a shared variable in which i stores any value and that value should be accessible to all web pages of that web site.

I want to assign a value to that shared variable at the time of login and whenever new page is requested i will send that variable value in a query string to the requested page.

Here i can do it by using sessions but i dont want to use sessions and cookies.

Can anybody guide me how to solve the same.

Thanks in advance.
Shailesh J.

You will find a number of CodeProject articles and other posts which will explain you that. Please see http://www.codeproject.com/search.aspx?q=store+in+session+ASP.NET&sbo=kw[^].

—SA


you can use static class with static variables for storing and retrieving values.But keep in mind these variables are shared among your whole site.


Step 1: Add a MasterPage with asp:label or asp:textbox like shown below..

<%@ Master Language="C#" ClassName="MyMaster" AutoEventWireup="true" CodeFile="MainMaster.master.cs" Inherits="MainMaster" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head  runat="server">
    <title></title>
    <link  runat="server" rel="shortcut icon" href="~/Images/favicon.ico" type="image/x-icon" />
    <link  runat="server" rel="icon" href="~/Images/favicon.ico" type="image/ico" />
    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1"  runat="server">
    <div>
        <div class="Header" style="width: 100%">
            <div class="Title">
                Test
            </div>
            <div>
                <asp:Label ID="Query" runat="server"></asp:Label>
            </div>
        </div>        
        <asp:ContentPlaceHolder ID="BodyContentPlaceHolder" runat="server">
        </asp:ContentPlaceHolder>        
    </div>
    </form>
</body>
</html>



Step 2: In the code behind page set values to above labels or textboxes in the page_load event as shown below

...
protected void Page_Load(object sender, EventArgs e)
    {  
       Query.Text = "Your Text";
    }
...



Step Additional: Now if you want to add the data to each page request as querystring you may add the following code to the page_load event of the masterpage..

...
protected void Page_Load(object sender, EventArgs e)
    {  
       Query.Text = "Your Text";
       if (!IsPostBack)
       {  
          validatePath(this.Request.Url.AbsolutePath);
       }
    }

    private void validatePath(string url)
    {
       Response.Redirect(url + "?query=" + Query.Text);
    }
...


这篇关于asp.net C#中的共享变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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