admin管理员组文章数量:1356248
As the title suggests, I am having trouble maintaining my code on postback. I have a bunch of jQuery code in the Head section and this works fine until a postback occurs after which it ceases to function!
How can I fix this? Does the head not get read on postback, and is there a way in which I can force this to happen?
JavaScript is:
<script type="text/javascript">
$(document).ready(function()
{
$('.tablesorter tbody tr').tablesorter();
$('.tablesearch tbody tr').quicksearch({
position: 'before',
attached: 'table.tablesearch',
stripeRowClass: ['odd', 'even'],
labelText: 'Search:',
delay: 100
});
});
</script>
As the title suggests, I am having trouble maintaining my code on postback. I have a bunch of jQuery code in the Head section and this works fine until a postback occurs after which it ceases to function!
How can I fix this? Does the head not get read on postback, and is there a way in which I can force this to happen?
JavaScript is:
<script type="text/javascript">
$(document).ready(function()
{
$('.tablesorter tbody tr').tablesorter();
$('.tablesearch tbody tr').quicksearch({
position: 'before',
attached: 'table.tablesearch',
stripeRowClass: ['odd', 'even'],
labelText: 'Search:',
delay: 100
});
});
</script>
Share
Improve this question
edited Jul 16, 2015 at 4:08
Blakes Seven
50.5k14 gold badges131 silver badges136 bronze badges
asked Nov 11, 2008 at 16:27
DamienDamien
14.1k14 gold badges59 silver badges89 bronze badges
5 Answers
Reset to default 6If you just have that code hard coded into your page's head then a post back won't affect it. I would check the following by debugging (FireBug in FireFox is a good debugger):
- Verify the script is still in the head on postback.
- verify that the css classes are in fact attached to some element in the page.
- verify that the jquery code is executing after the browser is done loading on post back.
EDIT: Are you using UpdatePanels for your post back? In other words is this an asynchronous postback or a normal full page refresh?
EDIT EDIT: AHhhhh... Ok. So if you're using UpdatePanels then the document's ready state is already in the ready so that portion of jquery code won't be fired again. I would extract the jquery delegate out to a separate function that you can also call after the async postback.
put your code in
function pageLoad(sender, args) {
/* code here */
}
instead of in $(document).ready(function() { ... });
pageLoad()
is a function that will execute after all postbacks, synchronous and asynchronous. See this answer for more details
- How to have a javascript callback executed after an update panel postback
I'm guessing that the postback pre-empts the page's onLoad event, which jQuery needs to hook into to use it's .ready().
- Does the script exist in the HTML code after the postback?
- If so, does the code get executed? Test by menting out your code and temporarily add
alert('test');
- If so, are the elements referenced by the code available on the page after postback?
Instead of using $(document).ready you should put your code in a function called pageLoad(). The pageLoad() function is by convention wired up to be called whenever the page has a postback/asyncpostback.
本文标签: Maintaining JavaScript Code in the ltHeadgt after ASPNet PostbackStack Overflow
版权声明:本文标题:Maintaining JavaScript Code in the <Head> after ASP.Net Postback. - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743943561a2566003.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论