Thomas
Thomas

Reputation: 34188

Master page and content page communication in asp.net

suppose i have one dowpdown in master page and i want that when user select any item from that dropdown then a postback will occur and the selected item text will be dispalyed in the label of content page. please help me with sample code.

thanks

Upvotes: 3

Views: 2758

Answers (4)

Pindi
Pindi

Reputation: 31

On the Master Page:

<asp:DropDownList ID="someDropDown" runat="server" AutoPostBack="True">
<asp:ListItem Text="Bob" Value="Bob"></asp:ListItem>
<asp:ListItem Text="John" Value="John"></asp:ListItem>
<asp:ListItem Text="Mark" Value="Mark"></asp:ListItem>
</asp:DropDownList>

On any other page in the aspx:

<asp:Label ID="userLabel" runat="server"/>

On any other page, in the codebehind:

protected void Page_Load(object sender, EventArgs e)
    {
        DropDownList thisDropDown = this.Master.FindControl("someDropDown") as DropDownList;
        userLabel.Text = thisDropDown.SelectedValue;
    }

Upvotes: 3

Luke
Luke

Reputation: 8407

Just one sample

MasterPageCode:

Public Class MyMasterPage
inherits Page (or MasterPage?)

public readonly property MyDropDown as DropDown
end Property

End Class

Page COde

Public Class MyContentPage
inherits Page

Public Overrides Sub OnLoad
       dim drop as DropDown = CType(Me.MasterPage, MyMasterPage).MyDropDown
       AddHandler drop.SelectedIndexChanged, AddressOf someprocedure
End Sub

End Class

Upvotes: 2

Marat Faskhiev
Marat Faskhiev

Reputation: 1302

You should add the following directive to content page:

<%@ MasterType VirtualPath="path to master page" %>

Add public property ih master page code-behind file:

public DropDownList DropDownList
{
    get { return dropDownList; }
}

Add event handler in content page:

Master.DropDownList.SelectedIndexChanged += OnSelectedIndexChanged;

Assign Master.DropDownList.SelectedValue to Label.Text in event hanlder.

Upvotes: 4

Related Questions