473,545 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
+ 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(objec t sender, System.EventArg s e)
{
if (!IsPostBack)
{
btnAdd.Attribut es.Add("OnClick ", ECMA.SetFocus(" txtNewItem"));
}
}
Relevant html output:

<input name="txtNewIte m" 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="docume nt.getElementBy Id('txtNewItem' ).focus();"/>

<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 1208
Hi David,
Try this :
if (!IsPostBack)
{
Page.RegisterSt artupScript("Se tFocus", "<script language= 'Jscript' >
document.getEle mentById('txtNe wItem').focus() ; </script>");

}

Hope this helps.

Regards,

Marshal Antony

..NET Developer

http://www.dotnetmarshal.com

"David Beardsley" <db**********@h otmail.com> wrote in message
news:a2******** *************** ***@posting.goo gle.com...
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(objec t sender, System.EventArg s e)
{
if (!IsPostBack)
{
btnAdd.Attribut es.Add("OnClick ", ECMA.SetFocus(" txtNewItem"));
}
}
Relevant html output:

<input name="txtNewIte m" 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="docume nt.getElementBy Id('txtNewItem' ).focus();"/>

<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**********@h otmail.com> wrote in message
news:a2******** *************** ***@posting.goo gle.com...
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(objec t sender, System.EventArg s e)
{
if (!IsPostBack)
{
btnAdd.Attribut es.Add("OnClick ", ECMA.SetFocus(" txtNewItem"));
}
}
Relevant html output:

<input name="txtNewIte m" 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="docume nt.getElementBy Id('txtNewItem' ).focus();"/>

<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
3798
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 reverse. This is causing logic problems and it really bothers me that this sequence of events fires inconsistently. Can anyone tell me why this happens...
2
1350
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 "onLoad" and "onClick") by calling the javascript method <object>.disabled = <true/false>. This works visably fine on the webpage, however, when I do...
4
5093
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 address book on the left, and you build the message recipients on the right. * an asp:button that, when clicked, fires off some code to create and...
3
2244
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 confirmation. So I've got <body onUnLoad="CheckConfirm();"> The javascript method fires off, and I've got no question
1
4942
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 __doPostBack(eventTarget, eventArgument) {... But my page is still posting back just fine with JavaScript disabled (while other JavaScript-dependent features...
9
2295
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 using attributes in the *.aspx page. Everything displays great for the initial load, but whenever I try to add or delete an item (I have controls to do...
2
1414
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 another usercontrol to the page. Thanks
2
37672
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 functionality in the user's browser without having to make calls back to the server; thus, saving resources. The following example demonstrates how...
1
3437
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 have to made in my code <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"...
0
7802
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7410
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5958
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5318
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4940
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3441
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3436
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1865
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 we have to send another system
0
692
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.