Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking 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 Basics 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
 
Old April 18th, 2007, 01:45 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 428
Thanks: 57
Thanked 2 Times in 2 Posts
Default Field References in User Controls

I have created a user control that is being used multiple times on the same form. It contains a text box that calls a script to count the number of characters as they are entered and display a running total in another field in the same user control:

HTML:
<textarea id="txtAltText" onkeyup="CountAndLimitAltText()" >
</textarea>
(Maximum length: 255; <span id="fldLengthAltText">0</span> entered.)

VBScript:
sub CountAndLimitAltText()
   fldLengthAltText.innerhtml=len(txtAltText.value)
characters.")
End Sub

This code is throwing an error: object required "txtAltText"

The code behind of a user control on a windows application would not have this problem, but that's because it assumes the scope of the field is for the code behind for the instance. However, looking at the source code generated for the web, I notice that the field txtAltText has been renamed "FormGraphic_txtAltText", where FormGraphic is the name of one of the instances of this UserControl. Scope therefore seems unlimited within a web page, and is "limited" by renaming the internal controls.

I can change the reference in the script to "FormGraphic_txtAltText", but then the code won't work for the second instance of the control on the page where it is named "TileGraphic" and this field would then be renamed "TileGraphic_txtAltText". And of course, two controls cannot be named the same thing on the same form, nor do I want to be forced to call this object the same name on all Forms, either.

So, as the web is not maintaining scope the way a windows form would to avoid this issue, is it possible to reference the fields in this control in some relative way within the script (like me.txtAltText), or somehow make it also change the name in the script as it is written to the browser, so that it doesn't matter what the control is named on the form? Or am I going to be forced give my controls the same name on all forms because the control name must be hard coded in the script to run properly? That will also force me to create multiple "versions" of identical controls just so I can use more than one on the same form. Is there a work around for this short coming of user controls in web applciations?
 
Old April 19th, 2007, 07:03 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

(I'm going to assume that the VBScript code you posted is client-side code. This isn't such a good idea if you hope to run this web application on any browser other then internet explorer.)

The problem is that the the html running in the client's browser doesn't know (or care) that the html was created using .NET. So it has to follow the rules of basic html and javascript. Names of controls need to be unique to be useful. So it has to prefix the textbox and other controls names with the instance name of the control they reside in.

You can try something like this:

First you need to ensure that your multiline text box (txtAltText) and the count label (fldLengthAltText) are server controls (the label will render as a <span>).

Then you assign the clientside event call on the textbox from the server-side code so you can pass to the call the fully qualified names of the objects involved:
[Server side code]
txtAltText.Attributes("onKeyUp") = String.Format("CountAndLimitAltText({0}, {1});", fldLengthAltText.ClientID, txtAltText.Client)

Now we just need to modify the client side method to deal with those arguments:
[Client side code]
sub CountAndLimitAltText(label, textbox)
   label.innerhtml=len(textbox.value)
End Sub

You may have to fiddle with the clientside code to get it to work, but this is the basic idea.

-Peter
 
Old April 19th, 2007, 10:04 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 428
Thanks: 57
Thanked 2 Times in 2 Posts
Default

Planoie,

That works, although it's not exactly a work around that leapt to my mind, obviously. Thanks very much!





Similar Threads
Thread Thread Starter Forum Replies Last Post
User controls dcct84 C# 2 October 28th, 2007 07:20 PM
User controls' content: Chapter 2 User Controls AGS BOOK: Professional ASP.NET 2.0 Server Control and Component Development ISBN: 978-0-471-79350-2 10 July 26th, 2007 05:36 AM
User Controls..... Help please cowboy2066 General .NET 2 July 28th, 2004 09:22 AM
User Controls Duncan Pro VB.NET 2002/2003 2 December 1st, 2003 05:41 AM
User Controls raZZa VS.NET 2002/2003 3 July 30th, 2003 07:36 AM





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