我有使用 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