Client Side Javascript

J

Julie Barnet

I just posted a question, but decided to make it clearer...sorry about
the re-post.

I am developing an asp.net page with 3 server side asp controls
(dropdownlist, textbox, button). I would like to capture the tab from
the dropdownlist to force a postback and then move focus to the
textbox.

I'm not sure if I'm doing this right, but here is my javascript
function:
The postback works fine (but should I be using the clientID, instead
of the server control name?) The focus will not work. When I press
tab it posts back and then focus is lost to all controls)

<script language="JavaScript1.1"><!--
function keyPressed(this_field, next_field) {
key=event.keyCode;
if (key==9)
{
__doPostBack(this_field,'');
document.all('next_field').Focus();
}

return false;
}
//--></script>

My dropdownlist has an attribute:
onKeyDown="keyPressed('cmbTest', 'txtTest')"

Any help would be greatly appricated!!

Julie Barnet
 
K

Ken Cox [Microsoft MVP]

Hi Julie!

This was an interesting challenge!

What I came up with seems to work. You add the "onblur" attribute to your
dropdownlistbox and get ASP.NET to generate a postback reference to it. Then,
if this is a postback, emit client-side script to set the focus to the textbox.
The code is below.

Does this help?

Ken
MVP [ASP.NET

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
DropDownList1.Attributes.Add _
("onblur", "alert('Doing postback');" & _
Page.GetPostBackEventReference(DropDownList1))
If IsPostBack Then
Page.RegisterStartupScript _
("focusit", "<script language='javascript'> " & _
"document.forms[0].TextBox1.focus();</script>")
End If
End Sub

<form id="Form1" method="post" runat="server">
<p>
<asp:dropdownlist tabindex="2" id="DropDownList1" runat="server"
autopostback="False">
<asp:listitem value="red">red</asp:listitem>
<asp:listitem value="blue">blue</asp:listitem>
<asp:listitem value="gree">gree</asp:listitem>
</asp:dropdownlist></p>
<p>
<asp:textbox id="TextBox1" runat="server"></asp:textbox></p>
<p>
<asp:button id="Button1" runat="server" text="Button"></asp:button></p>
</form>

--
Microsoft MVPs have a question for *you*: Are you patched against the Worm?
http://www.microsoft.com/security/security_bulletins/ms03-026.asp



I just posted a question, but decided to make it clearer...sorry about
the re-post.

I am developing an asp.net page with 3 server side asp controls
(dropdownlist, textbox, button). I would like to capture the tab from
the dropdownlist to force a postback and then move focus to the
textbox.

I'm not sure if I'm doing this right, but here is my javascript
function:
The postback works fine (but should I be using the clientID, instead
of the server control name?) The focus will not work. When I press
tab it posts back and then focus is lost to all controls)

<script language="JavaScript1.1"><!--
function keyPressed(this_field, next_field) {
key=event.keyCode;
if (key==9)
{
__doPostBack(this_field,'');
document.all('next_field').Focus();
}

return false;
}
//--></script>

My dropdownlist has an attribute:
onKeyDown="keyPressed('cmbTest', 'txtTest')"

Any help would be greatly appricated!!

Julie Barnet
 
J

Julie Barnet

Ken,
The onblur does not seem to work with this control. I'm not sure why?
I think I will have to use the onkeydown clientside javascript event
(I'm not sure how I will take care of it if the user clicks off the
control - onclick is when the click on the control, right?)
www.thecodeproject.com/aspnet/combobox.asp

Also, can I do a registerclientscriptblock from a controls onprerender
event handler? Then I can allow focus to go to different controls
based on the current control.

Thanks for the suggestion
Julie
 

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,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top