Click here to Skip to main content
16,012,107 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to pass default1.aspx listboxitems to default2.aspx in asp.net using c# code
(I need code for this)
Posted

http://msdn.microsoft.com/en-us/library/6c3yckfw.aspx[^]

This should answer ur question!
 
Share this answer
 
try this code:

page_1.aspx :coding and explains:


first make one listbox and add some values to an listbox(go to property window and select items and give collection of values in your listbox)



second create one button to pass value in next page.
now try this code:



protected void Button1_Click(object sender, EventArgs e)
{
        int n=ListBox1.Items.Count;
        string[] arr=new string[n];
        for (int i = 0; i < arr.Length; i++)
        {
            arr[i] = ListBox1.Items[i].ToString();
        }
        Session["arr"] = arr;
        Response.Redirect("Default2.aspx");
}

here i first i find the how many items are have in listbox. so use items count.
second i make one string array and store that listbox values in string array.
third i make one session variable and pass that array value to session. now i navigate next page.


now try to get this listbox values in second page.

Second_page.aspx
-----------------

first i make listbox. and try this code.



coding:
-------
string[] arr;
    protected void Page_Load(object sender, EventArgs e)
    {
        arr = (string[])Session["arr"];
        for (int i = 0; i < arr.Length; i++)
        {
            ListBox1.Items.Add(arr[i].ToString());
        }
    }


here i declare one array and get that session value from that array. and i add the listbox.



i hope this is useful to you.

take care bye bye...
 
Share this answer
 
v3
Comments
RaisKazi 19-Aug-11 2:57am    
Formatted answer with <pre> tag.
Dalek Dave 19-Aug-11 3:41am    
Good Call.
1. Setting the PostBackUrl property of the Button to the default.aspx, as follows:-


<asp:button id="button1" runat="server" text="submit" postbackurl="~/default2.aspx" xmlns:asp="#unknown" />


2. Then, in the default2.aspx.cs, you can access the ListBox control on Default.aspx as follows:-

public void page_load()
{
if(!IsPostBack)
{
Listbox list= (Listbox)PreviousPage.FindControl("Listbox1");
foreach (ListItem item in Listbox1.Items)
{
 Respond.Write(item.value.Tostring());
}

}
 
Share this answer
 
v2
Comments
Dalek Dave 19-Aug-11 3:41am    
Edited for Code Block.

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