How to trigger a button as clicked using jquery or jscript? - asp.net

Is there a way to trigger a hidden button on an html markup page using jquery or jscript?
How would I do that if possible?
Thank you,
James

With jQuery:
$('#buttonId').click();
or:
$('#buttonId').trigger('click');
With plain JavaScript:
document.getElementById('buttonId').onclick();
Since you're using ASP .NET you might want to get the button id by using the ClientID server-side property of the control:
$('#<%=Button.ClientID %>').click();
Or:
document.getElementById('<%=Button.ClientID %>').onclick();

$('#example-button-id').click();

If I understand correctly, you want to fire an event programmatically. This is usually not necessary. You can very easily move the button click even code to a new method, and have the event invoke that method. You can then also call the method in the code that would want to fire the button click even programmatically.

Try this (requires jquery):
$("button").trigger('click');

Related

TextBox keydown event at server side in asp.net

i have a textbox on a page, now whenever any age number is entered in that page, that page should be loaded in my datalist, i have created rest of the thing, but i am not getting how to trigger the textbox onkeydown event from asp.net, i know working with that from javascript, but the problem is i have below things done in that function:
it applies the currentpage value to the static variable from textbox
it binds the datalist
it enable disable the next previous buttons accordingly
and i dont think this i can do from javascript, anyone have any resolution, how this could be achieved from server side, onkeydown event
You can capture the keydown event in a javascript function then do a AJAX call to the server. If this does not suit you, then you could try manually do postback in javascript.
So in your textbox:
mytextBox.Attributes.Add("onkeydown", "return doDataListBind()");
Now in the javascript function:
function doDataListBind()
{
__doPostBack("myTextBox"); // etc, etc
// some other code here...
}
More info can be found here:
http://geekswithblogs.net/mnf/archive/2005/11/04/59081.aspx
http://www.asp.net/ajaxlibrary/AjaxControlToolkitSampleSite/
Try looking into or using either:
AJAX Control Toolkit, or
JQuery Autocomplete Plugin
Alternatively you could try to call _postback() function from a client side event/function for your textbox in javascript

fire .net event from jquery

I have an image on my page than can be changed from a fileupload asp.net control on the page, i want to provide and alternative to clicking the button by allowing the user to click their image. i gave the fileupload a class 'jqueryPhotoUploadTrigger' and it renders like this:
<input type="file" name="ctl00$ctl00$ctl00$ContentPlaceHolderDefault$C2kMasterPlaceholder$ucEditDetails_20$photoupload" id="ctl00_ctl00_ctl00_ContentPlaceHolderDefault_C2kMasterPlaceholder_ucEditDetails_20_photoupload" class="jqueryPhotoUploadTrigger" />
i then have jquery script at the bottom:
$(document).ready(function() {
$("#clickMyColl").click(function () {
alert("image has been clicked");
$('.jqueryPhotoUploadTrigger').click();
});
});
the alert does show so i know the script is firing when i click the image. i was just hoping the .click would fire the event on the button but it didnt. can i do it this way or must i find an alternative?
you could do a submit or use the Ajax way, there are functions like __doPostback(), look in the ASP.NET documentation.
You could either to an ajax call of have an ImageButton on the page with a transparent image (so its hidden)
then in your jquery you could use
$("#yourhiddenbutton").click();
this would then fire the server side click event
Maybe you've to set the focus on the input (if I've understood your question):
$('.jqueryPhotoUploadTrigger').focus();
You are using the ID of your image as "#clickMyColl", but instead use the ID as it is rendered (jQuery does not know the asp ID): "ctl00_ctl00_ctl00_ContentPlaceHolderDefault_C2kMasterPlaceholder_ucEditDetails_20_photoupload"
Alternatively, you could use the class to select:
$(".jqueryPhotoUploadTrigger").click(function ...

In asp.net textbox: enter text and it appears instantly in another textbox

In asp.net textbox: enter text and it appears instantly in another textbox
any good suggestions?
i need this all over the my web app so it should be more light weighted too
Here is an example using jQuery.
http://jsbin.com/axeko3/2/edit
Hope this helps.
Here is the jQuery code to do this.
$(document).ready(function(){
$("#box1").keyup(function(){
$("#box2").val($(this).val());
});
});
You should do it using JavaScript. You save so much resources by doing it in client side.
Use the onChange event of input element
Try JavaScript, OnBlur or OnChange access textbox_1 by document.getElementById or by document.getElementByName and write to other textbox_2 by the same functions.

Which .NET controls don't use javascript to handle events?

I noticed that the
asp:Button
for example will work without javascript enabled in the browser. However any other control with an "OnClick" or "OnServerClick" event must use the javascript_doPostback.
Are there any controls besides the Button that don't need to use javascript?
I want to know because I want to be able to style the Control however I want without it looking like a button, and I want it to still work without the user having javascript enabled.
Any suggestions?
I think the answer is none. The button submits the form (as buttons will do) but no other controls auto-submit a form without a client side event handler.
Note that this also applies to the Link button.
_doPostback is the heart of ASP.NET. If you don't want to require the use of Javascript then ASP.NET isn't the language for you.

How can I trigger a server-side event from a client-side event?

Short version
All I need is that, in a ModalPopUp, when the DoubleClick event of a ListItem fires, the click event of my OK button should be executed.
Detail
I have a ModalPopUpExtender, which hosts a user control. The user control has an OK and a Cancel button. Along with that, it has a dynamic ListBox added to it. So far, I've considered the following possible solutions:
Use Ajax.Net. But, I cannot afford to have a WebMethod.
Use a ClientScriptCallBack. This will need a lot of JavaScript, since I have made almost every control dynamic.
Is there any other way apart from using an UpdatePanel?
Have a look at this way using __doPostback
calling a VB.net function from javascript

Resources