Posts

DropDownList Part 4: Bind DropDownList To A List Of Objects

Image
In this part of the tutorial we will bind a DropDownList control to a list of objects.  This common practice if you are working with business objects that are mapped to the database table.  We will create a business object call "Category" and create a list call "categories" in our .aspx page. One of the most commonly used server controls in ASP.NET is the DropDownList control.  In this blog I will show you how to bind a list of objects to a DropDownList control. 1. Create a .aspx page 2. Click on the "Toolbox" panel 3.  Drag the DropDownList control into a design surface 4. Create a connection string in the Web.Config file <connectionStrings> <add name="NorthwindConnectionString" connectionString="Data Source=(local); Initial Catalog=Northwind;Integrated Security=True" providerName="System.Data.SqlClient"/> </connectionStrings> 5. Create a new folder in your project call "BusinessObjects" th...

RadioButtonList : Bind RadioButtonList Control To A DataTable

Image
The RadioButtonList displays a collection of radio buttons on a web page. There can only be one selection in a group of choices at a time. In this tutorial we will create a RadioButtonList and then bind it to the Categories table of the Northwind database using a DataTable. To create a RadioButtonList control do the following: 1.  Select the "RadioButtonList" control under the "Standard" control in the "Toolbox" pane on the left. 2.  Drag the RadioButtonList control to a design surface 3.  In the source code of the .aspx page make sure "AutoPostBack" is set to true, the source code should look like this <asp:RadioButtonList ID="RadioButtonList1" runat="server" AutoPostBack="true"> </asp:RadioButtonList> 4. Create a connection string in the Web.Config file <connectionStrings> <add name="NorthwindConnectionString" connectionString="Data Source=(local); Initial Catalog=Northw...

DropDownList Part 3: Use The DropDownList Control To Populate GridView

Image
If you work on the DropDownList control, eventually you will be asked to populate some sort of data grid based on the selection on the list. The most common control you have to populate is the GridView control. This tutorial builds from part 1 and part 2 about DropDownList control. So make sure you go through those blogs before you start this one. In this tutorial we will learn how to populate the GridView via a DropDownList selection. 1. Select the "GridView" control from the "Toolbox" pane on the left 2. Drag the GridView control to a design surface 3. Enable the "AutoPostBack" attribute on the "DropDownList1" control, by clicking on the "Source" tab then add the following line <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true"> </asp:DropDownList&gt 4. In the code behind add the BindGridView method protected void BindGridView(int catId) { DataTable dtProduc...

DropDownList Part 2: Setting Default Value on First Page Load

Image
In part one  DropDownList Part 1: Bind a DataTable to A DropDownList  we learned how to bind a DataTable to a DropDownList.  In this tutorial we will learn to set the default value of the DropDownList control on the page's first load that is before there is a post back to the page.  This behavior is commonly used when you want to default the drop down list to a particular selection.  Such as the "United States" in a list of countries drop down list. To set a default value in the DropDownList control follow the steps below. 1. Add the SetDefaultSelectItem method to the code behind file. protected void SetDefaultSelectItem(string txtSelect) { DropDownList1.Items.FindByText(txtSelect).Selected = true; } 2. Add a condition to only set the default value on the first page load protected void Page_Load(object sender, EventArgs e) { if(!Page.IsPostBack) { BindCategoriesList(); SetDefaultSelectItem("Seafood"); } } 3. If you run the applica...

DropDownList Part 1: Bind a DataTable To A DropDownList

Image
One of the most commonly used server controls in ASP.NET is the DropDownList control.  In this blog I will show you how to bind a DataTable to a DropDownList control. 1. Create a .aspx page 2. Click on the "Toolbox" panel 3.  Drag the DropDownList control into a design surface 4. Create a connection string in the Web.Config file <connectionStrings> <add name="NorthwindConnectionString" connectionString="Data Source=(local); Initial Catalog=Northwind;Integrated Security=True" providerName="System.Data.SqlClient"/> </connectionStrings> 5.  In the code behind page(.cs) page of the .aspx page, type in the following lines at the top to use the following libraries using System.Web.Configuration; using System.Data.SqlClient; using System.Data; 6. Create a new method call BindCategoriesList() that returns void protected void BindCategoriesList() { DataTable dtCategories = new DataTable(); string connectString = WebConfi...

Add a Data Connection To A SQL Server Database In Visual Studio 2013

Image
The "Server Explorer" tool in Visual Studio 2013 is a good tool at your disposal if want to interact with the database in GUI environment.  To create a new data connection to the database in the "Server Explorer" perform the following actions: Click on "Server Explorer" tab in the left hand side, then click on "Add Connection" 2.  In the "Data source" list box, select "Microsoft SQL Server", for data provider select ".NET Framework Data Provider for SQL Server", then click "Continue" 3. In the "Server name" field type in the hostname\database instance  of the database you want to connect to.  Under "Connect to a database", there is a text box or a drop down list.  You can type in the name of the database if you know it.  Or let Visual Studio pick up the database for you so that you can select it from a list of available databases in the SQL Server instance you've selected.  If you se...

Create A Blank Solution in Visual Studio 2013

Image
When you are starting a new project, you might not want to use the templates that Microsoft provided for you. There are times when you want to start off the project with a clean slate.  You can create a blank solution in Visual Studio.  However, the option to create a blank solution is kind of hidden. Follow the steps below to create a blank solution: Open Visual Studio 2013, and then click on "File", then select "New", then  "Project" 2.  Expand the "Other Project Types" node on the left hand navigation, then select "Visual Studio Solutions". Then select the "Blank Solution" template on the right hand side. 3.  Give your solution a name and then click "OK" 4.  Your blank solution will be displayed in "Solution Explorer"