c# - 页面加载后运行 Javascript

标签 c# jquery asp.net datatables startupscript

我知道有很多关于此的帖子,但我已经尝试了每一个,但没有一个有效!

我有一个正在使用 visual studio 2010 运行的 aspx 和 aspx.cs 页面。

在我的 aspx 页面上我有一个表

<table id="viewPendingTable" runat="server"></table>

在 aspx.cs 页面上,表中加载了数据。这工作正常,没问题。加载页面后,该表包含我需要的动态添加的数据。

我正在使用 jquery dataTables 使用标准来设计我的表格

$(document).ready(function () {
    $('#viewPendingTable').dataTable({
        "bJQueryUI": true,
    });
});

当我直接在 html 中创建一个包含虚拟信息的测试表时,数据表会按要求正确显示。但是当我将 jquery 脚本应用于动态表时。什么都没发生。动态数据按要求显示,但数据表(搜索功能、样式等)不存在。

现在我怀疑这是因为脚本是在表中填充数据之前运行的。

这是我开始执行强制 jquery/javascript 在 aspx.cs 运行并填充表后运行的任务的地方。

但我失败了...所以作为测试,我想我应该运行一个 alert('YAAY');从 c# 端使用注册的启动脚本。但无济于事。

有人可以告诉我为什么这不起作用吗。

我尝试了多种不同的启动脚本方式!

我的 aspx 是:

<table id="viewPendingTable" runat="server"></table>

<script type="text/javascript" language="javascript" id="ShowTable">
<!--
    loadTable();
    function loadTable() {
        $(document).ready(function () {
            $('#viewPendingTable').dataTable({
                "bJQueryUI": true,
                "sPaginationType": "full_numbers"
            });
        });
        alert('ALEX');
        document.getElementById("table_id").width = "100%";
    }
//-->
</script>

我的 aspx.cs 是:

protected void Page_Load(object sender, EventArgs e)
{
    loadMyTable();
    ClientScriptManager a = null;
    a.RegisterStartupScript(GetType(), "anotherkey", "alert('ASDFASDFASDF');", true);
    a.RegisterStartupScript(GetType(), "anfgsdgsderkey", "alert('MOTHER');", false);
    a.RegisterStartupScript(this.GetType(), "AKey", "loadTable();", true);

    ClientScript.RegisterStartupScript(GetType(), "MyScript", "<script language=javascript>" + "alert('Hello ASP.NET'); }</script>");

    ScriptManager.RegisterStartupScript(this.Page, GetType(), "script", "alert('Success!');", true);

    Page.ClientScript.RegisterClientScriptBlock(GetType(), "script", "alert('AAA');", true);

    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "script",
        "alert('a');" +
        "$(document).ready(function (){" +
            "$('#viewPendingTable').dataTable({" +
                "\"bJQueryUI\": true" +
                "\"sPaginationType\": \"full_numbers\"" +
            "});" +
        "}); alert('b'); document.getElementById('viewPendingTable').width = 100%", true);

}

我已经尝试了更多的方法来尝试在页面加载后运行 javascript,但他们在失败如此严重的愤怒中迷失了方向。

有人请帮忙!

为什么我的简单 javascript 警报没有运行?

即使我让它们工作...通过在页面加载后执行 jquery 是否可以正确设置数据表的样式???

谢谢您的宝贵时间

亚历克斯

附注没有关于 asp:gridView 或 asp:DataTable 的评论,因为它彻底失败了,几天前我在上面发布了一个问题,但没有人回复。如果你想看一看 CLICK HERE

最佳答案

改下这行代码试试

"$('# " +   viewPendingTable.ClientID + "').dataTable({" +

在你的文档中阅读你写的脚本

这是因为当页面在浏览器上呈现时,您可以获得表控件的实际 ID

编辑 利用

RegisterStartupScript因为它会在页面末尾 </form> 之前发出您的 JavaScript标签(在 <form> 标签结束之前)。

关于c# - 页面加载后运行 Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6745327/

相关文章:

javascript - 如何替换 src 域名 tld

jquery - 在 ASP.NET Mvc 中使用 Dropzone 传递隐藏的表单值

c# - 使用其他语言字符的 HttpUtility.HtmlEncode 问题

asp.net - 找出 AD 中的组是否在通讯组中?

c# - Dekker 算法混淆的变体

c# - .NET4.6中DataRow.Item [string]上的CS1061

javascript - Rails 4 JQuery Ajax 未显示

c# - 如何从后面的 c# 代码调用 javascript 函数

c# - 如何将依赖项从测试项目传递到 DryIoc 中的具体实现

c# - 移动刚体游戏对象的正确方法