473,471 Members | 2,037 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

JavaScript events and postbacks

My web form has one edit box, a button, a list box and a combo box.
When the button is clicked the text in the edit box is added to the
list box and the combo box. After the new text is added I want to
clear the text of the edit box and set focus back to the text box.
I setup the event here:

private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
btnAdd.Attributes.Add("OnClick", ECMA.SetFocus("txtNewItem"));
}
}
Relevant html output:

<input name="txtNewItem" type="text" id="txtNewItem" />
<input name="TextBox2" type="text" id="TextBox2" style />

<select name="cbItem" id="cbItem">...</select>

<input type="submit" name="btnAdd" value="Add" id="btnAdd"
OnClick="document.getElementById('txtNewItem').foc us();"/>

<select name="lbItem" size="4" id="lbItem">...</select>
When I run the application the focus is not set as desired. What
appears to happen is the focus is set momentarily, then the postback
event fires and the focus shifts again.

My question is, how do you properly manage the client side focus
around the postback events?

TIA!

David Beardsley
Nov 18 '05 #1
3 1206
Hi David,
Try this :
if (!IsPostBack)
{
Page.RegisterStartupScript("SetFocus", "<script language= 'Jscript' >
document.getElementById('txtNewItem').focus(); </script>");

}

Hope this helps.

Regards,

Marshal Antony

..NET Developer

http://www.dotnetmarshal.com

"David Beardsley" <db**********@hotmail.com> wrote in message
news:a2**************************@posting.google.c om...
My web form has one edit box, a button, a list box and a combo box.
When the button is clicked the text in the edit box is added to the
list box and the combo box. After the new text is added I want to
clear the text of the edit box and set focus back to the text box.
I setup the event here:

private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
btnAdd.Attributes.Add("OnClick", ECMA.SetFocus("txtNewItem"));
}
}
Relevant html output:

<input name="txtNewItem" type="text" id="txtNewItem" />
<input name="TextBox2" type="text" id="TextBox2" style />

<select name="cbItem" id="cbItem">...</select>

<input type="submit" name="btnAdd" value="Add" id="btnAdd"
OnClick="document.getElementById('txtNewItem').foc us();"/>

<select name="lbItem" size="4" id="lbItem">...</select>
When I run the application the focus is not set as desired. What
appears to happen is the focus is set momentarily, then the postback
event fires and the focus shifts again.

My question is, how do you properly manage the client side focus
around the postback events?

TIA!

David Beardsley

Nov 18 '05 #2
I believe that will set the inital focus when the form loads. What I am
trying to do is (re)set the focus after the button is clicked.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 18 '05 #3
Hi,

there's also free control to achieve this:

http://www.metabuilders.com/Tools/FirstFocus.aspx

The control it sets focus to can be specified in code despite that at the
page is said "after the page has loaded" :-)

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist
http://blogs.aspadvice.com/joteke
"David Beardsley" <db**********@hotmail.com> wrote in message
news:a2**************************@posting.google.c om...
My web form has one edit box, a button, a list box and a combo box.
When the button is clicked the text in the edit box is added to the
list box and the combo box. After the new text is added I want to
clear the text of the edit box and set focus back to the text box.
I setup the event here:

private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
btnAdd.Attributes.Add("OnClick", ECMA.SetFocus("txtNewItem"));
}
}
Relevant html output:

<input name="txtNewItem" type="text" id="txtNewItem" />
<input name="TextBox2" type="text" id="TextBox2" style />

<select name="cbItem" id="cbItem">...</select>

<input type="submit" name="btnAdd" value="Add" id="btnAdd"
OnClick="document.getElementById('txtNewItem').foc us();"/>

<select name="lbItem" size="4" id="lbItem">...</select>
When I run the application the focus is not set as desired. What
appears to happen is the focus is set momentarily, then the postback
event fires and the focus shifts again.

My question is, how do you properly manage the client side focus
around the postback events?

TIA!

David Beardsley
Nov 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: V | last post by:
I have found that when I have a composite control that uses the CreateChildControls method, on a regular page load, Page_Load executes before CreateChildControls, but on a postback it is the...
2
by: Harry | last post by:
Hello, I have an ASP.Net web page populated with WebControls, such as textboxes and checkboxes. On this page I also have a JavaScript that disables these textboxes under certain events (such as...
4
by: Bob P. | last post by:
Hello, I have a page with: * two side-by-side asp:listboxes and two arrow asp:buttons allowing users to add/remove email addresses between them -- very much like Outlook, where you have the...
3
by: Weston Weems | last post by:
Ok, I've posted on this before, lemme give you guys the run down. I've got an asp form that has postbacks etc. I'd like it so that when navigating away from it, It'll prompt user for save...
1
by: Frankie | last post by:
Just doing some testing here to see what happens when JavaScript is disabled. I was told somewhere along the line that PostBacks would fail because they rely on JavaScript: function...
9
by: Nathan Sokalski | last post by:
I have a very simple UserControl which contains an Image and a Label, which I use to display an image with a caption. I am using this control inside a DataList, setting the two Public variables...
2
by: =?Utf-8?B?SHV6ZWZh?= | last post by:
Hi, I am using a Multipage control with ASP.Net 1.1. The events for controls within the multipage are not getting fired. Any ideas why this would happen. It was working fine before i added...
2
Frinavale
by: Frinavale | last post by:
JavaScript in ASP.NET Using JavaScript in ASP.NET pages can make your application seem to work faster and prevent unnecessary calls to the server. JavaScript can be used to perform client-side...
1
by: swethak | last post by:
hi, i have a code to disply the calendar and add events to that. It works fine.But my requirement is to i have to disply a weekly and daily calendar.Any body plz suggest that what modifications i...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.