0

So I searched but could not solve my problem. please help.The user control is loaded. But onserverclick event does not work.

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("/Site/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
Amir Bagheri
  • 1
  • 1
  • 1
  • Possible duplicate of [ASP.net button onserverclick only works when onclick isn't defined](http://stackoverflow.com/questions/4830095/asp-net-button-onserverclick-only-works-when-onclick-isnt-defined) – Visual Vincent Oct 10 '16 at 23:15
  • I do not mean. When the control is loaded String event does not work. – Amir Bagheri Oct 11 '16 at 06:40
  • What's a "String event"? The question and its accepted answer currently describes your problem rather much. Apparently, `OnServerClick` is only fired when `OnClick` is defined - which you don't have. Try it out instead. – Visual Vincent Oct 11 '16 at 07:13

0 Answers0