Saturday, December 8, 2012

How to pass an array in a Query String?

Today I am going to give an example of passing an array in a query string. We have two pages one is Sender.aspx and second is Receiver.aspx. We will create the ArrayList in Sender.aspx and sent it to Receiver.aspx.



Code in Sender.aspx

//Create instance of ArrayList
ArrayList arrTest = new ArrayList();

//Add element into arraylist
arrTest.Add("1");
arrTest.Add("2");
arrTest.Add("3");

//create query string value using join method String class
string strQueyString = String.Join("||", ((string[])arrTest.ToArray(typeof(String))));
           
//sending arrayliat values to Reciver.aspx
Response.Redirect("Receiver.aspx?param=" + strQueyString); 


Code in Receiver.aspx

//get the value of query string
string[] param = Request["param"].ToString().Split('|');
//create array list
ArrayList arrTest = new ArrayList();
foreach (string str in arrTest)
{
    arrTest.Add(str);
}


Note: - You have to ensure that your array string's length doesn't exceed the permissible maximum length of query string.

Happy coding!!

No comments:

Post a Comment

^ Scroll to Top