Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 使用web用户控件更新页面文本_Asp.net_User Controls - Fatal编程技术网

Asp.net 使用web用户控件更新页面文本

Asp.net 使用web用户控件更新页面文本,asp.net,user-controls,Asp.net,User Controls,我正在构建一个应用程序,页面将根据查询字符串动态加载用户控件(x.ascx) 我在页面上有一个验证摘要,希望从用户控件更新它。这将允许我使用一个验证摘要拥有多个控件。如何在控件和页面之间传递数据 我知道我可以在设计时定义控件,并使用事件来定义,但这些控件是使用Page.LoadControl动态加载的 另外,我希望避免使用会话或查询字符串。假设您讨论的是asp的验证程序控件,使它们与验证摘要一起工作应该很容易:使用相同的验证组。通常,我从添加ValidationGroup属性的基类派生所有use

我正在构建一个应用程序,页面将根据查询字符串动态加载用户控件(x.ascx)

我在页面上有一个验证摘要,希望从用户控件更新它。这将允许我使用一个验证摘要拥有多个控件。如何在控件和页面之间传递数据

我知道我可以在设计时定义控件,并使用事件来定义,但这些控件是使用Page.LoadControl动态加载的


另外,我希望避免使用会话或查询字符串。

假设您讨论的是asp的验证程序控件,使它们与验证摘要一起工作应该很容易:使用相同的验证组。通常,我从添加ValidationGroup属性的基类派生所有usercontrols,该属性的setter调用重写的方法,该方法将所有内部验证器更改为同一个验证组

棘手的部分是使它们在动态添加时表现良好。您应该注意一些问题,主要是关于页面周期以及将它们添加到页面对象时的问题。如果您知道所有可能在设计期间使用的用户控件,我会尝试使用EnableViewState和Visible静态添加它们,以最小化开销,即使它们太多。

找到了一种方法:

步骤1:创建基本用户控件并在此控件中定义委托和事件

步骤2:在基本用户控件中创建一个公共函数,以引发步骤1中定义的事件

'SourceCode for Step 1 and Step 2 Public Delegate Sub UpdatePageHeaderHandler(ByVal PageHeading As String) Public Class CommonUserControl Inherits System.Web.UI.UserControl Public Event UpdatePageHeaderEvent As UpdatePageHeaderHandler Public Sub UpdatePageHeader(ByVal PageHeadinga As String) RaiseEvent UpdatePageHeaderEvent(PageHeadinga) End Sub End Class '步骤1和步骤2的源代码 公共委托子更新PageHeaderHandler(ByVal页面标题为字符串) 公共类CommonUserControl 继承System.Web.UI.UserControl 公共事件UpdatePageHeaderEvent作为UpdatePageHeaderHandler Public Sub UpdatePageHeader(ByVal PageHeadinga作为字符串) RaiseEvent UpdatePageHeaderEvent(PageHeadinga) 端接头 末级 步骤3:从步骤1中创建的基本用户控件继承Web用户控件

'SourceCode for Step 1 and Step 2 Public Delegate Sub UpdatePageHeaderHandler(ByVal PageHeading As String) Public Class CommonUserControl Inherits System.Web.UI.UserControl Public Event UpdatePageHeaderEvent As UpdatePageHeaderHandler Public Sub UpdatePageHeader(ByVal PageHeadinga As String) RaiseEvent UpdatePageHeaderEvent(PageHeadinga) End Sub End Class 步骤4:从Web用户控件调用在步骤2中定义的MyBase.FunctionName

'SourceCode for Step 3 and Step 4 Partial Class DerievedUserControl Inherits CommonUserControl Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load MyBase.PageHeader("Test Header") End Sub End Class '步骤3和步骤4的源代码 部分类DeriveDuserControl 继承CommonUserControl 受保护的子页加载(ByVal sender作为对象,ByVal e作为System.EventArgs)处理Me.Load MyBase.PageHeader(“测试头”) 端接头 末级 步骤5:在页面中,使用page.LoadControl动态加载控件,并将该控件转换为基本用户控件

步骤6:将事件处理程序附加到此控件

'SourceCode for Step 5 and Step 6 Private Sub LoadDynamicControl() Try 'Try to load control Dim c As CommonUserControl = CType(LoadControl("/Common/Controls/Test.ascx", CommonUserControl)) 'Attach Event Handlers to the LoadedControl AddHandler c.UpdatePageHeaderEvent, AddressOf PageHeaders DynamicControlPlaceHolder.Controls.Add(c) Catch ex As Exception 'Log Error End Try End Sub '步骤5和步骤6的源代码 专用子加载DynamicControl() 尝试 '尝试加载控制 Dim c作为CommonUserControl=CType(加载控制(“/Common/Controls/Test.ascx”,CommonUserControl)) '将事件处理程序附加到LoadedControl AddHandler c.UpdatePageHeaderEvent,PageHeaders的地址 DynamicControlPlaceHolder.Controls.Add(c) 特例 '日志错误 结束尝试 端接头