set focus on postback/textchanged

G

Guest

I'm setting focus depenting upon which field has changed value. I'm doing
this with the following: Page.RegisterStartupScript("SetFocus", "<script
language=""Jscript"" > document.getElementById(""txtCity"").focus();
</Script>"). This is being called on the textChanged event for textboxes. for
city it goes to state for state it goes to zip, ...
It works, but is this the correct way to do this or should this be a seties
of if then else's in he page load? if so, how can i isolate the textbox that
caused the event?

thanks
kes
 
G

Guest

You can change the javascript you have below to generate the name of the
control that caused the event:

Page.RegisterStartupScript("SetFocus", "<script language='Jscript'>
document.getElementById(" + controlName + ").focus(); </Script>").

in the "Changed" event of the control just set the "controlName" variable to
the required javascript id for the control:

i.e. for the txtCity changed event:

controlName = "txtCity";

I'm assuming that you are calling the Page.RegisterStartupScript() function
in the "Render" event of the page - you'll need to remember that because the
"Render" is called after the "Changed" event and thats what you want, if you
put the Page.RegisterStartupScript() in the Page_Load() by mistake, then the
"controlName" variable will not be assigned a value yet.
 
G

Guest

Thank you for replying!

i have the function call in each of the respective controls' changed event:
Example:
Private Sub txtbAdr1_TextChanged(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles txtbAdr1.TextChanged
If chkShipSame.Checked Then
txtSadr1.Text = txtbAdr1.Text
Page.RegisterStartupScript("SetFocus", "<script
language=""Javascript"" > document.getElementById(""txtbadr2"").focus();
</Script>")
End If
End Sub

In vS.net i could not find the Render event under page, but i'm still
learning this. as well as JavaScript. I see PreRender, but not render.
1. My guess is the funtion calls are not in the right place, but should only
be one function call in the Render event?
2. where can i find Render?
3. the controlname variable is this a simple .net string variable or a
javascript variable?
Thanks
kes
 
G

Guest

Sorry, I meant the "PreRender" event, not the "Render".

Answers:
1. Yes, but use "PreRender" not "Render"
2. Don't worry about Render
3. The controlName variable is a field of the "Page" class that you create
and all the "Change" events can set it to the javascript ID of the control.

The page events are typically executed in the following order:
a) Init
b) Load
c) Change Events
d) Click Events
e) PreRender
f) Render

REF:
http://msdn.microsoft.com/library/d...guide/html/cpconcontrolexecutionlifecycle.asp

ASP.NET 2.0 will have an enhanced page event life cycle with more events
that you can hook into.
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,142
Latest member
arinsharma
Top