我使用jQuery,ASP.NET,VB.NET构建了web应用程序。此应用程序使用jQuery选项卡构建选项卡。单击选项卡,用户控件将动态加载到div中,如下所示。
//Load user control - jquery call to page method to load user control
LoadTabControlPage = function (tabselected) {
var loc = window.location.href;
loc = (loc.substr(loc.length - 1, 1) == "/") ? loc + "Default.aspx" : loc;
$j.ajax({
url: loc + "/Result",
async: false,
cache: false,
type: "POST",
data: '{ "controlName": "' + tabselected + '"}',
contentType: "application/json; charset=utf-8",
dataType: "json",
statusCode: {
200: function (response) {
$j('#controlsdiv').show();
$j('#controlsdiv').html(response.d);
}
}
});
};
VB中的PageMethod:
<WebMethod()> _
Public Shared Function Result(controlName As String) As String
Dim page As New UI.Page()
Dim userControl As UserControl = DirectCast(page.LoadControl("UI/" + controlName.Replace(" ", "") + ".ascx"), UserControl)
userControl.EnableViewState = False
Dim form As New HtmlForm()
form.Controls.Add(userControl)
page.Controls.Add(form)
Dim textWriter As New StringWriter()
HttpContext.Current.Server.Execute(page, textWriter, False)
Return textWriter.ToString()
End Function
问题是在加载用户控件后,其按钮事件未被触发。 MyAscx文件:
<asp:Button ID="reportsbtn" Text="Run Report" OnClick="reportsbtn_Click" runat="server" />
MYAscx.vb
Protected Sub reportsbtn_Click(sender As Object, e As EventArgs) Handles reportsbtn.Click
//Code
End Sub