没有在javascript函数</div>中获取<div>标记内容

时间:2014-01-18 08:57:41

标签: c# javascript jquery asp.net html

第一个ContentPlaceHolder中的Javascript代码: -

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">

<script type="text/javascript" >
    function PrintElem(elem) {
        alert(elem);
        Popup($(elem).html());
    }
    function Popup(data) {

        var mywindow = window.open('', 'Loan Approve Details', 'height=400,width=600');
        mywindow.document.write('<html><head><title>Loan Inquiry Details</title>');
        /*optional stylesheet*/ //mywindow.document.write('<link rel="stylesheet" href="main.css" type="text/css" />');
        mywindow.document.write('</head><body >');
        mywindow.document.write(data);
        mywindow.document.write('</body></html>');

        mywindow.print();
        mywindow.close();

        return true;
    }
</script> 
</asp:Content>

第二个ContentPlaceHolder中的源代码: -

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
      <div id="mydiv">
       <table>
          //here is lots of textbox and other controls
<asp:Button ID="btnPrint" runat="server" Text="Print" OnClientClick="PrintElem('#mydiv')"
             CausesValidation="False" />
       <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" 
                                    CausesValidation="False" />
       </table>
    </div>
</asp:Content>

代码背后: -

protected void Button1_Click(object sender, EventArgs e)
{
    ClientScript.RegisterClientScriptBlock(this.GetType(), "myscript", "PrintElem('#mydiv')",true);
} 

说明: - 我的问题是,当我从源代码中调用 PrintElem(elem)函数时,它可以正常工作但是当我从后面的代码调用 PrintElem(elem)函数时,我不是在 $(elem).html()中获取 div 的任何值,即使我在提醒(elem)中获得相同的ID值,请帮助我解决了这个问题。我实际上想要在代码后面执行一些代码后打印 div 标签的所有内容。所以我正在尝试这样的事情。

1 个答案:

答案 0 :(得分:1)

在页面加载完成后尝试RegisterStartupScript执行脚本:

protected void Button1_Click(object sender, EventArgs e)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "myscript", "PrintElem('#mydiv')",true);
        }