Presentation is loading. Please wait.

Presentation is loading. Please wait.

Using List Controls with SQL Server

Similar presentations


Presentation on theme: "Using List Controls with SQL Server"— Presentation transcript:

1 Using List Controls with SQL Server
Working with Data-Bound DropDownLists, RadioButtons, and CheckBoxes

2 List Web Controls You’ve worked with DropDownList controls
Similar to the Visual Basic ComboBox ASP provides two new controls: RadioButtonList – a series of radio buttons CheckBoxList – a series of check boxes These controls share similar properties An Items collection can be accessed in code Can generate a SelectedIndexChanged event Can optionally generate automatic postback Items collection can be set up thru VS, your code, or populated by a data source control

3 Binding to a List Web Control
Binding process uses a Text and Value property similar to a ComboBox control Create a new web page ListControls.aspx Add an SqlDataSource named SqlDsCategory Sql Select access only – no Insert, Update, Delete Select all rows & all columns from Categories table Add DropDown, CheckBox, & RadioButton lists Configure the list controls Choose SqlDsCategory as the data source Choose Description to display and Code as Value Web page shows book category types Value property returns category code (key)

4 Filtering with a DropDownList
Remove RadioButtonList and CheckBoxList Add a 2nd SqlDataSource Select ISBN, Title, Author, YearPublished, and Price columns from the Books table Add a Where clause Set Column to CategoryCode Source to Control Control ID to the DropDownList control Add a GridView using the new SqlDataSource Select the new data source for the GridView View the resulting web page

5 Filtering and AutoPostBack
First category automatically selected Web page shows 1 Book in this category Change Category selected in drop down list No change in books listed Must set AutoPostBack true for drop down list Change drop down list AutoPostBack to true View web page again GridView automatically filters for book category

6 Filtering with a RadioButtonList
Remove DropDownList, add RadioButtonList Set RadioButtonList AutoPostBack true Change Where clause for grid SqlDataSource Remove previous Where clause Set Column to CategoryCode Source to Control Control ID to the RadioButtonList control Resulting web page shows no books initially Click on a radio button GridView automatically filters for book category Note that CheckBoxList won’t work in this case

7 Formatting a RadioButtonList
RadioButtonList shown in one long column Preferable to have several shorter columns Use RepeatColumns property to do this Set RepeatColumns to 4 Increase CellPadding to 10 to provide spacing RepeatDirection determines whether the first items appear in the first column or first row

8 Selecting Multiple Categories
Use CheckBoxList to select multiple categories But this exceeds the abilities of Visual Studio Need a variable number of Where clauses SqlDataSource does not allow for this Time to write some code!

9 Sql Server Connection String
Sql Server connection string in web.config: Web site folder located at B:\WEB250 Books.mdf residing in App_Data folder "Data Source=(LocalDB)\v11.0; AttachDbFilename=B:\WEB250\ListControls\App_Data\Books.mdf; Integrated Security=True;User Instance=False;" Hard coding web site path usually a bad idea Substitute |DataDirectory| for hard coded path AttachDbFilename=|DataDirectory|\Books.mdf;

10 Using Web.Config Connect String
Test database access w/new ConnectionString Remove SqlDataSource controls from page Change data source for CheckBoxList and GridView controls to None Connection strings are accessed in code as a property of the ConfigurationManager conString = ConfigurationManager.ConnectionStrings(“StringName"). ConnectionString Web.config can hold many connection strings StringName must match your connection string

11 Using Web.Config Connect String
Helpful to import namespaces to VB code Imports System.Data Imports System.Data.SqlClient Eliminates need to preface classes with namespaces Connection strings are accessed in code as a property of the ConfigurationManager conString = ConfigurationManager.ConnectionStrings(“StringName"). ConnectionString

12 Populating the CheckBoxList
sqlString = "Select CategoryCode, CategoryDescription from Categories;" Dim dbConnObj As New SqlConnection(conString) Dim cmdObj As New SqlCommand(sqlString, dbConnObj) Dim adapterObj As SqlDataAdapter = New SqlDataAdapter Dim tableObj As New DataTable adapterObj.SelectCommand = cmdObj adapterObj.Fill(tableObj) With chklCategory .DataSource = tableObj .DataTextField = "CategoryDescription" .DataValueField = "CategoryCode" .DataBind() End With

13 CheckBoxList Properties
Each check box is an item in the CheckBoxList Items Collection Number of check boxes chklCategory.Items.Count If check box has been checked chklCategory.Items(i).Selected Check box description displayed to user chklCategory.Items(i).Text Check box value returned to program chklCategory.Items(i).Value.ToString

14 Populating the GridView
Drag a GridView to your web page Do not select a data source for the GridView Edit Columns to set up GridView columns Add a BoundField for each column Set properties for each BoundField DataField must match database column HeaderText displayed to user in column header Build an SQL Where clause dynamically based on which check boxes have been selected Retrieve Books table just like Category table Ignore DataTextField and DataValueField which have no meaning for a GridView


Download ppt "Using List Controls with SQL Server"

Similar presentations


Ads by Google