codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:



Home » ASP » Article

ASP.NET : Control Attributes

Article by: David Sussman, et al (7/8/2002)
Bookmark us now! Add to Favourites
Email a friend! Tell a friend
Sponsored by: FindMyHosting - Web Hosting Search
Summary: How do we get hold of the <asp:label> control attributes within our ASP.NET code? Well, we can do this by adding some ASP.NET code at the head of the HTML, as in the following example
Viewed: 19598 times Rating (3 votes): 
 3 out of 5
  Rate this Article   Read Comments   Post Comments


Previous Page  Page 1 Page 2 Page 3 Page 4 Page 5 Page 7 Page 8 Page 9 Page 10 Page 11 Page 12 Page 13 Next Page  

Control Attributes

This still leaves one question unanswered though. How do we get hold of the <asp:label> control attributes within our ASP.NET code? Well, we can do this by adding some ASP.NET code at the head of the HTML, as in the following example. First delete the Text attribute at the end of both <asp:label> controls:

 

...

<asp:label id="Message1" runat="server" />

you have selected to receive information about

<asp:label id="Message2" runat="server" />

...

 

Now add the following ASP.NET script block before your HTML code:

 

<script language="C#" runat="server">

  void Page_Load()

  {

    Message1.Text = "Vervain";

    Message2.Text = "Madrid";

  }

</script>

 

<html>

<head>

  <title>Label Control page</title>

... 

 

You must make sure that you type this part in exactly, using exactly the same case in the letters, otherwise it won't work. So if you typed Void page_load() for example instead of void Page_Load() then you would get an error. The same holds true for all of the examples in this chapter and indeed the book. If any example doesn't work when you first try it, check the cases are correct. This is because C# is a case-sensitive language. It can be awkward to get use to at first but once you get used to it, it will be like second nature.

 

So, if you run the example now, you'll see that the output has changed:

 


Ignore the void Page_Load() statement and it's accompanying curly braces (we'll discuss this in Chapter 7): it's the code contained within the curly braces that's important for now:

 

Message1.Text = "Vervain";

 

In the first line Message1 refers to the identity of our first <asp:label> control, while the Text attribute refers to the actual text that we wish to set. So we're saying "set the Method1 <asp:label>'s Text attribute to Vervain". The second line:

 

Message2.Text = "Madrid";

 

does just the same for the Message2 <asp:label> control, setting the text attribute to Madrid.

This is reflected in the final display of our web form. This ASP.NET code has allowed us to directly influence the contents of the <asp:label> control, and set it separately, independently from our HTML code. You should also note that both lines are ended by a semi-colon. In C#, every line should be completed by a semi-colon.

 

So all of the HTML server controls are created as separate objects, which you can access with ASP.NET code. These objects allow you to reference any of the attributes of the server control as follows:

 

[ServerControl].[ServerControlAttribute]

 

So you could make the control invisible by setting the Visible attribute to false:

 

Message.Visible = "false";

 

Let's now move on to a more complex control.

Previous Page  Page 1 Page 2 Page 3 Page 4 Page 5 Page 7 Page 8 Page 9 Page 10 Page 11 Page 12 Page 13 Next Page  




Click here to Buy!

Buy Beginning ASP.NET with C# here

© Copyright 2002 Wrox Press This chapter is written by David Sussman, et al and taken from "Beginning ASP.NET with C#" published by Wrox Press Limited in June 2002; ISBN 1861007345; copyright © Wrox Press Limited 2002; all rights reserved.

No part of these chapters may be reproduced, stored in a retrieval system or transmitted in any form or by any means -- electronic, electrostatic, mechanical, photocopying, recording or otherwise -- without the prior written permission of the publisher, except in the case of brief quotations embodied in critical articles or reviews.






CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums
Rate this article:     Poor Excellent
View highlighted Comments
User Comments on 'ASP.NET : Control Attributes'


To post comments you need to become a member. If you are already a member, please log in .

 



RELATED ARTICLES
ASP Format Date and Time Script
by Jeff Anderson
An ASP script showing the variety of date and time formats possible using the FormatDateTime Function.
Creating a Dynamic Reports using ASP and Excel
by Jeff Anderson
A simple way to generate Excel reports from a database using Excel.
Create an ASP SQL Stored Procedure
by Jeff Anderson
A beginners guide to setting up a stored procedure in SQL server and calling it from an ASP page.
ASP Shopping Cart
by CodeToad Plus!
Complete source code and demo database(Access, though SQL compatible) to an ASP database driven e-commerce shopping basket, taking the user through from product selection to checkout. Available to CodeToad Plus! Members
Email validation using Regular Expression
by Jeff Anderson
Using regular expression syntax is an exellent way to thoroughly validate an email. It's possible in ASP.
Creating an SQL Trigger
by Jeff Anderson
A beginners guide to creating a Trigger in SQL Server
The asp:checkbox and asp:checkboxlist control
by David Sussman, et al
Checkboxes are similar to radio buttons, and in HTML, they were used to allow multiple choices from a group of buttons.
ASP.NET Forum Source Code
by ITCN
Complete open source website Forum and Discussion Board programmed in Microsoft dot Net 1.1 Framework with Visual Basic.
The asp:listbox control
by David Sussman, et al
The next HTML server control that we'll look at, <asp:listbox>, is very much related to <asp:dropdownlist>.
Concatenate strings in sql
by Jeff Anderson
A brief introduction to concatenating strings in an sql query (using SQL server or access databases).








Recent Forum Threads
•  Re: User input. Command line application.
•  email in bulk
•  problem displaying
•  Re: dynamic crystal report generation
•  reading text files line by line
•  Re: crystal report in windows C#.NET
•  is null or not an object error
•  Need to control remote machine
•  Re: convert minutes into hours and minutes


Recent Articles
What is a pointer in C?
Multiple submit buttons with form validation
Understanding Hibernate ORM for Java/J2EE
HTTP screen-scraping and caching
a javascript calculator
A simple way to JTable
Java Native Interface (JNI)
Parsing Dynamic Layouts
MagicGrid
Caching With ASP.Net


© Copyright codetoad.com 2001-2006