Wrox Programmer Forums
|
ASP.NET 1.1 As of 10/6/2005, this forum is locked as part of the reorganization described here: http://p2p.wrox.com/topic.asp?TOPIC_ID=35394. No posts have been deleted. Open ongoing discussions from the last week have been moved to either ASP.NET 1.0 and 1.1 Beginners http://p2p.wrox.com/asp-net-1-0-1-1-basics-60/ or ASP.NET 1.0 and 1.1 Professional. http://p2p.wrox.com/forum.asp?FORUM_ID=50. See my sticky post inside for more.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.1 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 May 4th, 2004, 09:00 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

How does the code in the source of the browser look like?
Instead of a new object, you could also just drop the names of the controls in the array. The looping code could then get a reference to the object using its ID....
Instead of txtLastName, you should then use the control's ClientID property.....

Cheers,

Imar

P.S. Did you delete your previous post?

---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: Privilege by Incubus (Track 1 from the album: Make Yourself) What's This?
 
Old May 4th, 2004, 09:29 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
Send a message via Yahoo to melvik
Default

u can access that Server side Controls by
Code:
FormName.elements[Control].[u]value</u>= "newValue";
HTH.

Always:),
Hovik Melkomian.
 
Old May 4th, 2004, 12:29 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Quote:
quote:Originally posted by Imar


Instead of txtLastName, you should then use the control's ClientID property.....

P.S. Did you delete your previous post?
I'm not sure I'm following you about the control's ClientID property.

And yes. I felt that it was a waste of a post.

 
Old May 4th, 2004, 02:17 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

The ClientID property will return the ID of the control as it will eventually end up in the html source of the page.
Usually, the ClientID matches the ID of the control; e.g. a TextBox with an ID of txtTest will have an HTML attribute of txtTest.
However, controls nested in other controls, like a Repeater or DataGrid will have the ID of their container control as a prefix in their name, e.g. gridMyGrid__ctl3__txtTest for a TextBox control *inside* a DataGrid called gridMyGrid. This ensures that every HTML object has a unique ID throughout the enitre page (which is a requirement to use the control in JavaScript).

Consider the following example. At run-time I create a number of TextBoxes. In the example below I create only two, but I could just as easy create an unknown number of controls dynamically, in a loop for example. The controls are added to a PlaceHolder control.
Code:
TextBox txtBox1 = new TextBox();
TextBox txtBox2 = new TextBox();
txtBox1.ID = "txtBox1";
txtBox2.ID = "txtBox2";
PlaceHolder1.Controls.Add(txtBox1);
PlaceHolder1.Controls.Add(txtBox2);
RegisterArrayDeclaration("myArray", 
  String.Format("'{0}', '{1}'", txtBox1.ClientID, txtBox2.ClientID));
When the page has finished loading, you'll end up with the following JavaScript:
Code:
<script language="javascript">
<!--
  var myArray =  new Array('txtBox1', 'txtBox2');
// -->
</script>
You can use this (dynamically generated) array inside a JavaScript function and do something with the controls. For example, you can see if they contain a value or not, as the following code demonstrates:
Code:
<script type="text/javascript">
  function CheckControls()
  {
    for (var i = 0; i < myArray.length; i++)
    {
      if (document.getElementById(myArray[i]).value == '')
      {
        alert('Control ' + myArray[i] + ' cannot be empty');
      }
    }
  }
</script>
This code uses the dynamic array and loops through it. Using the client side ID of the control as a parameter for the getElementById method, you can uniquely access each control in your page and with it whatever you see fit.

If you would add the controls to a repeated control, like a Repeater or DataGrid, you'll see that the ClientID of the control will return something different from its ID property, allowing you to uniquely identify the controls in the repeated items (e.g. TextBox1 for row 1 has a different ClientID than TextBox1 in row 2)

Of course this is a simple and rather limited example, but I am sure you can see the power of the RegisterArrayDeclaration method in combination with dynamic controls and client side JavaScript for validation and other purposes.

If you have any questions, feel free to ask.

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: Hysteria by Muse (Track 8 from the album: Absolution) What's This?





Similar Threads
Thread Thread Starter Forum Replies Last Post
Validation controls and postback sdi126 ASP.NET 2.0 Basics 5 November 11th, 2007 12:08 PM
Validation controls in datalist anujrathi ASP.NET 1.0 and 1.1 Professional 4 July 26th, 2006 03:32 PM
Validation Controls andyj00 ASP.NET 1.0 and 1.1 Professional 3 June 9th, 2005 09:19 AM
validation involving 2 controls Renu ASP.NET 1.0 and 1.1 Basics 24 December 21st, 2004 06:33 AM





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