So you want to populate a DropDownList with values from a database, huh? Well, it is not incredibly hard. You can do it from the designer by hovering over the DropDownList and selecting "Choose Data Source..." from the smart tag. You can also do it from code like so:
SelectCommand="SELECT [linkShortDesc] FROM [Links]"></asp:SqlDataSource>
As you can see from the code, we created a new ASP.NET SqlDataSource, provided our connection string and our query we wanted to run. In my case I'm returning a list of links from a table. Now, in our ASP.NET DropDownList code, we can specify a DataValueField and a DataSourceID. The DataSourceID is the ID of the data source we want to link to, and DataValueField is the name of the column we want to display in our drop down list.
Navigation is nice. And so is implementing navigation easily. It is really cool that ASP.NET and Visual Studio will help us with this, with the Menu control.
In your toolbox, go to the "Navigation" tab and double click "Menu" on the list. This will add a new menu control to your page. The Menu control is easier to use in design mode, so switch to design mode.
If you hover over your menu control, the smart tag should appear (the little arrow usually to the right). It gives us some options (I won't list all of them here):
Auto format: This will open a dialog to quickly apply themes to your menu control. Go ahead, select one.
Views: (Static or Dynamic): Static view is where the links for your menu are always visible on the screen. Useful if you want something similar to a navigation bar at the top of the screen. Dynamic: allows for some fancy animations and other useful features. Useful if you want to preserve screen real-estate.
Edit Menu Items: This will open a dialog where you can add items to your menu. Root items will appear on the menu bar and children items will appear below their corresponding root items.
How to change the orientation of the menu control:
After you select your menu control, go to the properties page of Visual Studio and find "Orientation". It has two possibilities: Horizontal or Vertical. Just like you would think, Horizontal gives the menu control a horizontal orientation and vertical gives the menu control a vertical orientation.
To do this in code:
However, this can be accomplished through the code window. I will show you the code, and then explain it.
Lines 1 and 13 define the region of code which will contain the ASP.NET Menu Control. The Menu Control is contained between the <asp:Menu> tag and the </asp:Menu> tag. If you notice, the <asp:Menu> tag has a few attributes associated with it in this example:
runat: Determines where an element is processed. 'Server' controls are processed before the website is sent to the browser.
Orientation: gets or sets the direction in which to render the Menu control.
Lines 2 and 11 define the region of code where the ASP.NET Menu Control items are defined. The Menu Control items are contained between the <Items> tag and the </Items> tag.
The individual ASP.NET Menu Control items are defined with the <asp:MenuItem> and </asp:MenuItem> tags.
If you look at this code: <asp:MenuItem Text="About Us"Value="About Us"></asp:MenuItem>
you will see the attributes 'Text' and 'Value':
The 'Text' attribute sets the text displayed in the ASP.NET Menu Control
The 'Value' attribute sets the text associated with the Menu Control that will not be displayed on the ASP.NET Menu Control
External Resources Related To: ASP.NET Menu Control
ASP.NET makes it really simple, maybe to simple, to add form validation to your web forms. Suppose we had a simple form that consisted of these fields:
First Name
Last Name
Email Address
The code for such a form would probably resemble this:
<form id="simpleForm" runat="server">
<div>
First Name:<asp:TextBox ID="txtFName" runat="server"></asp:TextBox>
<br />
Last Name:<asp:TextBox ID="txtLName" runat="server"></asp:TextBox>
And what will happen now is our web form will make the First Name, Last Name and email fields required, as well as validate that the email address is really an email address.
This post will illustrate some of the more common ASP.NET controls you will find in the Visual Studio toolbox. Although you can double click the icon in the toolbox to add the control to your web page, this post focuses on adding the controls through the code editor, or "Source" view.
It is good to remember that you can use any HTML, CSS, or any other web technologies inside your ASP.NET web application (web sites are web applications). This post just highlights how to achieve similar results of traditional HTML technologies using ASP.NET controls and classes.
A lot of the controls have the same, or similar, names to standard HTML controls, but they are prefixed by "asp:" and has a few minor syntax changes. Read below to see some of the differences.
Buttons
This is how we generally make a button in standard HTML:
<buttonname="button">This is a button</button>
And, like you could guess it will have this output:
Now, in ASP.NET this would look like this:
<asp:Button ID="Button1" runat="server" Text="This is a button"/>
If you look at the two code examples you will the differences in how to create a button from the syntax editor. The ASP.NET Button class has quite a few properties and events that you can play with. The list is too large to go into much depth in a generalized post, but you can access the MSDN Button Class information here, provided by Microsoft.
Checkboxes
In HTML, we are used to making buttons like this:
<inputtype="checkbox"name="acceptTerms"value="Terms"/> I accept these terms and conditions.<br/>
And this would give this output:
I accept these terms and conditions.
ASP.NET Equivalent:
<asp:CheckBox ID="Terms" runat="server" Text="I accept these terms and conditions"/>
The above code is pretty simple and straightforward. We created an HTML select control named "Select1" with three items, or options: One, Two and Three. The drop down items of this list are defined between the <option> and </option> tags. If we added this code to a web site, we would end up with this:
Things are a little different when playing with the ASP.NET drop down list. The exact same thing in ASP.NET would look like this: