2013-07-23 32 views
0

我正在使用母版和自定義控件在asp.net中開發示例應用程序我有包含兩個文本框和提交按鈕的用戶控件頁面,我只是從兩個文本boc並且只顯示到主網頁.. 這裏是代碼:如何將結果值傳遞給用戶控件的母版頁

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="WebApplication1.WebUserControl1" %> 

<asp:TextBox ID="txtA" runat="server" /> 
<p> 
</p> 
<asp:TextBox ID="txtB" runat="server" /> 
<p /> 

<asp:Button ID="btn01" runat="server" Text="Submit" onclick="btn01_Click" /> 


using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 

namespace WebApplication1 
{ 
    public partial class WebUserControl1 : System.Web.UI.UserControl 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 

     } 
     public delegate void SendMessageDelegate(string message); 

     public event SendMessageDelegate sendMsg; 



     protected void btn01_Click(object sender, EventArgs e) 
     { 
      decimal dec01 = Convert.ToDecimal(txtA.Text); 

      decimal dec02 = Convert.ToDecimal(txtB.Text); 

      decimal dectotal = dec01 + dec02; 

      if (sendMsg != null) 
      { 
       sendMsg(dectotal.ToString()); 
      } 


     } 
    } 
} 
+0

我覺得這個[thread] [1] mayb e可以幫助你。 [1]:http://stackoverflow.com/questions/15040421/access-a-control-in-user-control-and-master-page-value-in-user-control –

+0

在什麼在你的母版頁週期的時刻你需要知道按鈕被點擊了嗎?在Page_Load事件裏面或者沒關係? – jdecuyper

回答

0

你可以做一個公共propertie在您的用戶控制和您所放置用戶控件在網頁中調用這個要傳遞給母版你。可以這樣做:

((MasterPage)this.Master).SomeValue = SomeValue; 
+0

請致電 –

+0

如果您在您的用戶控件上創建了一個公共屬性,則可以在放置usercontrol的頁面的代碼中訪問它。所以如果你想在你的用戶控件中這樣做: 'public string SomeValue {get;設置;}' 你可以在你的aspx頁面後面的代碼中使用它,並使用上面的代碼將它傳遞給你的母版頁。 (將MasterPage替換爲您自己的母版頁的類名稱。 – FreddieH

相關問題