2011-02-12 75 views
3

內容頁通訊假設我有在母版頁一個dowpdown,我想,當用戶從下拉列表中選擇任何項目,然後會出現一個回傳和所選擇的項目文本將在標籤被dispalyed內容頁。請幫助我提供示例代碼。母版頁,並在asp.net

感謝

回答

4

你應該將以下指令添加到內容頁:

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

添加公共財產IH母版頁的代碼隱藏文件:

public DropDownList DropDownList 
{ 
    get { return dropDownList; } 
} 

添加事件處理程序中的內容頁面:

Master.DropDownList.SelectedIndexChanged += OnSelectedIndexChanged; 

將事件hanlder中的Master.DropDownList.SelectedValue分配給Label.Text。

2

只是一個樣本

MasterPageCode:

Public Class MyMasterPage 
inherits Page (or MasterPage?) 

public readonly property MyDropDown as DropDown 
end Property 

End Class 

頁面代碼

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 
3

在母版頁:

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

論ASPX任何其他網頁:

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

上的任何其他頁面的代碼隱藏:

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

這對我的作品!謝謝 – 2011-11-21 13:39:36