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

Call button click event ajax loaded user control

本文关键字:用户 控件 加载 ajax 按钮 单击 事件 调用      更新时间:2023-09-26

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

$.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
  }
});

[网络方法]

    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();
            }
        }
    }

很难presume你指的是哪个click。假设新加载的HTML中存在button,并且click事件附加到该按钮。

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

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

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