调用按钮单击事件ajax加载用户控件

时间:2016-04-27 05:04:15

标签: javascript c# jquery asp.net ajax

我通过ajax调用在我的aspx页面中添加了usercontrol,UserControl UI已成功加载,但其click事件未触发。

$.ajax({
  type: "POST",
  url: "Test.aspx/UserControl",
  data: JSON.stringify({
    load: target.substring(1)
  }),
  contentType: "application/json; charset=utf-8",
  dataType: "json",
  success: function(uc) {
    $('#results').html('').html(uc.d);
  },
  failure: function(msg) {
    //error
  }
});

[的WebMethod]

    public static string UserControl(string load)
    {
        using (Page page = new Page())
        {
            UserControl userControl = (UserControl)page.LoadControl("UserControls/ucgTest.ascx");
            page.Controls.Add(userControl);
            using (StringWriter writer = new StringWriter())
            {
                page.Controls.Add(userControl);
                HttpContext.Current.Server.Execute(page, writer, false);
                return writer.ToString();
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

protected void Application_AuthenticateRequest(Object sender, EventArgs e) { try { if (Context.Request.Cookies[".ASPXAUTH"] != null) { var authCookie = FormsAuthentication.Decrypt(Context.Request.Cookies[".ASPXAUTH"].Value); var identity = new GenericIdentity(authCookie.Name); Context.User = new GenericPrincipal(identity, null); } else { Context.User = null; } } catch(Exception ex) { Context.User = null; } } 很难presume你指的是click。 假设新加载的button中存在HTML,并且该按钮附加了click个事件。

在这种情况下,您必须delegate event并进行此更改

$('body').on('click',"id/class",function(event){
  //Rest of the code
})

其中id / class是将附加click事件的按钮的id或类。它也可以是任何其他标识符