所以我搜索但无法解决我的问题。 请帮忙。加载用户控件。但onserverclick事件不起作用。
WebFormTest.aspx
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebFormTest.aspx.vb" Inherits="MS2.WebFormTest" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<%=MS2.CLoadUC.RenderUserControl("/Site/testUC.ascx")%>
</div>
</form>
</body>
</html>
testUC.ascx
<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="~/testUC.ascx.vb" Inherits="MS2.testUC" %>
<script runat="server">
Protected Sub Button1_Click(sender As Object, e As EventArgs) 'Handles Button1.ServerClick
Dim a = "test"
End Sub
</script>
<button ID="Button1" runat="server" onserverclick="Button1_Click">Button</button>
CLoadUC.RenderUserControl( “/站点/ testUC.ascx”)
Imports System.IO
Imports System.Reflection
Public Class CLoadUC
Public Shared Function RenderUserControl(path As String) As String
Dim pageHolder As New Page
Dim viewControl As UserControl = DirectCast(pageHolder.LoadControl(path), UserControl)
Dim viewControlType As System.Type = viewControl.GetType()
'Return viewControl
pageHolder.Controls.Add(viewControl)
Dim output As New StringWriter()
HttpContext.Current.Server.Execute(pageHolder, output, False)
Return output.ToString()
End Function
End Class