How to conditionally set focus to control in ASP.NET

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I see several articles on using javascript to set focus to a control. I need to conditionally set focus to any of several controls, depending on what just happened on the server side. What can I do on the server side to send a message to to the javascript so it can set an appropriate focus?
 
Hi Bruce,

Thanks for posting in the community.
What can I do on the server side to send a message to to the javascript so
it can set an appropriate focus?

I am somewhat confusion about send a message to the client. Do you mean you
hope the Serverside will active post back the message to the client? If so,
I think we can not do that. Because Browser/Server mode is based on pull
mode, its working process is that the client(Browser) sends request to the
WebServer, and the WebServer handle the request and post
back(HTML,javascript) to the client side(browser).
Have I fully understood you? If there is anything I misunderstood, please
feel free to let me know.



Howerver, you can achieve your aim by click a button on the the webpage, in
the button click event in code behind, you can use registerstartupscript to
write the javascript from the serverside to the clientside.
e.g.
private void Button1_Click(object sender, System.EventArgs e)
{
String scriptString = "<script language=JavaScript>alert(\"hello\");<";
scriptString += "/";
scriptString += "script>";
if(!this.IsStartupScriptRegistered("Startup"))
this.RegisterStartupScript("Startup", scriptString);

}

Also if you do not want to click the button or do other user interactive
things, you may need to write a javascipt to send request to the server
every a period of time. Here is a sample, you may take a look.

http://groups.google.com/groups?hl=zh-CN&lr=&ie=UTF-8&oe=UTF-8&selm=$9sGgp
0YDHA.2172%40cpmsftngxa06.phx.gbl&rnum=9


Please apply my suggestion above and let me know if it helps resolve your
problem.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Although I've been developing for 20 years, I'm new to ASP.NET - only having used Visual Studio .NET. I'm unfamiliar with with how to dynamically change java or HTML code - can it be done from the code-behind page? Perhaps you could send me a small sample

Thank you for your help.
 
Hi Bruce,

Have you tried the code snippet in my reply?

Please feel free to let me know if you have any concern on that issue.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top