页面加载后运行 Javascript
我知道有很多关于此的帖子,但我已经尝试了每一个,但没有一个起作用!
我有一个使用 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 脚本应用到动态表时。什么也没发生。动态数据按要求显示,但数据表(搜索功能、样式等)不存在。
现在我怀疑这是因为脚本是在表填充数据之前运行的。
这就是我开始我的任务的地方,即在 aspx.cs 运行并且表已填充之后强制 jquery/javascript 运行。
但我失败了......所以作为一个测试,我想我应该运行一个警报('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 数据表的样式是否正确???
谢谢您抽出宝贵的时间,
Alex
p.s.对 asp:gridView 或 asp:DataTable 没有评论,因为它失败了,几天前我发布了一个问题,但没有人回复。如果你想看一下 点击此处
I understand there are MANY posts on this, but I have tried every single one and none of them work!
I have an aspx and aspx.cs page being run using visual studio 2010.
on my aspx page I have a table
<table id="viewPendingTable" runat="server"></table>
On the aspx.cs page, the table is loaded with data. This works fine, No Problem. Once the page is loaded, the table contains the dynamically added data I require.
I am using jquery dataTables to style my table using the standard
$(document).ready(function () {
$('#viewPendingTable').dataTable({
"bJQueryUI": true,
});
});
When I create a test table with dummy information directly into the html, the data table displays correctly as required. But when I apply The jquery script to the dynamic table. Nothing happens. The dynamic data is displayed as required, but the data table (search functions, style etc) are not present.
Now I suspect this is because the scripts are run before the table is populated with data.
This is where I began my mission to force the jquery/javascript to run AFTER the aspx.cs has run and the table has been populated.
But I failed... So just as a test I thought I'd run an alert('YAAY'); from the c# side using registered start up scripts. but to no Avail that wouldnt work.
So can somebody PLEASE tell me why this isn't working.
I've tried Multiple Different ways of startup scripts!
My aspx is:
<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>
And my aspx.cs is:
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);
}
I have tried more methods for trying to run javascript after page load but they were lost in the anger of failing so badly.
Somebody PLEASE HELP!
Why aren't my simple javascript alerts running?
Even if I got them to work... would the data table be correctly styled by executing the jquery after page Load???
Thankyou Kindly for your time
Alex
p.s. No comments on asp:gridView or asp:DataTable as That Failed EPICALLY and i posted a question on it days ago and nobody has replied. If you care to take a look
CLICK HERE
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
更改这行代码并尝试
在您编写的 document.read 脚本中
尝试它,因为当页面在浏览器上呈现时,您可以获得表格控件的实际 ID
编辑
使用
RegisterStartupScript
因为它会在页面末尾的标记之前(在
Change this line of code and try
in you document.read script that you have written
Its because you can get acutal ID of the table control when the page get render on the browser
Edit
Make use of
RegisterStartupScript
because it emits your JavaScript at the end of the page just before</form>
tag (before the<form>
tag ends).您面临的最有可能的问题是您的页面上不存在
id="viewPendingTable"
。通过在表中添加
runat="server"
属性,ID 将被更改,以便 ASP.NET 可以在回发时绑定到它。您需要在 jQuery 中使用表的
ClientID
属性:The most likely issue you're facing is that
id="viewPendingTable"
doesn't exist on your page.By adding the
runat="server"
attribute on your table, the ID will be changed so that ASP.NET can bind to it on postback.You'll need to use the
ClientID
property of the table in your jQuery:首先,客户端代码始终在服务器端代码执行后运行。尽管客户端代码可以在浏览器中呈现 html 之前运行。所以这与此无关。
如果检查生成的 html,您将看到 asp.net 将为您应用 runat 属性的元素生成 id。要解决此问题,请向表中添加一个类并将 js 选择器更改为 $(".classname")。
额外提示:使用像 firebug 这样的调试器,它允许您调试 js 代码,这使得解决此类问题变得容易:)
First of all, client side code is ALWAYS run after the server side code is executed. Although client side code can be run before the html is rendered in your browser. So this has nothing to do with that.
If you check the generated html, you will see that asp.net will generate the id's voor the element to which you apply the runat attribute. To solve this, add a class to the table and change your js selector to $(".classname").
Extra tip: Use a debugger like firebug which allows you to debug your js code, which makes it easy to solve problems like these :)