JavaScript events and postbacks

D

David Beardsley

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').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
 
M

Marshal Antony

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
 
D

David Beardsley

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.
 
T

Teemu Keiski

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


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').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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top