简体   繁体   English

如何在GridView的OnLoad事件上调用javascript函数?

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

I want to call a javascript function when the gridview loads. 我想在gridview加载时调用javascript函数。 Im calling it like this, 我这样叫它,

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

and here is the javascript function 这是javascript函数

<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. 但是我在调​​用javasccript函数时会遇到编译错误。 How to call javascript function on the OnLoad event of GridView? 如何在GridView的OnLoad事件上调用javascript函数? What i want to do is basically print gridview header on each page, but i couldn't figure out how. 我想要做的基本上是在每个页面上打印gridview标题,但我无法弄清楚如何。 Please help! 请帮忙!

I don't see an OnLoad event in gridview, but I do see a Load event. 我没有在gridview中看到OnLoad事件,但我确实看到了Load事件。 Inside this event I would use scriptmanager to call a javascript function from the code behind. 在这个事件中,我会使用scriptmanager从后面的代码中调用javascript函数。 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: 使用scriptmanager可能需要您向表单添加scriptmanager控件:

    <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. 当尝试从后面的VB代码调用我的js函数时,这种技术总是适用于我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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