簡體   English   中英

使用jQuery將用戶控件動態加載到div后,如何使javascript執行?

[英]How to make javascript execute after using jQuery to dynamically load user control to div?

我有一個帶有javascript和CSS塊的Web用戶控件。 我正在使用jQuery將其動態加載到主頁中。 當用戶控件加載到名為“ divTable”的div中時,如何使alert('haha')執行?

在我的.ascx文件中,

<script type="text/javascript">
  function pageLoad(sender, args) {
    alert('haha');
  }
</script>

在.aspx文件中,我有

<script type="text/javascript">
    $(function () {
        $('button').click(GetTable);
    });

    function GetTable() {
        debugger;
        var id_1 = $('#txtID1').val();
        var id_2 = $('#txtID2').val();
        $.ajax({
            url: 'Services/Data.svc/RenderUC',
            data: JSON.stringify({ path: 'Controls/ParameterizedTableControl.ascx', id1: id_1, id2: id_2 }),
            type: "POST",
            contentType: "application/json",
            dataType: "json",
            success: function (data) {
                debugger;
                $('#divTable').html(data);
            },
            error: function showError(xhr, status, exc) {
                debugger;
                alert('error');
            }
        });
    }
</script>

在.svc文件中,我有

    [OperationContract]
    public string RenderUC(string path, string id1, string id2)
    {
        Page pageHolder = new Page();
        var viewControl = (ParameterizedTableControl)pageHolder.LoadControl(path);
        viewControl.ID1= id1
        viewControl.ID2 = id2;
        pageHolder.Controls.Add(viewControl);
        StringWriter output = new StringWriter();
        HttpContext.Current.Server.Execute(pageHolder, output, true);

        return output.ToString();
    }

ajax操作完成后,您要運行的所有javascript都應放入成功處理程序中。

function GetTable() {
    debugger;
    var id_1 = $('#txtID1').val();
    var id_2 = $('#txtID2').val();
    $.ajax({
        url: 'Services/Data.svc/RenderUC',
        data: JSON.stringify({ path: 'Controls/ParameterizedTableControl.ascx', id1: id_1, id2: id_2 }),
        type: "POST",
        contentType: "application/json",
        dataType: "json",
        success: function (data) {
            debugger;
            $('#divTable').html(data);

            //
            // insert whatever you want here
            //

        },
        error: function showError(xhr, status, exc) {
            debugger;
            alert('error');
        }
    });
}

您可以在ascx控件的<script>塊內調用函數,如下所示:

<script type="text/javascript">
  function pageLoad(sender, args) {
    alert('haha');
  }
  pageLoad();
</script>

當瀏覽器呈現腳本標記時,這將使您的腳本運行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM