Hey guyz I wonder if somebody else faced this issue.
I have user control, which contains TextBox control and some jQuery that avoids user to input anything else but numbers. Here is my user control
<script type="text/javascript">
$("input[type=text][id*=txtBox1]").live('keydown', function (event) {
if ((!event.shiftKey && !event.ctrlKey && !event.altKey) && ((event.keyCode >= 48 && event.keyCode <= 57) || (event.keyCode >= 96 && event.keyCode <= 105))) // 0-9 or numpad 0-9, disallow shift, ctrl, and alt
{
// check textbox value now and tab over if necessary
}
else if (event.keyCode != 8 && event.keyCode != 46 && event.keyCode != 37 && event.keyCode != 39) // not esc, del, left or right
{
event.preventDefault();
}
});
</script>
<asp:TextBox ID="txtBox1" MaxLength="9" CssClass="allInp" runat="server"></asp:TextBox>
Now I'm dynamically adding this control to Repeater control, which is inside of UpdatePanel. But the problem is jQuery doesn't work once it's inside of UpdatePanel, I tried to get Repeater control outside of UpdatePanel and it worked as expected. Any considerations
regardin to this issue?
Any kind of help would be appreciated.
Have you tried with the client Id rather than passing the static textbox id?
AFAIK, content of the update panel replace after update. You need to rebind the event to the control after the page_load. Use PageRequestManager to declare the javascript event. A little bit of google/bing search will help you a lot.
Hope that will help you
Anuj Tripathi
Please remember to click “Mark as Answer” on the post that helps you, it will help other(s) to get there answer.
Hello Manvel i tried your code in my sample application and it worked perfectly.... i think there is some issue when your controls renders inside the page then control.clientid is changing thats the reason its not binding to the textbox.....
so look in that direction your control inside usercontrol and you are adding that control inside repeater control so you can think what will be id of your control....
regards,
Rick Jackson (Please mark as answer if my post help you to solve your issue) [Happy programming]
I had similar issue before, and binding events to controls using live() solved my problems, this time it doesn't help, and I get your point Rick, but when I'm lookin @ page source, I can see that both, ID of my control and jQuery selector are same,,, so
I don't think that's a problem, also I can't figure out why alert() inside of <script> tag doesn't work, seems when controls rendered <script> tag is ignored, I thought that it may be problem in my jQuery code ( maybe some syntax error ) but it works outside
of UpdatePanel. Any other considerations guyz?
inline script (<script>...</script>) inside an update panel is only supported on the initial render. any script is ignored on an ajax update. you must use scriptmanger to register the javascript.
bruce (sqlwork.com)
Marked as answer by _manvel_ on Jun 07, 2011 06:12 AM
Thanx for response bruce, one question thou. How can I make sure, that registered script from server side would be inserted inside of UpdatePanel's Content, cuz as I understand on partial postbacks only those parts inside of ContentTemplate gets rendered,
and refreshed, so if <script> would be inserted anywhere outside of <ContentTemplate> it wouldn't be parsed. If I'm wrong, plz let me know. And once again thanx for response!!!
_Manvel_
Contributor
4240 Points
922 Posts
jQuery doesn't work inside of UpdatePanel
Jun 06, 2011 01:01 PM|LINK
Hey guyz I wonder if somebody else faced this issue.
I have user control, which contains TextBox control and some jQuery that avoids user to input anything else but numbers. Here is my user control
<script type="text/javascript"> $("input[type=text][id*=txtBox1]").live('keydown', function (event) { if ((!event.shiftKey && !event.ctrlKey && !event.altKey) && ((event.keyCode >= 48 && event.keyCode <= 57) || (event.keyCode >= 96 && event.keyCode <= 105))) // 0-9 or numpad 0-9, disallow shift, ctrl, and alt { // check textbox value now and tab over if necessary } else if (event.keyCode != 8 && event.keyCode != 46 && event.keyCode != 37 && event.keyCode != 39) // not esc, del, left or right { event.preventDefault(); } }); </script> <asp:TextBox ID="txtBox1" MaxLength="9" CssClass="allInp" runat="server"></asp:TextBox>Now I'm dynamically adding this control to Repeater control, which is inside of UpdatePanel. But the problem is jQuery doesn't work once it's inside of UpdatePanel, I tried to get Repeater control outside of UpdatePanel and it worked as expected. Any considerations regardin to this issue?
Any kind of help would be appreciated.
anujtripathi
Contributor
3759 Points
595 Posts
Re: jQuery doesn't work inside of UpdatePanel
Jun 06, 2011 01:14 PM|LINK
Have you tried with the client Id rather than passing the static textbox id?
AFAIK, content of the update panel replace after update. You need to rebind the event to the control after the page_load. Use PageRequestManager to declare the javascript event. A little bit of google/bing search will help you a lot.
Hope that will help you
Please remember to click “Mark as Answer” on the post that helps you, it will help other(s) to get there answer.
rickjackson
Participant
1362 Points
405 Posts
Re: jQuery doesn't work inside of UpdatePanel
Jun 06, 2011 01:35 PM|LINK
Hello Manvel i tried your code in my sample application and it worked perfectly.... i think there is some issue when your controls renders inside the page then control.clientid is changing thats the reason its not binding to the textbox.....
so look in that direction your control inside usercontrol and you are adding that control inside repeater control so you can think what will be id of your control....
regards,
_Manvel_
Contributor
4240 Points
922 Posts
Re: jQuery doesn't work inside of UpdatePanel
Jun 06, 2011 01:38 PM|LINK
Thanx for quick response.
Yeah I've tried with clientId , and checked, it generates right ID of control.
$("#<%=txtBox1.ClientID %>").live('keydown', function (event) { // content });However it seems doesn't run the script. I've put alert inside of <script> tag, but even alert doesn't showed
$( function () { alert('hi'); $("#<%=txtBox1.ClientID %>").live('keydown', function (event) { // content }); });So I don't think the only problem is event doesn't get binded with my control, seems <script> tag is ignored. Am I gettin anywhere closer ?
_Manvel_
Contributor
4240 Points
922 Posts
Re: jQuery doesn't work inside of UpdatePanel
Jun 06, 2011 01:51 PM|LINK
Thanx for response Rick!
I had similar issue before, and binding events to controls using live() solved my problems, this time it doesn't help, and I get your point Rick, but when I'm lookin @ page source, I can see that both, ID of my control and jQuery selector are same,,, so I don't think that's a problem, also I can't figure out why alert() inside of <script> tag doesn't work, seems when controls rendered <script> tag is ignored, I thought that it may be problem in my jQuery code ( maybe some syntax error ) but it works outside of UpdatePanel. Any other considerations guyz?
bruce (sqlwo...
All-Star
36852 Points
5446 Posts
Re: jQuery doesn't work inside of UpdatePanel
Jun 06, 2011 03:47 PM|LINK
inline script (<script>...</script>) inside an update panel is only supported on the initial render. any script is ignored on an ajax update. you must use scriptmanger to register the javascript.
_Manvel_
Contributor
4240 Points
922 Posts
Re: jQuery doesn't work inside of UpdatePanel
Jun 06, 2011 06:09 PM|LINK
Thanx for response bruce, one question thou. How can I make sure, that registered script from server side would be inserted inside of UpdatePanel's Content, cuz as I understand on partial postbacks only those parts inside of ContentTemplate gets rendered, and refreshed, so if <script> would be inserted anywhere outside of <ContentTemplate> it wouldn't be parsed. If I'm wrong, plz let me know. And once again thanx for response!!!
asteranup
All-Star
30184 Points
4906 Posts
Re: jQuery doesn't work inside of UpdatePanel
Jun 07, 2011 03:51 AM|LINK
Hi,
Check this code. I think you are facing this issue-
http://forums.asp.net/p/1667058/4359465.aspx/1?Re+Modal+Dialog+not+triggered+by+control+within+update+panel
Anup Das Gupta
Mark as Answer if you feel so. Visit My Blog
_Manvel_
Contributor
4240 Points
922 Posts
Re: jQuery doesn't work inside of UpdatePanel
Jun 07, 2011 06:12 AM|LINK
Thnax to all of you guys for response, I had to put this into every page that should run this script
<script type="text/javascript"> $(document).ready(function () { Sys.WebForms.PageRequestManager.getInstance().add_endRequest(bindKeyDown); bindKeyDown(); }); function bindKeyDown() { $('.numerable').live('keydown', function () { if ((!event.shiftKey && !event.ctrlKey && !event.altKey) && ((event.keyCode >= 48 && event.keyCode <= 57) || (event.keyCode >= 96 && event.keyCode <= 105))) { // check textbox value now and tab over if necessary } else if (event.keyCode != 8 && event.keyCode != 46 && event.keyCode != 37 && event.keyCode != 39) { event.preventDefault(); } }); } </script>modaloda
Member
124 Points
72 Posts
Re: jQuery doesn't work inside of UpdatePanel
Apr 25, 2012 08:04 AM|LINK
hi every one
plz check my thread
i think that i deal same issue but i can't solve till now
http://forums.asp.net/t/1795551.aspx/1?i+think+it+conflict+between+ajax+and+jquery+but+i+m+not+sure
thnx