ASP: ontextchanged not firing - asp.net

Note: I am brand new at ASP.NET. I'm actually going to training on Monday, but we have a hot project that required me to dive in and get done as much as I can.
I've got a textbox on my page, and I would like it to call an event on the server whenever it is changed. I don't care if "changed" means changed and loses focus, or just whenever a keyup happens. Either case will work.
The issue is that, for some reason, whenever I change my textbox and then remove focus, the server-side method is not getting called. Viewing through the debugger in Chrome, I don't even see any sort of AJAX call being made that would inform the server that a textbox was changed.
My Code:
ASCX File
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:TextBox ID="tempTagBuilder" runat="server"
CssClass="depBuilder_tempTagBuilder"
ontextchanged="tempTagBuilder_TextChanged" AutoPostBack="True"></asp:TextBox>
</ContentTemplate>
</asp:UpdatePanel>
ASCX.cs File
//whenever the text is changed
protected void tempTagBuilder_TextChanged(object sender, EventArgs e)
{
System.Diagnostics.Debug.WriteLine("Hit");
}
Anyone have a good idea of what my issue might be?
Update 1:
I got it working (somewhat). I had to go into the updatepanel's properties and add the textchanged event to the triggers collection. However, now whenever it sends the update it is emptying out the other textboxes on my page!

It doesn't fire because it's in an update panel most likely. Seems the updatepanel requires you to set a trigger for the event.
Problem with textbox inside updatepanel - not causing OnTextChanged event

Related

ASP.NET webform Fileupload inside an ascx which is inside an updatepanel in the aspx

here is some code in my page :
<asp:UpdatePanel ID="UpdatePanelEQSelector" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<uc12:EQSelector ID="custEQSelector" OnEqChange="custEQSelector_OnEqChange" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
Inside my user Control, I'm asked to add a fileuploader.
After coding it simply, I tested and my fileuploader is always empty.
I've searched a while and discovered that it was a normal behavior.
The solution to make it work is to add a PostBackTrigger for the updatePanel.
When I tested it in my aspx page, I achieved to do it and my fileUploader had the file.
Then I tried to add it dynamically (to finally do it in my control), it worked with that :
PostBackTrigger trigger = new PostBackTrigger();
trigger.ControlID = this.btnTest.ID;
this._UpdatePanelEQSelector.Triggers.Add(trigger);
But I can't manage to make this code work in my control (I passed my updatePanel as a parameter to my control set in Load, the fileUpload is always empty)
Do you see a solution ?
Thanks
Does it postback, but leaves the control empty? or is it not posting back at all. If it's not posting back at all I'd add this
ScriptManager.GetCurrent(this).RegisterPostBackControl(btnTest);
to the page_load of the user control.
If it is posting back, but the control is empty, then I'd wager there is probably some dodgy html somewhere on the page and the values are getting lost.

asp:checkedchanged event not firing?

there. I have a gridview with a column of check boxes which was working when it was a small test project but when adding it to a page on my teams project it stopped firing the checkedChanged event. The check mark still appears or disappears but nothing fires. The only major difference is I was using an sqlDataSource object at first but for the new project i had to bind it to a database in the behind code.
Here's my html:
<asp:TemplateField HeaderText="Create Incident">
<ItemTemplate>
<asp:CheckBox ID="Selections" runat="server" ViewStateMode = "Enabled" OnCheckedChanged="CheckBox1_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
and some simple behindcode:
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
Console.WriteLine("clicked!");
}
set AutoPostBack to True to enable post back
<asp:CheckBox ID="Selections" runat="server" ViewStateMode = "Enabled" OnCheckedChanged="CheckBox1_CheckedChanged" AutoPostBack="True" />
I found the solution. I added the conditional the block if(!page.isPostBack) around the stuff in my page load event.
Kept searching for about an hour till I found out that the actual reason why the OnCheckedChanged event was not firing on my page was due to duplication of names. Duh
An input control had a name 'submit' and there also exists a method in my JavaScript that is called submit(). The JavaScript interpreter was confused between the name of the control and a function, every time it needed to fire the event. Changed the name of the control and everything went back to working perfectly.

OnTextChanged loses focus when AutoPostBack is true

I have a ASP.Net webform that has several textboxes. Some of the textboxes have an OnTextChanged event with AutoPostBack set to true.
When the user enters some text and leaves the textbox, I want some code to run. This part works fine.
The problem is that if a user enters some text, then clicks or tabs to another textbox, the OnTextChanged of the current textbox event fires fine but the textbox that the user clicked on does not keep focus. This causes problems because the user thinks they are on the next textbox but they aren't. And no object seems to have the focus.
Is there anything that can be done to make the next object keep focus while the OnTextChanged event of the current textbox fires?
One option is to use <asp:UpdatePanel> Control.
Facts about using it:
The postback request would be made via AJAX.
It would not recreate the whole page HTML.
When the UpdatePanel updates, it replaces the DIV innerHTML, that would make the textbox lose focus (bad point).
To maintain the focus, you would have to avoid the UpdatePanel from updating when the textbox posts back.
You can avoid the update by setting the UpdateMode and ChildrenAsTriggers properties.
Here is an example:
<asp:UpdatePanel ID="uppTextboxes" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="false">
<ContentTemplate>
<asp:TextBox ID="txb1" runat="server" AutoPostBack="true" OnTextChanged="txb1_OnTextChanged" />
<asp:TextBox ID="txb2" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
Since the whole page is recreated on postback on serverside and the browser will recreate all html on clientside, you have to tell ASP.NET that your TextBox needs focus.
Use Page.SetFocus:
Page.SetFocus(IdOfControl);
However, i would prefer not to postback at all if i can. Can't you use a button that the user has to click after he has entered all necessary data?

How can I persist the changes of a text box when it loses focus?

I have several text boxes on a page. I want to save the text in the corresponding TextBox on LostFocus. It is to ensure the data is not lost when power failure or internet connectivity is lost. How can I accomplish something like this?
Another solution would be to use an UpdatePanel. You could then leverage server-side events rather than an event handler. You're markup might look something like this:
<asp:ScriptManager ID="ScriptManager" runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
<asp:TextBox ID="SomeTextBox" runat="server" TextChanged="SomeTextBox_TextChanged" AutoPostBack="true" />
</asp:UpdatePanel>
</asp:ScriptManager
and then in the TextChanged handler you could do your work.
Explanation: the TextChanged event will fire on the text box in which the text was actually changed when the post back occurs. The AutoPostBack property is necessary so that the page will in fact post back when the text box loses focus.
One final thing to remember, you will probably need to leverage this.IsPostBack on the page in the Load handler because every time a control is updated it's going to reconstruct the page. It's common that logic in the Load handler only needs to execute one time, but that would pose a problem if you didn't check to see if it was a post back because then it would execute every single time you left a text box.
Use jquery.
First on attach blur event handler, where you call ajax method to server passing new value of the textbox.
Handle this ajax event on serverside and write your data to the database or anywhere else.
Here is a piece of code that may help.
$('#textbox_id').blur(function() {
$.ajax({url:"handler_url.ashx?textbox_value=" + $('#textbox_id').val()});
});
Then create your ashx handler on server and handle your requests with ProcessRequest method. From there you will have access to Request.QueryString where new value of textbox will be stored.

Event issue with ASP.net Update Panel

I am completely stumped on this and would really appreciate any help.
I am working on a user control that is situated inside of an update panel. There is a button on the form which loads some data. This is working correctly.
There is also a drop-down box to filter the data. Changing this does initiate a post back, however nothing happens. The drop-down box goes back to it's default value the OnSelectedIndexChanged function is never called.
I've put break points in page_prerender and page_preload and both are being hit the post back is definitely occuring. Breakpoints withing the dropdownGroup_changed function are never hit.
Removing the update panel solves the problem, however it breaks the rest of the page so I can't use that for anything other than testing.
I've also verified that there is nothing in my prerender / page load that is resetting the page's state.
Here is the update panel code:
<asp:UpdatePanel ID="UpdatePanel6" runat="server" ChildrenAsTriggers="true" UpdateMode="Conditional" >
<ContentTemplate>
<ucControlName:ControlName ID="ControlName1" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
Here is the drop-down in question - It is located inside of the user control
<asp:DropDownList ID="dropdownGroup" runat="server" Visible="false" AutoPostBack="true" OnSelectedIndexChanged="dropdownGroup_changed"></asp:DropDownList>
It is of course visible and databound by the point in the code where the issue is occuring
A bit more info-
added Both a hard coded dropdown (To rule out a stupid databinding issue) and a textbox to the same control. I have the same issue.
It appears that the event isn't triggering because the values are never changing as far as .net is concerned. I've checked the control during page_init and page_load - the value is always the same.
The fact that the button works but the other controls don't makes me think that there is a view state issue here somewhere but I can't quite ferret out what is causing it. Viewstate is enabled for the page and the panel- don't know if anything else could be overriding / corrupting it.
Did i mention that I hate update panels with a passion? because I hate update panels with a passion.
I suggest checking the 'Value' property for each 'ListItem' in the 'DropDownList' control. If they are all the same even if the 'Text' properties are different, then the 'OnSelectedIndexChanged' will not fire at all since ASP.NET cannot tell if anything has changed (See this related question for more info.)
This was the real cause of my problem even though I, too, had a 'UserControl' with a 'DropDownList' inside an 'UpdatePanel' and the 'AutoPostBack' was firing as expected. I thought the UpdatePanel was the culprit but it was not the case. Each of the items in my DropDownList had the same underlying value of "10" even though they had distinct 'Text' values. I changed them to each have a unique value which then allowed for the OnSelectedIndexChanged event to fire thus fixing the problem.
Two answers for the price of one:
Are you calling DataBind() in your Page_Load? If you do that on a PostBack, you will lose events. Replace the call with the following:
if (!IsPostBack) {
DataBind();
}
If your DropDownList is outside your UpdatePanel, you need to add a Trigger as follows:
<asp:UpdatePanel ID="UpdatePanel6" runat="server" ChildrenAsTriggers="true" UpdateMode="Conditional" >
<Triggers>
<asp:AsyncPostBackTrigger ControlID="dropdownGroup" EventName="SelectedIndexChanged" />
</Triggers>
<ContentTemplate>
<ucControlName:ControlName ID="ControlName1" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
Have you tried UpdatePanel.Update (); after your databind.

Resources