0

Javascript Code In First ContentPlaceHolder :-

<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>

Source Code in second 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>

Code Behind :-

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

Explanation :- Here my problem is that when I call PrintElem(elem) function from source code then it works properly but when I call PrintElem(elem) function from code behind then I am not getting any value of div in $(elem).html() even I am getting same id value in alert(elem) so please help me to solve this problem.And I actually want to print all contents of div tag after execution of some code in code behind.so I am trying something like this.

1
  • please write your javascript code on document.ready function. Commented Jan 18, 2014 at 9:02

1 Answer 1

1

Try RegisterStartupScript to execute a script when the page finishes loading:

protected void Button1_Click(object sender, EventArgs e)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "myscript", "PrintElem('#mydiv')",true);
        } 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.