0

I'm new to this and have already searched for this with not much luck :(

Lets say I have defined a struct array globally and filled the array with data on an Ajax ModalPopupExtender. I then have a ddl_SelectedIndexChanged event that does a postback and seems to recycle my array.

Is there a way to fire the ddl_SelectedIndexChanged event to perform some code without doing a postback? Or is there an easy way to make the array of type struct retain it's values?

(I am creating a website btw)

Thanks in advance...

Bouwer
  • 1
  • 1
  • 2
  • You could save the Array in a **session** object, or if it's page level, maybe use **ViewState** - assuming this is Webforms? – Christian Phillips Jun 03 '14 at 09:18
  • What do you mean by 'defined globally'? And you should tag it properly, because you seem to be talking about Ajax calls so I'm guessing you're creating a website. `Show us some code: !important;` – Silvermind Jun 03 '14 at 09:19
  • @christiandev - Yea, Webforms. I though of saving it as a session variable but it will be quick a large number of variables. I am not quite familiar with ViewState but will look it up. – Bouwer Jun 03 '14 at 09:20

4 Answers4

2

To store in session...

Session["ArrayName"] = ArrayVariable;

..to get it back into the Array type

var array = (ArrayType)Session["ArrayName"];

With viewState

ViewState.Add("ArrayName", ArrayVariable);
var array= (ArrayType)ViewState["ArrayName"];

What is the difference between SessionState and ViewState?

If this is to be used only for the page you are on, then you should use ViewState. If you need it to be available across the site, then your options are Session, Cache or DB. Also, make sure you first check that the objects are available, as in..

if (Session["ArrayName"] != null)
   var array = (ArrayType)Session["ArrayName"];

..or you will be victim to the Object Not Set Reference error.

See this article for using Cache in ASP.NET.

Community
  • 1
  • 1
Christian Phillips
  • 18,399
  • 8
  • 53
  • 82
1

You could store your array in ViewState, like below:

ViewState["arrayName"] = array;

where array is the name of your array. Then you could easily retrieve it using ViewState["arrayName"]. If you array is an array of strings, you could easily retrieve it like:

string[] arr = ViewState["arrayName"] as string[];

For further documenation on ViewState please have a look here.

Christos
  • 53,228
  • 8
  • 76
  • 108
0

If it is page level then go for viewstate

ViewState["arrayName"] = array;

and if it is to different page then it is through session.

Session["ArrayName"] = ArrayVariable;
Sunil
  • 150
  • 7
0

As said already use ViewState. Create a property in the page's class and read/write to that property. Obviously change the List to what ever type your array is.

public List<string> SomeArray
{
    get
    {
        if (ViewState["SomeArray"] != null)
        {
            return (List<string>)ViewState["SomeArray"];
        }
        return null;
    }
    set { ViewState["SomeArray"] = value; }
 }

Really though when you bind values to a dropdown the dropdown should hold the data in viewstate itself unless your logic clears on page load. Posting your Page_Load event may identify your real issue.

UPDATE Although having read your question again I could be wrong, post code!

matt_lethargic
  • 2,706
  • 1
  • 18
  • 33