i want my textbox to fire a textChanged client side (JS) event when a text is changed inside it.
i read many posts about it. most of them are talking about a code-behind event and the ones that talk about the client side tells to add attribute of onchange:
<asp:TextBox runat="server" ID="TextBox1" onchange="javascript: ontextchanged();"></asp:TextBox>
but this event only fires when i lose the focus on the textbox.
what is the solution to this ?
how can i fire a JS function each time a text is changed inside the textbox?
Use onkeyup or onkeydown instead.
This will then run the function when you type or click on the textbox. You can also then detect the keycode of the event, and prevent the function if you dont want it to run for certain keys.
Try this http://www.zurb.com/playground/jquery-text-change-custom-event
Related
I have a DropDownList which is connected to a TextBox. When the event SelectedIndexChanged is fired, the text in the Textbox changes. The content of the textbox can also be changed by user input. At the end of the page the user has to submit the data (button).
When javascript is enabled everything works fine, but I have also users where javascript is disabled.
Without javascript the SelectedIndexChanged event of the DropDownList gets called after the Button-Click (before the Click-Event of the button is processed). The user input to the textbox gets overwritten by the SelectedIndexChanged event.
How can I detect in Code behind if the SelectedIndexChanged event was triggerd by the button click? How can I get the text of the textbox changed by the user?
EDIT:
Can I use the property IsPostBackEventControlRegistered? This always seems to be true when I click the button, but is false in SelectedIndexChanged event.
Well, it IS possible, but not easy. You would essentially need to track whether javascript is enabled by using a hidden field, do something like:
<asp:HiddenField ID="JSEnabled" runat="server" ClientIDMode="Status" Value="F" />
<script type="text/javascript">
function checkForJavaScript() {
$("#JSEnabled").val("T");
}
</script>
You call this script from window.onload or jquery.ready event, whatever JS option you like, and this can tell you whether you have JS enabled to do the selectedindexchanged event, or if a button triggered it because JS wasn't enabled. To tell whether the button was clicked and caused the postback, you can check the:
Request.Form.Get("__EVENTTARGET")
Field and see if the ID matches the UniqueID of the button. Every control in ASP.NET sets its ID (usually, but not always, for certain reasons) here. Although, I'm not sure, you may have to set UseSubmitBehavior="False" for that to happen. A hidden field can be manipulated, so it's not fullproof.
The question you should ask is do you really need that complexity?
I've written a web page that uses an ASP.NET CustomValidator to validate the input of a text field (client side). The text field uses JQuery UI Autocomplete - and this is where I run into problems.
The validator works just fine. But in the event that the validation fails and the user gets an error message, the user will go back to the text field and enter a new value - choosing from the drop down made by the autocomplete plugin. Now when a new value has been chosen and the user leaves the input field, the validation code doesn't fire again. I suspect that it is because, for some reason, it is not detected that the text was changed, when the value came from the autocomplete helper. Does that make sense?
Does anyone know how I can force the field to validate via the CustomValidator when the user removes focus from the field?
Heres the CustomValidator:
<asp:CustomValidator EnableClientScript="True" runat="server" ControlToValidate="tbInput"
ID="inputCustomValidator" ClientValidationFunction="validateFunction" ErrorMessage="Not valid"
Display="Dynamic" ValidationGroup="ValidationGrp1" />
The javascript that is called is not interesting - since it is not being called. That is what I want to achieve.
I found a way to do this. In javascript I added a blur() function to the element I wanted to validate and made that function trigger Page_ClientValidate('validationGroupName'). A functionality that was new to me.
$('.elementToValidate').blur(function () {
Page_ClientValidate('ValidationGrp1');
});
This might not be what you want to hear, but I would avoid mixing jQuery and ASP.net's Javascript whenever possible - they don't tend to play nicely.
In your case, I'd recommend dumping your ASP.net CustomValidator and switching to jQuery's Validate plugin. That will play much more nicely with jQuery UI.
You can also use autoselect's select: handler to trigger validation.
$(".someClass").autocomplete({
select: function(event, ui)
{
Page_ClientValidate(ui);
}
});
I got this working by setting the onBlur event for my textbox.
<asp:TextBox ID="TextBox1" runat="server" onBlur="Page_ClientValidate('ValidationGrp1');"></asp:TextBox>
(assuming your CustomValidator's ValidationGroup is 'ValidationGrp1')
I want to call Textbox.OnTextChange event without having to click a submit button or any button. How can I do this? As I enter text in the textbox, I want the ontextchange event to fire.
I would elaborate this further.I've a function for validating my entry in textbox which I'm calling ontextchange property.Now this function should be called without me changing focus from my textbox.
You can use onkeypress , onkeyup and onkeydown events for this.
http://www.w3schools.com/tags/tag_input.asp
Example :
<asp:TextBox ID="TextBox1" runat="server" onKeyPress="javascript:alert('Key Pressed');"></asp:TextBox>
you can set AutoPostBack to true on the textbox control. when the client side change event fires (when the focus changes from the textbox to something else), the page will do a postback automatically.
If you want a postback:
You want to use the onkeypress or onkeyup javascript event to run the __doPostBack javascript method. __doPostPack has some parameters you will need to look up on msdn
Otherwise just use the onkeypress or onkeyup javascript event to do whatever else it is you need with javascript.
I used asp.net server side control to display and modify data in database, the control is just like this one:
http://demos.telerik.com/aspnet-ajax/grid/examples/dataediting/alleditablecolumns/defaultcs.aspx
what I want to do is after I click the "edit" button it will display a "edit" ui, and I want everytime I modify the data in the text box, asp.net will automatically click the "update" button for me to update the data i entered.
I tried to call the event handler, but failed.
There is a update command in asp.net, and how to programmatically call it ?
Try this. You can get the event refernce via this code
string postbackEvent = this.ClientScript.GetPostBackEventReference(this.button,"");
the postbackEvent will contains a __doPostback() function, which will invoke the button click in the server side. Assign this to some event, like onBlur of textbox.
this.txtSample.Attributes.Add("onBlur",postbackEvent);
Probably you will have to use the onTextChanged event of your TextBox control.
Set the autopostback attribute to "true"
<asp:TextBox AutoPostBack="True" ID="somethingID" OnTextChanged="CallSomeMethod" />
Look here : http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.textbox.autopostback.aspx
I am building a user registration form in asp.net. I want to check if the username is available or not on the leave event of the TextBox. I am not able to get the Leave Event of the TextBox.
Should I use OnBlur event of the Html TextBox.
Please Help.
How about using TextChanged server-side event and wrapping the whole thing in an UpdatePanel for very quick ajax functionality.
If you use this method make sure the AutoPostBack property is set to true for the TextBox control.