ASP.NET Textbox: OnTextChange jumps to top of page because of autopostback - asp.net

I'm using the OnTextChange event on a textbox on an ASP.NET page. To have this working I have to put 'AutoPostBack=true'.
The problem is that the textbox is on the bottom of the page, and when the text changes it has to fill another textbox. This is working fine but when the event triggers the page refreshes and jumps to the top of the page, so I always have to scroll down again to see it. (Due to the autopostback)
Is there anything I can do to prevent it to jump to the top of the page?

Use UpdatePanel and put that text box for which u r triggering OnTextChange event in of from ajax extensions and dont forget to include scriptmanager at top of the page
the code goes like this
<asp:UpdatePanel>
<content>
<asp:TextBox runat="server" AutoPostBack="true" OnTextChanged="textbox_textchanged">
</asp:TextBox>
</content>
</asp:UpdatePanel>

In the page_load event write below code.
this.MaintainScrollPositionOnPostBack = true;

Related

on every postback page opens in a new tab

I have a weird problem in my page. I have a button called Print. I wanted report should come in a new tab, so i wrote some javascript on button's onClientClick. Which works great with no problem at all.
But problem starts now when user comes back on original page again, now here i have several controls which cause postback. Say for example its a dropdownlist. so whenever user changes dropdown item it causes postback which is fine but everytime it opens a new tab on every postback.
hope I am clear in question...
Any help??
Here is a code:
<asp:Button ID="btnshow" runat="server" Text="Show" Font-Bold="true" ForeColor="Black" Width="90px" OnClick="btnshow_Click" OnClientClick ="document.forms[0].target = '_blank';"/>
The issue is document.forms[0].target='_blank' is setting the target on the form not the individual button so a postback triggered by any control will open in a new tab.
You should use a HyperLink control instead of the Button control. The HyperLink control has a Target property which allows you to specify how the link should be opened.
Below is an example taken from the HyperLink documentation. This will render an anchor tag with target="_blank".
<asp:HyperLink ID="lnkPrint" NavigateUrl="http://www.microsoft.com" Text="Print" Target="_blank" runat="server" />

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?

ASP.NET OnTextChanged not firing from inside an update panel

I am using an ASP.NET update panel to retrieve user info using the on TextChanged for the textbox, here is my code:
<asp:UpdatePanel runat="server" ID="up1" ChildrenAsTriggers="true">
<ContentTemplate>
<asp:TextBox runat="server" ID="loginEmail" Text="Email"
CssClass="textBoxes" OnTextChanged="userInfo" AutoPostBack="true"></asp:TextBox>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="loginEmail" EventName="TextChanged" />
</Triggers>
</asp:UpdatePanel>
and the code behind:
string url, emailInfo;
emailInfo = loginEmail.Text;
url = Membership.GetUserNameByEmail(emailInfo);
emailText.InnerText = "Email: " + emailInfo;
urlText.InnerText = "Webiste: http://www.Elwazefa.com/User/" + url ;
the code wont fire on textchanged but it wil,
on ButtonClick or PageLoad.
What is the problem?
Using ASP.NET 4.0
<asp:TextBox AutoPostBack="true" OnTextChanged="thingId_TextChanged" ID="thingId" runat="server"></asp:TextBox>
AutoPostBack on text-box will trigger post-back when the focus is lost from the text box. TextChanged event will be fired on any subsequent post-back (can be due to button-click or text box focus change). So you need to make certain
After text is changed, you are moving out of text-box
Whatever controls that you are change are part of update-panel (can be different update panel). If there are not part of any update panel then those changes won't get reflected on client side.
My guess is you are probably suffering from #2. You can use tool such as Fiddler (or Firebug on FireFox) to check if browser is firing AJAX (XHR) request when the focus is lost from the text-box.
As #VinayC posted, AutoPostBack means that the page will postback to the server when your TextBox loses focus. No built-in event causes postback on every character added to a text input, and for good reason. UpdatePanel postbacks don't cause the page to flicker, but they can be just as heavy as a full postback.
If you want to work around this, you can give your textbox a client onchanged event handler, the JavaScript of which will be built from Page.ClientScript.GetPostBackEventReference().
The correct solution would be to use an AJAX method call from your JavaScript code rather than an UpdatePanel partial postback in onchanged.

IFrame not to be reloaded on ASP.NET postback

I have an IFrame on my asp.net page along with other user controls such as radiobutton, textbox, etc.
A problem that I am facing right now is that when I click on a radio button, it triggers the postback which make my IFrame reload as well.
Is there any way I could make the IFrame not to reload on the postback?
Thanks.
Set AutoPostBack="false" porperty of all asp.net controls or use update panel <asp:RadioButton Text="text" AutoPostBack="false" runat="server" />

Refresh a control on the master page after postback

What i am trying to do here is to show a couple of validation messages in form of a bulletlist, so i have a Div on my master page containing a asp:bulletlist. Like this:
<div>
<asp:BulletedList ID="blstValidationErrorMessage" runat="server" BulletStyle="Disc">
</asp:BulletedList>
</div>
When i then click the Save button from any of my pages (inside the main contentPlaceHolder) i create a list of messages and give this list as datasouce like this:
blstValidationErrorMessage.DataSource = validationMessageCollection;
blstValidationErrorMessage.DataBind();
The save button is located inside an updatepanel:
asp:UpdatePanel runat="server" ID="UpdatePanel" ChildrenAsTriggers="true" UpdateMode="Conditional">
Nothing happens, i can see that the datasource of the bulletlist contains X items, the problems must arise because the Save button is inside an update panel and the elements outside this updatepanel (master page controls for example) is not refreshed.
So my question is, how do i make the bulletlist refresh after the postback?
Thanks in advance.
If your button is inside an UpdatePanel, you should place your BulletedList control inside an UpdatePanel too.
You can place an UpdatePanel surrounding the BulletedList in the MasterPage file. Set "UpdateMode" to "Conditional" then call the Update method of the UpdatePanel to refresh only when needed ('save button' click for example).
The Save button will only update the contents of the UpdatePanel you placed it in. Here's what I recommend doing:
Move the SaveButton outside of the UpdatePanel. Where you put it I'll leave up to you.
Put your validation div inside another UpdatePanel. Call it ValidationUpdatePanel
Add your SaveButton as an AsyncPostbackTrigger for both update panels. Since you may have each UpdatePanel separated into different controls/pages, you'll probably want to do this in the code-behind programmatically.

Resources