FormView, with 2 dropdownlists

M

Milton Snider

I am somewhat new to asp.net. I have a form which is always in insert mode
and I am trying to let one dropdownlist(dropdownlist1) select a state and
then limit the number of cities populating the 2nd
dropdownlist(dropdownlist2) to only those cities within the state selected
in dropdownlist1. Dropdownlist1and Dropdownlist2 select their choices from
a table called states_cities and stores the state as the value. How do I
filter the dropdownlist2 based on the value in dropdownlist1. Both
dropdownlists are bound to the same table called requests where each stores
a value in state, and city respectively. Any help would be appreciated.
thanks
Milton
 
Y

Yvonne

Hi Milton,
You can handle this in the whith the event handler for
DropDownList1_SelectedIndexChanged. There you may asign the new
selection of cities to to your second combo.
Yvonne
 
M

Milton Snider

I assume I should databind() in the SelectedIndexChanged event handler code
but how do I tell the DropDownList2 to Databind() ? If I try to use
Me.Dropdownlist2 it is not under the me object. how do I tell it to
Databind. it gets its listing from a datasource.

thanks
Milton
 
Y

Yvonne

can you post your sourcecode, please?

Milton said:
I assume I should databind() in the SelectedIndexChanged event handler code
but how do I tell the DropDownList2 to Databind() ? If I try to use
Me.Dropdownlist2 it is not under the me object. how do I tell it to
Databind. it gets its listing from a datasource.

thanks
Milton
 
M

Milton Snider

Here is the formpage. There is no code on the VB page yet.
<%@ Page Language="VB" AutoEventWireup="false"
CodeFile="cascadingDropDownLists.aspx.vb"
Inherits="Test_cascadingDropDownLists" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>Untitled Page</title>

</head>

<body>

<form id="form1" runat="server">

<asp:SqlDataSource ID="FormDataSource" runat="server" ConnectionString="<%$
ConnectionStrings:Req2ExceedConnectionString6 %>"

DeleteCommand="DELETE FROM [District_Cities] WHERE [tableID] = @tableID"
InsertCommand="INSERT INTO [District_Cities] ([Dist], [City]) VALUES (@Dist,
@City)"

SelectCommand="SELECT [tableID], [Dist], [City] FROM [District_Cities]"
UpdateCommand="UPDATE [District_Cities] SET [Dist] = @Dist, [City] = @City
WHERE [tableID] = @tableID">

<DeleteParameters>

<asp:parameter Name="tableID" Type="Int32" />

</DeleteParameters>

<UpdateParameters>

<asp:parameter Name="Dist" Type="String" />

<asp:parameter Name="City" Type="String" />

<asp:parameter Name="tableID" Type="Int32" />

</UpdateParameters>

<InsertParameters>

<asp:parameter Name="Dist" Type="String" />

<asp:parameter Name="City" Type="String" />

</InsertParameters>

</asp:SqlDataSource>

<asp:FormView ID="FormView1" runat="server" AllowPaging="True"
DataKeyNames="tableID"

DataSourceID="FormDataSource" DefaultMode="Insert" Height="160px"
Width="408px">

<EditItemTemplate>

tableID:

<asp:Label ID="tableIDLabel1" runat="server" Text='<%# Eval("tableID")
%>'></asp:Label><br />

Dist:

<asp:TextBox ID="DistTextBox" runat="server" Text='<%# Bind("Dist") %>'>

</asp:TextBox><br />

City:

<asp:TextBox ID="CityTextBox" runat="server" Text='<%# Bind("City") %>'>

</asp:TextBox><br />

<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True"
CommandName="Update"

Text="Update">

</asp:LinkButton>

<asp:LinkButton ID="UpdateCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel"

Text="Cancel">

</asp:LinkButton>

</EditItemTemplate>

<InsertItemTemplate>

Dist:

<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="DDL_District" DataTextField="DISTRICT"

DataValueField="DIST"
OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"

SelectedValue='<%# Bind("Dist") %>'>

</asp:DropDownList><asp:SqlDataSource ID="DDL_District" runat="server"
ConnectionString="<%$ ConnectionStrings:Req2ExceedConnectionString6 %>"

SelectCommand="SELECT DISTINCT [DISTRICT], [DIST] FROM
[Cities]"></asp:SqlDataSource>

<br />

City:

<asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="DDL_City"
DataTextField="DUTY_STAT"

DataValueField="DUTY_STAT" SelectedValue='<%# Bind("City") %>'>

</asp:DropDownList><asp:SqlDataSource ID="DDL_City" runat="server"
ConnectionString="<%$ ConnectionStrings:Req2ExceedConnectionString6 %>"

SelectCommand="SELECT [DUTY_STAT], [ID] FROM [Cities] WHERE ([DIST] =
@DIST)">

<SelectParameters>

<asp:ControlParameter ControlID="DropDownList1" Name="DIST"
PropertyName="SelectedValue"

Type="String" />

</SelectParameters>

</asp:SqlDataSource>

<br />

<asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True"
CommandName="Insert"

Text="Insert"></asp:LinkButton>

<asp:LinkButton ID="InsertCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel"

Text="Cancel"></asp:LinkButton>

</InsertItemTemplate>

<ItemTemplate>

tableID:

<asp:Label ID="tableIDLabel" runat="server" Text='<%# Eval("tableID")
%>'></asp:Label><br />

Dist:

<asp:Label ID="DistLabel" runat="server" Text='<%# Bind("Dist")
%>'></asp:Label><br />

City:

<asp:Label ID="CityLabel" runat="server" Text='<%# Bind("City")
%>'></asp:Label><br />

<asp:LinkButton ID="EditButton" runat="server" CausesValidation="False"
CommandName="Edit"

Text="Edit"></asp:LinkButton>

<asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False"
CommandName="Delete"

Text="Delete"></asp:LinkButton>

<asp:LinkButton ID="NewButton" runat="server" CausesValidation="False"
CommandName="New"

Text="New"></asp:LinkButton>

</ItemTemplate>

</asp:FormView>

</form>

</body>

</html>
 
Y

Yvonne

Hy Milton,

You're right and you should have access to your second combo...

I'm not very use to VB-Code, but what you have to change is: remove the
select-statement for the second combo from the aspx-file and do it in
DropDownList1_SelectedIndexChanged.

yvonne
 
M

Milton Snider

Yes, I understand. I have don't know how to assign a datasource, select
statement with code for a combobox.
 
M

MikeS

Maybe set AutoPostBack="True" on DropDownList1. Then the
ControlParameter you set up should kick in and the databinding will
happen as expected.

Note that without that, not even DropDownList1_SelectedIndexChanged
will fire. Not that you need it, since you have the control parameter.

Also note that when controls are part of a data control, if you want
at it, you have to do a FindControl.

So say you didn't use an asp:ControlParameter but just a regular
asp:parameter in the DropDownList2 data source, then you can handle the
binding in DropDownList1_SelectedIndexChanged like so:


Dim dl1 As DropDownList =
FormView1.FindControl("DropDownList1")
Dim dl2 As DropDownList =
FormView1.FindControl("DropDownList2")
Dim ods As ObjectDataSource = FormView1.FindControl("DDL_City")
ods.SelectParameters("DIST").DefaultValue = dl1.SelectedValue
dl2.DataBind()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top