Posts

RadioButtonList : Bind RadioButtonList To A List Of Objects

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

SqlDataSource Control Part 1: Bind The SqlDataSource To A GridView Without Writing A Single Line Of Code

Image
The SqlDataSource control is a data source control that is wizard driven to give you a quick and easy way to connect to the database.  It is a great tool to use if you need something quick to show your boss or if you need to set up a quick demo as a proof of concept.  Most .NET beginners like to use this data source control because it requires very little coding if any at all.  However, if you want to build a real world application you might want to think about creating a data access layer and a business layer to communicate with the database.  Imagine if your application gets bigger and each of your GridView uses an SqlDataSource control.  Each SqlDataSource has it's own query to maintain.  It would be a maintenance nightmare. The following is a step by step instruction of how to bind a SqlDataSource control to a GridView: 1.  Create a new project Web project in Visual Studio 2.  Create a new .aspx page 3.  Drag the "GridView" control from t...

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