For instance, I have an ASP.NET button control that's tied to handle an event such as to save, how can I refer to that function through jQuery then call it, so it the function kicks in?
Since ASP.NET submit buttons onclick events are tied in to javascript: WebForm_DoPostBackWithOptions, my thought was to call it but there's got to be a better working way of doing it.
I'm not sure if this is possible, but just thought I'd the community anyways. Thanks.
Thanks for your suggestion, but I am working with .NET's asp:button's OnClick event like the below. I would like to have jquery call "ContactMeButton_Click' event. Is it possible?
<asp:Button id="contactButton" runat="server"
CssClass="nextStepButton buttonPhone"
ValidationGroup="none"
Text="Click here to Contact Me"
OnClick="ContactMeButton_Click" />
when I tried using "$($get("buttonid")).click()" I received a client script
error : object expected, but the "ContactMeButton_Click" was
triggered correctly. Do you how I can fix it or is there another way to call
the asp.net event?
Here's what worked for me. I added a Javascript/jQuery function that checks for postback. You can define a class or id and assign it to the linkbutton to only trigger specific controls but the one I have below triggers all of them. Let me know if this works
for you.
var inputChanged = false;
$("a").hover(function() {
var ahref = $(this).attr("href");
var isPostBack = ahref.indexOf('javascript:WebForm_DoPostBackWithOptions');
if (isPostBack === -1) {
if (inputChanged) {
$(this).click(linkclickevent);
}
}
}
Thank you very much for replying to my email and post. I really appreciate it. Looking at your code,
I do not know how to apply it in my situation. Basically I have two ASP:Button and each has it own OnClick eventhandlers like
below:
.aspx:
<asp:Button id="contactButton" runat="server"
CssClass="nextStepButton buttonPhone"
ValidationGroup="none"
Text="Click here to Contact Me"
OnClick="ContactMeButton_Click" />
If all that you want to to is fire the server-side ContactMeButton_Click handler, couldn't you just change the OnClick handler in buttonPhone to ContactMeButton_Click:
<asp:Button id="buttonPhone" runat="server" OnClick="ContactMeButton_Click" .. other properties
the_menace
Member
336 Points
111 Posts
Triggering an onclick event with jQuery?
Feb 21, 2009 02:56 AM|LINK
For instance, I have an ASP.NET button control that's tied to handle an event such as to save, how can I refer to that function through jQuery then call it, so it the function kicks in?
Since ASP.NET submit buttons onclick events are tied in to javascript: WebForm_DoPostBackWithOptions, my thought was to call it but there's got to be a better working way of doing it.
I'm not sure if this is possible, but just thought I'd the community anyways. Thanks.
jquery submit
Danny117
Star
11160 Points
1932 Posts
Re: Triggering an onclick event with jQuery?
Feb 22, 2009 10:41 AM|LINK
Hope this clicks for you.
http://docs.jquery.com/Events/click
$($get("buttonid")).click();
$("#buttonclientid").click();
$(".buttonclass").click();
Me on linked in
netNewBee
Member
250 Points
149 Posts
Re: Triggering an onclick event with jQuery?
Jan 13, 2010 08:00 PM|LINK
Hi,
I also wanted to have Jquery fire the OnClick event in an asp.net form. When I tried the first suggestion:
$($get("buttonid")).click() I received a client script error : object expected. However, the OnClient event did fire.
Could you please tell me how can I can fix it? I'm using IE 8.
Thanks in advance for any help.
netNewBee :-)
malcolms
All-Star
18687 Points
3124 Posts
MVP
Re: Triggering an onclick event with jQuery?
Jan 13, 2010 08:58 PM|LINK
You can also use jQuery's trigger event to trigger events. Here's an example where one button click triggers another button's click event:
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function(){
$("button:first").click(function () {
update($("span:first"));
});
$("button:last").click(function () {
$("button:first").trigger('click');
update($("span:last"));
});
function update(j) {
var n = parseInt(j.text(), 10);
j.text(n + 1);
}
});
</script>
<style>
button { margin:10px; }
div { color:blue; font-weight:bold; }
span { color:red; }
</style>
</head>
<body>
<button>Button #1</button>
<button>Button #2</button>
<div><span>0</span> button #1 clicks.</div>
<div><span>0</span> button #2 clicks.</div>
</body>
</html>
netNewBee
Member
250 Points
149 Posts
Re: Triggering an onclick event with jQuery?
Jan 14, 2010 12:43 PM|LINK
Hi,
Thanks for your suggestion, but I am working with .NET's asp:button's OnClick event like the below. I would like to have jquery call "ContactMeButton_Click' event. Is it possible?
<asp:Button id="contactButton" runat="server"
CssClass="nextStepButton buttonPhone"
ValidationGroup="none"
Text="Click here to Contact Me"
OnClick="ContactMeButton_Click" />
when I tried using "$($get("buttonid")).click()" I received a client script error : object expected, but the "ContactMeButton_Click" was
triggered correctly. Do you how I can fix it or is there another way to call the asp.net event?
netNewBee :-)
NC01
All-Star
82577 Points
15430 Posts
MVP
Re: Triggering an onclick event with jQuery?
Jan 14, 2010 03:28 PM|LINK
Shouldn't this "$($get("buttonid")).click()" be '$($get("contactButton")).click()'
NC...
netNewBee
Member
250 Points
149 Posts
Re: Triggering an onclick event with jQuery?
Jan 14, 2010 03:49 PM|LINK
Thanks for pointing that out.
yes. I forgot to change it in my post but I had it correctly in my code - $($get("contactButton")).click();
By the way, calling an ASP.net event by clicking on a button is such an common task in .NET, how come I can't find any
related posts/articles on how to do this topic online? Am I not asking my question correctly?
netNewBee :-)
the_menace
Member
336 Points
111 Posts
Re: Triggering an onclick event with jQuery?
Jan 14, 2010 05:08 PM|LINK
Here's what worked for me. I added a Javascript/jQuery function that checks for postback. You can define a class or id and assign it to the linkbutton to only trigger specific controls but the one I have below triggers all of them. Let me know if this works for you.
var inputChanged = false; $("a").hover(function() { var ahref = $(this).attr("href"); var isPostBack = ahref.indexOf('javascript:WebForm_DoPostBackWithOptions'); if (isPostBack === -1) { if (inputChanged) { $(this).click(linkclickevent); } } }netNewBee
Member
250 Points
149 Posts
Re: Triggering an onclick event with jQuery?
Jan 14, 2010 06:20 PM|LINK
Hi Dennis,
Thank you very much for replying to my email and post. I really appreciate it. Looking at your code,
I do not know how to apply it in my situation. Basically I have two ASP:Button and each has it own OnClick eventhandlers like
below:
.aspx:
<asp:Button id="contactButton" runat="server"
CssClass="nextStepButton buttonPhone"
ValidationGroup="none"
Text="Click here to Contact Me"
OnClick="ContactMeButton_Click" />
codebehind:
protected void ContactMeButton_Click(object sender, EventArgs e)
{
//code to update database goes here....
}
In my jqery file:
$(".buttonPhone").click(function() {
$($get("contactButton")).click();
return false;
});
How can I modify this jquery to call the ContactMeButton_Click(object sender, EventArgs e) function?
netNewBee :-)
NC01
All-Star
82577 Points
15430 Posts
MVP
Re: Triggering an onclick event with jQuery?
Jan 14, 2010 08:03 PM|LINK
If all that you want to to is fire the server-side ContactMeButton_Click handler, couldn't you just change the OnClick handler in buttonPhone to ContactMeButton_Click:
<asp:Button id="buttonPhone" runat="server" OnClick="ContactMeButton_Click" .. other properties
NC...