Fire Textbox.TextChange event without a button click - asp.net

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.

Related

asp.net detect DropDownList.SelectedIndexChanged after Button Click (JS disabled)

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?

asp.net textbox - client side textChanged event won't fire

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

ASP.NET button click fire jQuery

I have a jQuery Pager control, for each page there is a set of textboxes and a submit button. When this submit button is clicked I want it to fire off some jQuery to update the controls on the front end (i.e. current page, set visibility of controls etc). The button is an asp.net web forms postback button.
Does anyone know any way of doing this?
Merry Christmas!
Inject js from code-behind after your postback success like:
string script = "$(function(){setPage(\"" + yourpagenumber+ "\");});";
ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "Pager", script, true);
And in the js you will have a setPage function that does the job of setting page with your jquery pager plugin.
function setPage(pagenumber){
alert(pagenumber);
//do your page setting here
}
Note: you can use Page.ClientScript.RegisterStartupScript if that fits your needs but the idea remains same.
Don't use a submit button, use an input of type button with an onclick event instead.
Use the OnClientClick attribute of the asp:button i.e.
OnClientClick="JQueryFunction();return false;"
for no postback and
OnClientClick="JQueryFunction();return true;"
for a postback. I'm assuming that that JQueryFunction() returns true.

How to automatically click "update" button in asp.net control?

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

Is it possible to detect what asp:button has been clicked in the Page_init event?

I have an ASP:Button with an onclick event. Is it possible to detect that this button has been clicked on the Page_Init event?
If you know the ClientId of the button, you can check in the Request object. Otherwise, no. At the Page_Init stage, the page and child controls are only being loaded, and events will be handled only later.
Request.Form["_EVENTTARGET"]
You could try to use Request.Form["_EVENTTARGET"], it should hold the ID of the control that fired the PostBack.
The "button1" is the client id of your button. If Request.Params["button1"] does not return null then "button1" was clicked.

Resources