C# 如何将结果值从用户控件传递到母版页

C# 如何将结果值从用户控件传递到母版页,c#,asp.net,.net,C#,Asp.net,.net,我正在asp.net中使用主控件和自定义控件开发示例应用程序。我有一个用户控件页,其中包含两个文本框和提交按钮,我只需从两个文本框中获取输入并显示到主控页。。 以下是代码: <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="WebApplication1.WebUserControl1" %> <asp:TextBox ID="

我正在asp.net中使用主控件和自定义控件开发示例应用程序。我有一个用户控件页,其中包含两个文本框和提交按钮,我只需从两个文本框中获取输入并显示到主控页。。 以下是代码:

    <%@ 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());
            }


        }
    }
}


使用制度; 使用System.Collections.Generic; 使用System.Linq; 使用System.Web; 使用System.Web.UI; 使用System.Web.UI.WebControl; 命名空间WebApplication1 { 公共部分类WebUserControl1:System.Web.UI.UserControl { 受保护的无效页面加载(对象发送方、事件参数e) { } 公共委托void SendMessageDelegate(字符串消息); 公共事件SendMessageDelegate sendMsg; 受保护的无效btn01\u单击(对象发送者,事件参数e) { decimal dec01=Convert.ToDecimal(txtA.Text); decimal dec02=Convert.ToDecimal(txtB.Text); 十进制dectotal=dec01+dec02; 如果(sendMsg!=null) { sendMsg(dectotal.ToString()); } } } }


您可以在用户控件中创建一个公共属性,并在您放置用户控件的网页中调用它。要传递到母版页,可以执行以下操作:

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

我想这个[线索][1]也许可以帮助你。[1] :您需要知道在母版页周期的什么时刻单击了按钮?在Page_Load事件中,或者这无关紧要?如果在usercontrol上创建公共属性,则可以在放置usercontrol的页面的de代码中访问它。因此,如果您想在usercontrol:
publicstringsomevalue{get;set;}
中执行此操作,您可以在aspx页面后面的代码中使用它,并使用上面的代码将其传递到母版页。(用您自己的母版页的类名替换母版页。