将值从aspx传递给ascx

时间:2012-05-18 17:55:15

标签: c# asp.net ascx

我有一个网页,里面包含一个用户控件。我在aspx页面上有一个属性,它在pageinit方法中设置,我需要在ascx页面上进行操作。 我怎么能得到它?

3 个答案:

答案 0 :(得分:1)

MyAdminPage myPageInstance = this.Parent as MyAdminPage;
if(myPageInstance != null)
{
...
}

对此有一些问题。

Reference .aspx property from .ascx

答案 1 :(得分:1)

在ascx中创建一个公共属性,并在aspx页面中设置它。

只是为了让您知道,PreInit是EventHandler而非方法。

答案 2 :(得分:0)

最简单的选项如下:

  1. 使用公共变量并从父页面访问它。
  2. 将变量分配给ascx前端的隐藏字段。像这样的字段:NAME mprof-report - report generator for Mono's log profiler SYNOPSIS mprof-report [option]... file.mlpd DESCRIPTION mprof-report is the report generator for Mono's log profiler. It reads the MLPD log files produced by the log profiler and generates a report based on the options passed to it. The output is based on individual reports which can be turned on or off. By default, all reports are generated in summary form (i.e., non-verbose output). mprof-report can read both normal and gzip(1)-compressed log files transparently. For information about how to use the log profiler with a program, see the mono-pro- filers(1) page, under the LOG PROFILER section. ~~~~~
  3. 以下示例适用于#1,但#2几乎相同。

    示例#1:

    Aspx页面:

    前端:

    <asp:HiddenField ID="ascxField" runat="server"  />

    ...

    <%@ Register TagPrefix="Admin" TagName="MyUserControl" Src="~/UserControls/.../MyUserControl" %>
    

    代码背后:

    <Admin:MyUserControl ID="MyUserControl" AutoPostBack="true" runat="server" Visible ="false" />   
    

    Ascx页面:

    背后的代码

     this.MyUserControl.Variable1 = 1;
     this.MyUserControl.Variable2= "value";