Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.
Posted
Comments
Sergey Alexandrovich Kryukov 30-Jan-12 3:46am    
What seems to be a problem?
--SA
Sergey Alexandrovich Kryukov 30-Jan-12 3:49am    
Do you look at CodeProject Questions & Answers only, nowhere else? :-)
--SA
saj_21 30-Jan-12 3:49am    
Problem is where i should declare that variable so that it should be shared to all pages.
saj_21 30-Jan-12 3:51am    
ya i searched but i didn't found same kind of question or any discussion. :-(

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
 
Share this answer
 
Comments
saj_21 30-Jan-12 3:54am    
Thanks dr friend.

But i was looking for a solution except use of Sessions and Cookies.

I mean is it possible to do the same without use of Sessions and Cookies.????
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.
 
Share this answer
 
Comments
saj_21 30-Jan-12 4:11am    
It means once i initialize that variable in page load it will be initialize for all the peoples accessing my web site???
Nasir M@hmood 30-Jan-12 23:31pm    
yes within domain of your application.
Step 1: Add a MasterPage with asp:label or asp:textbox like shown below..

ASP.NET
<%@ 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

C#
...
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..
C#
...
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);
    }
...
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900