简体   繁体   中英

access dropdown control from the master page to the content page using asp.net

i have a master page and the content pages in the master page i have the textbox and dropdown the value in the dropdown may vary according to the content pages

eg for one content page the dropdown may contain

branchname, city, address

and let for other content page under same master page the dropdown may have values like

Contactnumber, EmailID, ........... ......... etc.....

so please help me to how can i bind that dropdown from my content page

thanks.

Please do as follows:

I have created a property in the master page and called that on the content page as:

public DropDownList SearchList
{
    get { return ddlFilterText; }
}

and on the content page:

// place here the datasource through which the  dropdown is biding

Master.SearchList.DataSource = null;

Master.SearchList.DataBind();

You could go and look for the dropdown control from your content page like this:

var yourdropdown = Page.Master.FindControl("ID of the dropdown") as DropDownList;

But be aware of that the masterpage's load event is succeeded by the page's load event so you dont double bind it.

如果下拉菜单中的值是由各个内容页面决定的,那么我的建议是将它们存储在缓存,会话或某种状态持久性机制中。

The cleanest way to accomplish this is by adding a MasterType directive to each of your content pages.

<%@ MasterType virtualpath="~/Masters/Master1.master" %>

Then from your codebehind in the content pages you can reference the Master Page controls like so:

DropDownList myDropDownList = Master.TheDropDownList;

Andreas is correct in that you need to be aware of the ASP.NET Event Life Cycle , so you actually bind the dropdown at the correct time.

It's pretty simple. You can 'find' the control from the content page.

Here's a sample where i'm binding to a DropDown control.

Master:

<asp:DropDownList runat="server" ID="ddlMaster" DataTextField="Name" DataValueField="Id" />

Content Page:

var ddlMaster = (DropDownList)Master.FindControl("ddlMaster");

ddlMaster.DataSource = dt;
ddlMaster.DataBind();

If you need to edit a textbox on the master page, you can also find that control and make changes to it from the content page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM