加载用户控件时添加事件onserverclick

时间:2016-10-10 21:32:04

标签: asp.net vb.net

所以我搜索但无法解决我的问题。 请帮忙。加载用户控件。但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

0 个答案:

没有答案