Hi All

I have a dropdownlist in one page, now I want to display the contents of that dropdownlist in a different page, can someone help me with this.

Thanks in advance

Dani AI

Generated

There are several safe ways to show a DropDownList from one ASP.NET WebForms page on another. 's suggestion to copy items will work, but it couples pages to UI controls and the example posted has an indexing bug (use Items[i] or iterate the collection, not IndexOf(i)). Safer patterns are: expose the dropdown data (not the control) as a simple collection, bind both pages to the same data source, or pass the data via session/context when needed.

Expose data-from-source and consume on target (works with cross-page post or PreviousPage). Example pattern (C# 2.0 compatible):

public List<KeyValuePair<string,string>> DdlItems
{
    get
    {
        List<KeyValuePair<string,string>> list = new List<KeyValuePair<string,string>>();
        foreach (ListItem li in DropDownList1.Items)
            list.Add(new KeyValuePair<string,string>(li.Text, li.Value));
        return list;
    }
}

Then on the receiving page:

if (PreviousPage != null)
{
    SourcePage src = PreviousPage as SourcePage; // replace with your code-behind class
    if (src != null)
    {
        foreach (KeyValuePair<string,string> kv in src.DdlItems)
            DropDownListTarget.Items.Add(new ListItem(kv.Key, kv.Value));
    }
}

Or bind both pages to the same helper/data source (best for large lists and maintainability):

if (!IsPostBack)
{
    DropDownList1.DataSource = DataHelpers.GetDropdownData(); // DataTable or List of objects
    DropDownList1.DataTextField = "Text";
    DropDownList1.DataValueField = "Value";
    DropDownList1.DataBind();
}

Troubleshooting & tips: avoid making controls public; expose plain data instead. Check PreviousPage for null and use @PreviousPageType or cast to your source page class. For large or persistent lists prefer shared data (DB/Cache/Session) rather than copying UI items. If the goal is only to show the selected value, pass the value/index via querystring or session instead.

Recommended Answers

All 3 Replies

'page' as in form?

if so, declare the drop-down list yourself and add a public access modifier to it.

in the new form, go through the index of the original list with a for-loop and populate the new one.

hope this helped
pygmalion

Its a website i created, using asp.net 2.0 and c#, Can you please send me the code for this. Just the line of dispalying the items in another web page.

Thanks,

well, in c#, that would do:

for(int i = 0; i < amountItems; i++)
{
    newDropdown.Items.Add(oldDropdown.Items.IndexOf(i);
}

hope this helped
pygmalion

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.