Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Professional
| Search | Today's Posts | Mark Forums Read
ASP.NET 2.0 Professional If you are an experienced ASP.NET programmer, this is the forum for your 2.0 questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old February 18th, 2009, 12:10 PM
Registered User
 
Join Date: Jun 2005
Location: London, , United Kingdom.
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to barkerl
Default Adding Client-Side Features to Server Controls

Hi,

I'm using the OnPreRender method within a Server Control to generate some client side JavaScript, which is to bring focus to a textbox. The method is as follows:

Code:
protected override void OnPreRender(EventArgs e)
{
Page.ClientScript.RegisterStartupScript(typeof(Page),
"ControlFocus", "document.getElementById('" + this.ClientID + 
"').focus();", true);
}
When the web page is displayed, the page source (relevant bits) is as follows:

Code:
<html>
....
<div>
<span id="WebCustomControl1_1" style="display:inline-block;background-
color:Green;font-weight:bold;height:28px;width:152px;">
 <div>
   <input type="text" id="WebCustomControl1_1" name="WebCustomControl1_1"
   value="Hello Skin" id="WebCustomControl1_1" style="display:inline-block;
   background-color:Green;font-weight:bold;height:28px;width:152px;" />
 </div>
</span>
</div>
 
<script type="text/javascript">
//<![CDATA[
document.getElementById('WebCustomControl1_1').focus();//]]>
</script>
...
</html>
The focus does not go to the textbox.

Can anyone help with this? I'm working through a textbook example (Wrox C# Pro Ch.23) and there's nothing on the erratum page. If anyone has some of their own examples they'd like to share, that would be appreciated.
  #2 (permalink)  
Old February 21st, 2009, 08:22 PM
Friend of Wrox
Points: 894, Level: 11
Points: 894, Level: 11 Points: 894, Level: 11 Points: 894, Level: 11
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2005
Location: London, , United Kingdom.
Posts: 166
Thanks: 2
Thanked 33 Times in 33 Posts
Default

I dont know the book so I don't know what the full code is meant to do, but looking at the code snippet, you have named both the containing span and the textbox WebCustomControl1_1, so when you call getElementById, it will find the span first and the focus() won't have any effect. Just try changing the id of the span.
You've also got id="WebCustomControl1_1" twice in the textbox. This won't break it, but shouldn't really be done.

Hope this helps
Phil
  #3 (permalink)  
Old February 22nd, 2009, 01:44 PM
Registered User
 
Join Date: Jun 2005
Location: London, , United Kingdom.
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to barkerl
Default

Philip, thanks for your reply.

I removed the <span> tag and it cured the problem. The code was auto generated and I never wanted the span tag to appear anyway.

It turns out to be related to the System.Web.UI.HtmlTextWriterTag that automatically sends out this tag unless you override it.

http://msdn.microsoft.com/en-us/libr...ox.tagkey.aspx


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem Converting Client-side to Server-side Code kwilliams ASP.NET 2.0 Professional 1 November 21st, 2007 05:25 PM
ASP.NET Server Controls and Client-Side Scripts LoneStar1 BOOK: Professional ASP.NET 2.0 and Special Edition; ISBN: 978-0-7645-7610-2; ISBN: 978-0-470-04178-9 5 March 15th, 2006 05:51 PM
Firing server side events at client side codes mehdi62b ASP.NET 1.0 and 1.1 Basics 6 May 18th, 2005 09:11 AM
sharing a server-side variable with client-side pigtail Javascript How-To 6 November 4th, 2004 02:01 AM
Accessing Server Side Data on Client Side steve456 Classic ASP Professional 3 October 15th, 2003 02:33 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.