Hi all

I have a DropDownList in in one of my web application's Page, now i want to display the contents of that DropDownList in another Page. Can someone please show me the code for this in C#. When i try to use the DropDownList in this other page it doesn't recognise it because the DropDownList is not in that Page, it is a different page. please help...

Thanks!

Dani AI

Generated

Short answer: do not try to access a DropDownList control instance that lives on PageA from PageB. Either move the UI into a shared UserControl, share the same data-binding code (or cache the data), or pass just the selected value between pages. As suggested, a UserControl is the cleanest reusable approach. and pointed at Session-based approaches — those work but have trade-offs (memory, per-user scope, and lifecycle).

Example: put the DropDownList in a UserControl and expose a simple API so any page can bind or read the selection.

UserControl markup (MyList.ascx):

<asp:DropDownList ID="ddlItems" runat="server" />

UserControl code-behind (MyList.ascx.cs):

public void Bind(DataTable dt)
{
    ddlItems.DataSource = dt;
    ddlItems.DataTextField = "Name";
    ddlItems.DataValueField = "Id";
    ddlItems.DataBind();
}

public string SelectedValue => ddlItems.SelectedValue;

Include and use on any page:

<%@ Register TagPrefix="uc" TagName="MyList" Src="~/Controls/MyList.ascx" %>
<uc:MyList ID="myList" runat="server" />
// then in Page_Load: myList.Bind(GetLookupTable());

Quick alternatives (when you only need the selected item): pass the value via QueryString, Session, or cross-page postback. Example Session storage:

Session["MyDDL"] = dataTable;            // page A
var dt = Session["MyDDL"] as DataTable;  // page B
if (dt != null) { otherDdl.DataSource = dt; otherDdl.DataBind(); }

Notes and cautions: avoid storing large datasets in Session for many users; prefer Cache or a shared data-access method that both pages call. For single-value passing, QueryString or Server.Transfer/PreviousPage with a public property are simpler and safer than trying to grab a control instance across pages.

Recommended Answers

All 4 Replies

put ur dropdownlist in user control. Then u just need to include the user control on any place or any page when u need to use the dropdownlist.

put ur dropdownlist in user control. Then u just need to include the user control on any place or any page when u need to use the dropdownlist.

Can you please explain more about this user control or give me a example. I don't really understand it.

easier way out would be store the datalist contents in probably a datatable and save to the session variable and read the session variable, type cast to datatable and get the contents.

hi,
it can be written using session.first we create a session for the selected item in the dropdownlist and redirect to next page.
In the newxt page we retrieve this session and use it for the specified purpose using convert option
hope this helps
bye

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.