472,143 Members | 1,124 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,143 software developers and data experts.

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 1178
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Harry | last post: by
3 posts views Thread by Weston Weems | last post: by
1 post views Thread by Frankie | last post: by
9 posts views Thread by Nathan Sokalski | last post: by
2 posts views Thread by =?Utf-8?B?SHV6ZWZh?= | last post: by

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.