简体   繁体   中英

How to call javascript function on the OnLoad event of GridView?

I want to call a javascript function when the gridview loads. Im calling it like this,

<asp:GridView ID="GridView4" runat="server" OnLoad="javascript:AddTHEAD('<%= GridView4.ClientID %>')">
</asp:GridView>

and here is the javascript function

<script type="text/javascript">
        function AddTHEAD(tableName) {
        window.open(document.location.href+'&mode=print','_blank');
            var table = document.getElementById(tableName);
            if (table != null) {
                var head = document.createElement("THEAD");
                head.style.display = "table-header-group";
                head.appendChild(table.rows[0]);
                table.insertBefore(head, table.childNodes[0]);
            }       
        }         
</script>

But im getting compilation errors on calling javasccript function like this. How to call javascript function on the OnLoad event of GridView? What i want to do is basically print gridview header on each page, but i couldn't figure out how. Please help!

I don't see an OnLoad event in gridview, but I do see a Load event. Inside this event I would use scriptmanager to call a javascript function from the code behind. Here is an example:

Protected Sub gridCreditLines_ExistingGlobal_Load(sender As Object, e As EventArgs) Handles gridCreditLines_ExistingGlobal.Load

    ScriptManager.RegisterStartupScript(Page,
                                        Page.GetType(),
                                        "AddTHEAD",
                                        "AddTHEAD(" & tableName & ");",
                                        True)

End Sub

Using scriptmanager may require you to add a scriptmanager control to your form:

    <ajaxToolKit:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
    </ajaxToolKit:ToolkitScriptManager>

I'm sure there is a little more to it to hook this into your existing code, but this should at least point you to the right direction. This technique always works for me when trying to call my js functions from the VB code behind.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM