开发者

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 t开发者_运维技巧he label of content page. please help me with sample code.

thanks


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.


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;
    }


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


You need to be looking at something like this...

http://www.eggheadcafe.com/tutorials/aspnet/969a11b2-a5a4-4a51-852a-003fd40bbd68/access-master-page-contro.aspx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜