Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,everyone.I am going to develop web application like online shopping that means concept like that.I have lots of images in my datalist which are retrieve from database successfully.Now for further selection i put all these images in asp:hyperlink
like
XML
<asp:HyperLink id="hyperlink1"
                 ImageUrl='<%#Eval("FilePAth") %>'
                 NavigateUrl='<%# string.Format("~/ItemSelection.aspx?&FileName={0}&FilePath={1}",Eval("fileName"),Eval("FilePath")) %>'
                 Text="Microsoft Official Site" Target="_new"   runat="server"/>

In the page_load method of ItemSelection.aspx I am going to create dynamic div and image control for selected item.For selected item i used static List<string> (in App_code/Class.cs);
add this list to session varilable.After that add this session variable to image control on dynamically created DIV.

It working fine for 1st time.but after that it can't reset static variables,previous values show as it.

Plz help me to reset the static variable.
thanks.

EDIT: AL) Updated from Comments

thanks. The code in ItemSelection.aspx.cs page_load event as

C#
protected void Page_Load(object sender, EventArgs e)
{ 
  Class1 c = new Class1();

  if (Request.QueryString["FilePath"] != null)
  {
    Session["itemcount"] = Convert.ToInt32(Session["itemcount"]) + 1;
    int count = Convert.ToInt32(Session["itemcount"].ToString());
    //here label2 is used for no.of item selected
    label2.Text = count.ToString();
    //list2 is used for store selected item 
    List list2 = new List();

    //Response.Write("count=" + count);
    //Here we add selected item in list2 

    list2.Add(Request.QueryString["FilePath"]);

    // then i added this list into static list created in class under app_code/class

    c.setArr(list2);
    Session["itemList"]= c.getArr();

    //If item selected then only i have created dynamic div with image control 
    if (count > 0)
    {
      for (int i = 0; i < count; i++)
      {
        HtmlGenericControl div1 = new HtmlGenericControl("div");

        string j = (i + 1).ToString();
        div1.Attributes.Add("id", "j");
        //Response.Write(div1.Attributes.ToString());
        div1.Attributes.Add("class", "top_rounded");
        Image img = new Image();

        //List a = c.listItem;
        List a = (List)Session["itemList"]; img.ImageUrl = a[i];
        div1.Controls.Add(img);
        Panel1.Controls.Add(div1);
     }
  }
Posted
Updated 25-Aug-15 0:42am
v3
Comments
Andy Lanng 25-Aug-15 5:15am    
If you don't want the values to stay as they are then why are they static?
Please post the code relevant to the question
SujataJK 25-Aug-15 6:30am    
thanks. The code in ItemSelection.aspx.cs page_load event as

protected void Page_Load(object sender, EventArgs e)
{
Class1 c = new Class1();

if (Request.QueryString["FilePath"] != null)
{
Session["itemcount"] = Convert.ToInt32(Session["itemcount"]) + 1;
int count = Convert.ToInt32(Session["itemcount"].ToString());
//here label2 is used for no.of item selected
label2.Text = count.ToString();
//list2 is used for store selected item
List<string> list2 = new List<string>();

//Response.Write("count=" + count);
//Here we add selected item in list2

list2.Add(Request.QueryString["FilePath"]);

// then i added this list into static list created in class under app_code/class

c.setArr(list2);
Session["itemList"]= c.getArr();

//If item selected then only i have created dynamic div with image control
if (count > 0)
{
for (int i = 0; i < count; i++)
{
HtmlGenericControl div1 = new HtmlGenericControl("div");

string j = (i + 1).ToString();
div1.Attributes.Add("id", "j");
//Response.Write(div1.Attributes.ToString());
div1.Attributes.Add("class", "top_rounded");
Image img = new Image();

//List<string> a = c.listItem;
List<string> a = (List<string>)Session["itemList"]; img.ImageUrl = a[i];
div1.Controls.Add(img);
Panel1.Controls.Add(div1);
}
}
Andy Lanng 25-Aug-15 6:42am    
You too, have to power to update the question :P

Ok - You do not want that list to be static. Just take the static keywork off.

Class1 c (aweful class name, btw) will retain its non-static values for its whole life. Its life is dictated by its scope. Consider these level of scope and the examples.

If you declare an instance within an if, for loop, using etc. then it's scope is that section. As soon as the section is closed then it is no longer available. It's dead.
C#
int x;
for(int y = 0; y < 10; y++){x=y;}
int a = x; //fine;
a = y; //compile error.  y doesn't exist anymore


If you declare an instance within a method, then it's life ends when that method exits. This is the scope you are using. It appears that the list is not being used anywhere else other that in the method so this is the appropriate scope.
C#
int x;
void method(){
    int y = 10;
    x = y;
}
void Main(){
    method();
    int a = x; //fine
    a = y;  //compile error.  y doesn't exist anymore
}


If you declare an instance in the class then it dies when the class dies. This happens after the class itself has left scope.
C#
class MyClass{ public int y {get;set} }
void method(){
  int x = 10;
  using(myClass = new MyClass())
  {
     myClass.y = x;
     int a = myClass.y;
  }
  int b = x;  //fine
  b = myClass.y //Well, myClass doesn't exist anymore so myClass.y doesn't either.
}


If you declare an instance as static then it will not die until the entire application dies. It's scope is maximal.

C#
class MyClass{ public static int y = 10; }
void method(){
  int x = MyClass.y; // fine
  //We don't even need an instance of MyClass to find y.  It will always be there and there is always 1 instance of it no matter how many MyClass objects are instantiated.
}



Take a look at these 4 levels of scope. Think about how your instance is being used. Does it really need to be static? Can it instead be in the method or class scope? Do you need Class1 at all, or could you just use a List variable in one of these scopes instead?
 
Share this answer
 
Comments
SujataJK 25-Aug-15 7:50am    
thanx.
here i want to collect item from several webpages and add in single list.For that i uesd static list in app_code/Class.cs .I got correct list but when my application exit then my list is still as it is.I want to reset my static list.
Andy Lanng 25-Aug-15 7:59am    
Ah, ok. That is a more complex issue, but I'm sure there are better solutions.

You say "when my application exit". What do you mean? Closing the browser?
you say "collect item from several webpages". How do you collect these pages? Is it the pages the user has open?
SujataJK 25-Aug-15 8:24am    
application exit means browser close.
collect item from several webpages means take example of online shopping like that i have to add several item of different categories in single list.
Andy Lanng 25-Aug-15 8:43am    
Ah, perfect! What you need are "Session variables"
These variables live only as long as the browser session is alive. They also only refer to one user whereas static would apply to all users. They would in effect all be adding to each others basket :P

You are correct to accept the above solution as it did answer your question as stated, But your requirement is different. I will add another solution to explain Session scope. You can accept that one as well :D
Maciej Los 25-Aug-15 7:58am    
Well explained, 5!
I'm not really sure what you're doing, but your use of static variables is probably incorrect. Static variables are "global" across all users and they are initialised on first use and exist until the site is restarted. You need to think carefully about if you really need to use static variables or not, and if you do you need to keep in mind their global nature, think about thread safety issues etc etc.
 
Share this answer
 
This solution answers the OPs requirements, which differ from the question asked. I do this in the hope that someone who has the same issue will find it.


There is another type of scope that was not mentioned in the above. This is session scope and is specific to web applications.

Any user on the website will be given a session id. This is usually kept in the form of a cookie on the clients machine. This session ID relates to a session object that can be used at the server side to identify the user. Once the user exist the website, this session expires. If the same user comes back then they will get a new session Id relating to a new session.

NB: if a user has the website open in several browsers (i.e. Chrome and IE) or has it open in normal and incognito mode, then each of these will have individual sessions.

So: How to use them? Dead easy:
In your code-behind you can access the users session via the SessionState object called simple "Session". You don't have to worry about wiring it up to the users session. That all gets done for you. Observe:
C#
public void Page_Load(blah){
  if(Session["Basket"]==null)
    Session["Basket"] = new List<items>();
}
public void addToBasket(item){
    //Stored as object so you have to cast to a variable
    List<items> basket = (List<items>)Session["Basket"];
    basket.Add(item);
    Session["Basket"] = basket;
}
</items></items></items>



When the user leaves the website then Session and all of it's values for that session die.

How's that? ^_^
 
Share this answer
 
Comments
SujataJK 26-Aug-15 1:36am    
I think this will be helpful for me.Let me try.
thanks
SujataJK 26-Aug-15 2:07am    
sir,In your code snippet what is items.is it class or xml object
Andy Lanng 26-Aug-15 3:48am    
it's whatever. You can store anything. Whatever your static object was, use that

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