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.
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.
|
|