ASP.NET
  Home arrow ASP.NET arrow Page 7 - ASP.NET Basics (Part 7): Command and Contr...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP.NET

ASP.NET Basics (Part 7): Command and Control
By: Harish Kamath (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 33
    2003-11-24

    Table of Contents:
  • ASP.NET Basics (Part 7): Command and Control
  • The Last Action Hero
  • Requesting More
  • Test Drive
  • The Taste Test
  • A Matter of Control
  • Alien Invasion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    ASP.NET Basics (Part 7): Command and Control - Alien Invasion


    (Page 7 of 7 )

    Of course, there is a lot more to these ASP.NET controls than just labels and text boxes. Take a look at the next example that demonstrates other form controls such as checkboxes, radio buttons, drop down lists and much more.


    <script language="c#" runat="server">
    void Page_Load()
    {   
        
    // check if the form has been submitted
        
    if(Request.QueryString["submit"] == "Register"
        {
        
            
    output.Text "You have entered the following:
    "
    ;
            
            
    // name field
            
    output.Text output.Text "Name:" name.Text "
    "
    ;
            
            
    // species field
            
    output.Text output.Text "Species:" species.Text "
    "
    ;
            
            
    // species type field
            
    output.Text output.Text "Species type: " speciesType.SelectedValue
    "
    "
    ;
            
            
    // immunization field
            
    output.Text output.Text "Immunization against: ";
            for(
    int counter 0counter immunization.Items.Countcounter++) 
            {
                if(
    immunization.Items[counter].Selected
                {
                    
    output.Text output.Text immunization.Items[counter].Text ",";
                }    
            }    
            
            
    output.Text output.Text "
    "
    ;

            
    // address field
            
    output.Text output.Text "Address:" address.Text "
    "
    ;
                            
        }
        
    }    

    </script>

    <html>
    <head>
    <basefont face="Arial">
    </head>
    <body>
    <center><asp:label id="output" runat="server" /></center> 
    <center> <form method="GET" runat="server"> 
    <table cellspacing="5" cellpadding="5" border="0">

    <tr>
    <td colspan="2" align="center"><font color="red" size="4">Alien Registration Center (ARC)
    </font></td> </tr>

    <tr>
    <td>Name</td>
    <td align="left"><asp:textbox id="name" runat="server" /></td> </tr>

    <tr>
    <td>Species</td>
    <td align="left"><asp:textbox id="species" runat="server" /></td> </tr>

    <tr>
    <td>Species type</td>
    <td align="left">
        <asp:radiobuttonlist id="speciesType" runat="server">
            <asp:listitem id="carbon" runat="server" value="Carbon-based"/>
            <asp:listitem id="other" runat="server" value="Other"/>
        </asp:radiobuttonlist>
    </td>
    </tr>

    <tr>
    <td>Residence</td>
    <td align="left">
        <asp:dropdownlist id="residence" runat="server" >
            <asp:listitem value="Temporary Residence"/>
            <asp:listitem value="Permanant Residence"/>
        </asp:dropdownlist>
    </td>
    </tr>

    <tr>
    <td>Immunization against</td>
    <td align="left">
        <asp:checkboxlist id="immunization" runat="server" >
            <asp:listitem id="spaceworms" runat="server" value="Space Worms" />
            <asp:listitem id="asthma" runat="server" value="Asthma " />
            <asp:listitem id="yellowfever" runat="server" value="Yellow Fever" />
        </asp:checkboxlist>
    </td>
    </tr>

    <tr>
    <td>Previous address</td>
    <td align="left">
        <asp:textbox  id="address" rows="5" textmode="multiline" runat="server" /> 
        </td> </tr> <tr> <td colspan="2" align="center">
        <input type="submit" name="submit" value="Register"></td> </tr>

    </table>
    </form>
    </center>

    </body>
    </html>



    This is what the form looks like:



    The form is constructed using a variety of different server controls. I start with two text controls:


    <tr>
    <
    td>Name</td>
    <
    td align="left"><asp:textbox id="name" runat="server" /></td> </tr>

    <
    tr>
    <
    td>Species</td>
    <
    td align="left"><asp:textbox id="species" runat="server" /></td> </tr



    This is followed by a set of radio buttons, created via the "radiobuttonlist" server control. For each radio option, the "listitem" control is used to specify the value:


    <tr>
    <
    td>Species type</td>
    <
    td align="left">
        <
    asp:radiobuttonlist id="speciesType" runat="server" >
            <
    asp:listitem id="carbon" runat="server" value="Carbon based"/>
            <
    asp:listitem id="other" runat="server" value="Other"/>
        </
    asp:radiobuttonlist>
    </
    td>
    </
    tr>



    Similarly, I have a "dropdownlist" server control which is used to create a drop-down list box for the type of residence:


    <tr>
    <
    td>Residence</td>
    <
    td align="left">
        <
    asp:dropdownlist id="residence" runat="server" >
            <
    asp:listitem value="Temporary Residence"/>
            <
    asp:listitem value="Permanant Residence"/>
        </
    asp:dropdownlist>
    </
    td>
    </
    tr>



    This is followed by a list of checkboxes. Since more than one can be selected, multiple "checkboxlist" server control are used to display the various options:


    <tr>
    <
    td>Immunization against</td>
    <
    td align="left">
        <
    asp:checkboxlist id="immunization" runat="server" >
            <
    asp:listitem id="spaceworms" runat="server" value="Space Worms" />
            <
    asp:listitem id="asthma" runat="server" value="Asthma " />
            <
    asp:listitem id="yellowfever" runat="server" value="Yellow Fever" />
        </
    asp:checkboxlist>
    </
    td>
    </
    tr>



    Finally, a multi-line text box - note the addition of the "textmode" attribute to the "textbox" server control -is used for the address.


    <tr>
    <
    td>Previous address</td>
    <
    td align="left">
        <
    asp:textbox  id="address" rows="5" textmode="multiline" runat="server" /> 
        </
    td> </tr> <tr> <td colspan="2" align="center">
        <
    input type="submit" name="submit" value="Register"></td> </tr>



    The form closes with the mandatory "submit" button. Enter some form values, hit the "Register" button, and you should see something like this:



    Since the "runat" attribute of the form is set to "server", ASP.NET will submit this form to the same page. As a result, in the Page_Load() function, I have put together some simple C# code to display the data entered by the user. Take a look:


    void Page_Load()
    {   
        
    // check if the form has been submitted
        
    if(Request.QueryString["submit"] == "Register"
        {
        
            
    output.Text "You have entered the following:
    "
    ;
            
            
    // name field
            
    output.Text output.Text "Name:" name.Text "
    "
    ;
            
            
    // species field
            
    output.Text output.Text "Species:" species.Text "
    "
    ;
            
            
    // species type field
            
    output.Text output.Text "Species type: " speciesType.SelectedValue
    "
    "
    ;
            
            
    // immunization field
            
    output.Text output.Text "Immunization against: ";
            for(
    int counter 0counter immunization.Items.Countcounter++) 
            {
                if(
    immunization.Items[counter].Selected
                {
                    
    output.Text output.Text immunization.Items[counter].Text ",";
                }    
            }    
            
            
    output.Text output.Text "
    "
    ;

            
    // address field
            
    output.Text output.Text "Address:" address.Text "
    "
    ;
                        
        }
        
    }    



    The code above analyzes each and every server control in the form, and displays the value provided by the user using the ubiquitous "output" label control. Note the manner in which I have used the "SelectedValue" property of the "radiobuttonlist" to get the value of the radio option selected by the user, and my deft handling of the "Selected" property associated with the "checkboxlist" control. Both these properties allow you to test if the particular option has been selected by the user.

    And that's about it for today. I started this article with a fast rundown of the various properties of the Request and Browser objects - both help you obtain more information about the users visiting your Web site. This was followed by some examples of processing form input, and a discussion of the various server controls built into ASP.NET that can simplify the task of constructing forms.

    In the next segment of this article, I shall dive straight into the world of database interaction with ASP.NET. I'm sure that many of you have been waiting for this. The time is near, so keep watching this space for more.

    Note: Examples are illustrative only, and are not meant for a production environment. Melonfire provides no warranties or support for the source code described in this article.
    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

     

    ASP.NET ARTICLES

    - Developing a Mini ASP.NET AJAX Server Centri...
    - Disadvantages of the ASP.NET MVC Framework
    - Advantages of the ASP.NET MVC Approach
    - ASP.NET Web Forms Weaknesses
    - ASP.NET Web Forms Meets ASP.NET MVC
    - Source Code for Saving and Retrieving Data w...
    - Using GridView to Save and Retrieve Data wit...
    - Handling Dynamic Images in ASP.NET 3.5 AJAX ...
    - Retrieving Data with AJAX and the GridView C...
    - Playing with Images in ASP.NET 3.5 AJAX Appl...
    - Saving and Retrieving Data with AJAX
    - Enhancing PHP Via the ASP.NET AJAX Framework...
    - Enhancing PHP Programming with the ASP.NET A...
    - Classes and ASP.NET AJAX
    - Using ASP.NET AJAX

     
    Best Practices for Windows Vista Migration Presentation
    Dell and Microsoft recently held a series of face-to-face seminars entitled, &qu....

     
    Creating a Culture for Code Reuse
    If you oversee development teams you know that like it or not proprietary and ex....

     
    Keys to Web Application Acceleration: Advances in Delivery Systems
    Accelerate Web apps by up to 5x. Ensure significantly faster access to the Web a....

     
    Optimizing Application Monitoring
    Tired of finding out from your customers that you're offline? This white paper e....

     
    Solaris to Solaris Migration -- Migrating applications from Sun SPARC to Dell PowerEdge R900
    This comprehensive Migration Guide reviews the approach that Principled Technolo....

     




    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT