Populating a DDL from a RadioButtonList (v2.0)

This is version 2 of a previous sample here that was done using ASP.Net v1.x. This one, of course, is created using the features of ASP.Net v2.

With the advent of ASP.Net 2.0, the RadioButtonList and the DropDownList (among others) have been updated so that population may occur with a DataSource control.

The initial page loads the RadioButtonList. Next, you will need to select an item from the RBL. This will then populate the DropDownList.

Notice, too, that, as with the previous sample, the DropDownList population is accomplished with a Stored Procedure. Since the SProc needs a parameter, this is accomplished by adding a ControlParameter to the SQLDataSource that populates the DDL.

In this Control Parameter, the Name property is the Parameter needed by the Stored Procedure. The ControlID property refers to the RadioButtonList’s ID property and lastly, the PropertyName is set to ‘SelectedValue’ – the Selected Item of the RadioButtonList, used to populate the DropDownList.

This all probably sounds more complicated than it really is….:)
Just check out the code below to see how easy it is to put something like this together.

<html>
<head>
<meta name="GENERATOR" Content="ASP Express 5.0">
<title>Populating a DropDownList from a RadioButtonList (v2.0)</title>
<script language="VB" Runat="server">
Sub GetCategory(Source as Object, E as EventArgs)
ddl.Visible="True"
lblProduct.text="Products in " & rb1.selecteditem.text & " Category"
label1.text= ("Selected Value = " & rb1.selecteditem.value )
End Sub
</script>
</head>
<body>
<Form id="form1" runat="server">
<table border="0">
<tr>
<td align="Left" valign="Top"><b><i>Categories:</i></b><br>
<asp:RadioButtonList id="rb1"
datatextfield="CategoryName"
datavaluefield="CategoryID"
DataSourceID="sqlDSCategories"
AutoPostBack="True"
OnSelectedIndexChanged="GetCategory"
runat="server">
</asp:RadioButtonList>
</td>
<td align="Left" valign="Top">
<b><i><asp:Label ID="lblProduct"  runat="server" /></i></b><br>
<asp:DropDownList id="ddl" DataKeyNames="ProductID"
Datatextfield="Productname"
DataSourceID="SQLDS2"
Visible="False" runat="server" />
</td>
</tr>
</table>
<asp:Label ID="label1" Font-Italic="True" Font-Bold="True" Runat="server" />
<asp:SQLDataSource ID="sqlDSCategories"
Runat="Server"
SelectCommand = "SELECT CategoryID, CategoryName From Categories"
ConnectionString="server=YourSrvr;Database=YourDB;pwd=YourPWD;uid=YourUID;">
</asp:SQLDataSource>
<asp:SqlDataSource ID="SQLDS2" runat="server"
SelectCommand="GetProductsByCategory"
SelectCommandType="StoredProcedure"
ConnectionString="server=YourSrvr;Database=YourDB;pwd=YourPWD;uid=YourUID;">
<SelectParameters>
<asp:ControlParameter
ControlID="rb1"
Name="CatID"
PropertyName="SelectedValue"
Type="String" />
</SelectParameters>
</asp:SqlDataSource>
</Form>
</body>

Inserting With a DetailsView Using a DDL

This code sample shows how to insert, with ASP.net 2.0, using a DetailsView. You will probably notice the similarities to another sample (Updating Using a DDL in a GridView), like using a DropDownList for the State List, in a TemplateField (‘Region’ field in the database). Except this time, it’s the ItemTemplate instead of the EditItemTemplate, like with the GridView Sample.

Notice 3 other things:
1. ‘AutoGenerateInsertButton’ is set to ‘True’, so it will automatically have an Insert button.
2. ‘DefaultMode’ is set to ‘Insert’, so that it will automatically go to Insert mode and not be populated.
3. The ‘HeaderRow’ section (below the Fields section). Here is where we add the ‘Insert New Employee’ Header.

To add a message (with a label, whatever), after the insert is complete, or bind another control with the new data, just add the OnRowInserted property with the DetailsView, and point it to a new Subroutine, using ‘DetailsViewInsertedEventArgs’ – something like this:

'Sub MyDetailsView_ItemInserted(sender As Object, DetailsViewInsertedEventArgs)'

<html>
<head>
<meta name="GENERATOR" Content="ASP Express 5.0">
<title>Inserting With a DetailsView</title>
</head>
<body>
<form id="form1" Runat="server">
<asp:DetailsView Runat="server"
Id="MyDetailsView"
GridLines="None"
cellpadding="0"
cellspacing="1"
Headerstyle-BackColor="#7988B7"
Headerstyle-Forecolor="#FFFFFF"
Headerstyle-Font-Names="Arial"
Headerstyle-Font-Size="12"
BackColor="#E0E0F6"
Font-Names="Arial"
Font-Size="10"
AutoGenerateInsertButton="True"
DefaultMode="Insert"
BorderColor="Black"
DataSourceID="sqlDS1"
DataKeyNames="EmployeeID"
AutogenerateRows="False">
<Fields>
<asp:BoundField DataField="LastName" SortExpression="LastName" HeaderText="LastName"/>
<asp:BoundField DataField="FirstName" SortExpression="FirstName" HeaderText="FirstName"/>
<asp:BoundField DataField="Title" SortExpression="Title" HeaderText="Title"/>
<asp:BoundField DataField="Address" SortExpression="Address" HeaderText="Address"/>
<asp:BoundField DataField="City" SortExpression="City" HeaderText="City"/>
<asp:TemplateField HeaderText="State">
<ItemTemplate>
<asp:DropDownList id="ddlStates" runat="server"
DataSourceID="sqlDS2"
BackColor="Pink"
selectedValue=”<%# Bind("Region") %>”
DataTextField="StAbbr" DataValueField="StAbbr">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="PostalCode" SortExpression="PostalCode" HeaderText="PostalCode"/>
</Fields>
<HeaderTemplate>
Insert New Employee
</HeaderTemplate>
</asp:DetailsView>

<asp:SQLDataSource ID="sqlDS1"
Runat="Server"
InsertCommand="Insert into Employees ([FieldList]) values (”@” before each field in List)"
ConnectionString="<%$ ConnectionStrings:YourConnStringGoesHere %>">
</asp:SQLDataSource>
<asp:SQLDataSource ID="sqlDS2"
Runat="Server"
SelectCommand = "SELECT STabbr From states"
DataSourceMode="DataSet"
ConnectionString="<%$ ConnectionStrings:YourConnStringGoesHere %>">
</asp:SQLDataSource>
</form>
</body>
</html>

Updating Using a DDL in Gridview

This code sample shows how to use a DropDownList inside a Gridview. The States which are listed in the DropDownList are actually pulled from a separate ‘States’ table, with the Selected Item chosen by the current record.

Along with that, we also show how an updata works with the same setup. As with a few other samples, this one loads the SelectCommand and the Updatecommand for the Gridview’s SQLDataSource in the Page_Load event. Naturally, these properties could be in the SQLDataSource tag itself. However, the line on this screen would be very long, therefore, it’s done this way to save horizontal the screen’s real estate.

To be able to use this Update scenario with a Stored Procedure, only two lines need to be addressed. First, of course, change
the UpdateCommand property to the actual name of the Stored Procedure. Then, assign ‘Stored Procedure”
to the ‘UpdateCommandType’ property:

 UpdateCommand="procUpdateEmployees"
 UpdateCommandType="storedProcedure"
<script language="VB" Runat="server">
Sub Page_Load(Source as Object, E as EventArgs)
	sqlDS1.SelectCommand = "SELECT  EmployeeID,  LastName,  FirstName,  " & _
		"Title,  Address, City,  Region,  PostalCode From  Employees"
	sqlDS1.UpdateCommand="UPDATE [Employees] SET [LastName] = " & _
		"@LastName, [FirstName] = @FirstName, [City] = @City, [Region] = " & _
		"@Region WHERE [EmployeeID] = @EmployeeID"
End Sub
</script>
<html>
	<head>
		<meta name="GENERATOR" Content="ASP Express 5.0">
		<title>DropDownList Inside a GridView</title>
	</head>
	<body>
	<div align="center"><b>DropDownList Inside a GridView</b></div>
		<form id="form1" Runat="server">
			<div align="center">
				<asp:GridView Runat="server"
					Id="gvEmp"
					GridLines="None"
					cellpadding="0"
					cellspacing="2"
					BackColor="#E0E0F6"
					Font-Name="Arial"
					Font-Size="10"
					BorderColor="Black"
					BorderStyle="Solid"
					BorderWidth="1px"
					Width="75%"
					AutogenerateColumns="False"
					DataSourceID="sqlDS1"
					DataKeyNames="Employeeid"
					AutoGenerateEditButton="True">
					<HeaderStyle BackColor="#7988B7" Forecolor="#FFFFFF"
						Font-Names="Arial" Font-Bold="True" Font-Size="12" />
					<AlternatingRowStyle
						BackColor="#DFDFDF"
						Font-Names="Arial"
						Font-Size="10" />
					<Columns>
						<asp:BoundField DataField="EmployeeID"
							HeaderText="ID" ReadOnly="True" />
						<asp:BoundField DataField="FirstName" HeaderText="FirstName" />
						<asp:BoundField DataField="LastName" HeaderText="LastName" />
						<asp:BoundField DataField="Address" HeaderText="Address" />
						<asp:BoundField DataField="City" HeaderText="City" />
						<asp:TemplateField HeaderText="State">
						   <ItemTemplate>
							      <asp:Label id="label1" runat="server"
							          Text=''<%# Container.DataItem("Region") %>''>
							      </asp:Label>
						   </ItemTemplate>
						   <EditItemTemplate>
						      <asp:DropDownList id="ddlStates" runat="server"
								DataSourceID="sqlDS2"
								 BackColor="Pink"
						         	DataTextField="StAbbr" DataValueField="StAbbr"
						         	SelectedValue=''<%# Bind("Region") %>''>
						      </asp:DropDownList>
						   </EditItemTemplate>
						</asp:TemplateField>
					</Columns>
				</asp:GridView>
			</div>
<asp:SQLDataSource
	ID="sqlDS1" Runat="Server"
SelectCommand="Select [FieldList] from [TableName]"
UpdateCommand="Update [TableName] set [Field=@Field] Where EmployeeID=@EmployeeID"
	ConnectionString="<%$ ConnectionStrings:YouConnStringGoesHere %>">
</asp:SQLDataSource>
<asp:SQLDataSource ID="sqlDS2"
Runat="Server"
	SelectCommand = "SELECT STabbr From states"
	DataSourceMode="DataSet"
	ConnectionString="<%$ ConnectionStrings:YouConnStringGoesHere %>">
</asp:SQLDataSource>
		</form>
	</body>
</html>