Cascading DropDowLists for ListView editing and inserting


Here is a sample code for working with cascading dropdownlists in editing and inserting with the new ListView data control.
I updated the code for the released version.

<asp:ListView ID="ListView1" runat="server" DataKeyNames="CustomerId" OnItemInserting="ListView1_OnItemInserting"

OnItemUpdating="ListView1_ItemUpdating"

DataSourceID="SqlDataSource1" InsertItemPosition="LastItem">

<AlternatingItemTemplate>

<tr runat="server" style="">

<td>

<asp:Button ID="DeleteButton" runat="server" CommandName="Delete"

Text="Delete" />

<asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />

</td>

<td>

<asp:Label ID="CustomerIdLabel" runat="server"

Text=’<%# Eval("CustomerId") %> />

</td>

<td>

<asp:Label ID="NameLabel" runat="server" Text=’<%# Eval("Name") %> />

</td>

<td>

<asp:Label ID="ManufactureLabel" runat="server"

Text=’<%# Eval("Manufacture") %> />

</td>

<td>

<asp:Label ID="ModelLabel" runat="server" Text=’<%# Eval("Model") %> />

</td>

</tr>

</AlternatingItemTemplate>

<LayoutTemplate>

<table id="Table1" runat="server">

<tr id="Tr1" runat="server">

<td id="Td1" runat="server">

<table ID="itemPlaceholderContainer" runat="server" border="0" style="">

<tr id="Tr2" runat="server" style="">

<th id="Th1" runat="server">

</th>

<th id="Th2" runat="server">

CustomerID

</th>

<th id="Th3" runat="server">

Name

</th>

<th id="Th4" runat="server">

Manufacturer

</th>

<th id="Th5" runat="server">

Model

</th>

</tr>

<tr ID="itemPlaceholder" runat="server">

</tr>

</table>

</td>

</tr>

<tr id="Tr3" runat="server">

<td id="Td2" runat="server" style="">

<asp:DataPager ID="DataPager1" runat="server">

<Fields>

<asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True"

ShowLastPageButton="True" />

</Fields>

</asp:DataPager>

</td>

</tr>

</table>

</LayoutTemplate>

<ItemTemplate>

<tr runat="server" style="">

<td>

<asp:Button ID="DeleteButton" runat="server" CommandName="Delete"

Text="Delete" />

<asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />

</td>

<td>

<asp:Label ID="CustomerIdLabel" runat="server"

Text=’<%# Eval("CustomerId") %> />

</td>

<td>

<asp:Label ID="NameLabel" runat="server" Text=’<%# Eval("Name") %> />

</td>

<td>

<asp:Label ID="ManufactureLabel" runat="server"

Text=’<%# Eval("Manufacture") %> />

</td>

<td>

<asp:Label ID="ModelLabel" runat="server" Text=’<%# Eval("Model") %> />

</td>

</tr>

</ItemTemplate>

<InsertItemTemplate>

<tr runat="server" style="">

<td>

<asp:Button ID="InsertButton" runat="server" CommandName="Insert"

Text="Insert" />

<asp:Button ID="CancelButton" runat="server" CommandName="Cancel"

Text="Clear" />

</td>

<td></td>

<td>

<asp:TextBox ID="NameTextBox" runat="server" Text=’<%# Bind("Name") %> />

</td>

<td>

<asp:DropDownList ID="ddlManufacture" runat="server" AutoPostBack="True" DataTextField="Manufacture"

DataValueField="Manufacture" Width="122px" DataSourceID="dsManufactures" AppendDataBoundItems="true" >

<asp:ListItem>Please select</asp:ListItem>

</asp:DropDownList>

<asp:SqlDataSource ID="dsManufactures" runat="server" ConnectionString="<%$ ConnectionStrings:test_for_forumConnectionString %>"

SelectCommand="SELECT [Manufacture] FROM [Manufactures]" ></asp:SqlDataSource>

</td>

<td>

<asp:DropDownList ID="ddlModel" runat="server" DataSourceID="dsModels"

DataTextField="Model" DataValueField="Model" >

</asp:DropDownList>

<br />

<asp:SqlDataSource ID="dsModels" runat="server" ConnectionString="<%$ ConnectionStrings:test_for_forumConnectionString %>"

SelectCommand="SELECT * FROM [Models] where Manufacture =@Manufacture">

<SelectParameters>

<asp:ControlParameter ControlID="ddlManufacture" Name="Manufacture" />

</SelectParameters>

</asp:SqlDataSource>

</td>

</tr>

</InsertItemTemplate>

<SelectedItemTemplate>

<tr runat="server" style="">

<td>

<asp:Button ID="DeleteButton" runat="server" CommandName="Delete"

Text="Delete" />

<asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />

</td>

<td>

<asp:Label ID="CustomerIdLabel" runat="server"

Text=’<%# Eval("CustomerId") %> />

</td>

<td>

<asp:Label ID="NameLabel" runat="server" Text=’<%# Eval("Name") %> />

</td>

<td>

<asp:Label ID="ManufactureLabel" runat="server"

Text=’<%# Eval("Manufacture") %> />

</td>

<td>

<asp:Label ID="ModelLabel" runat="server" Text=’<%# Eval("Model") %> />

</td>

</tr>

</SelectedItemTemplate>

<EmptyDataTemplate>

<table runat="server" style="">

<tr>

<td>

No data was returned.

</td>

</tr>

</table>

</EmptyDataTemplate>

<EditItemTemplate>

<tr runat="server" style="">

<td>

<asp:Button ID="UpdateButton" runat="server" CommandName="Update"

Text="Update" />

<asp:Button ID="CancelButton" runat="server" CommandName="Cancel"

Text="Cancel" />

</td>

<td>

<asp:Label ID="CustomerIdLabel1" runat="server"

Text=’<%# Eval("CustomerId") %> />

</td>

<td>

<asp:TextBox ID="NameTextBox" runat="server" Text=’<%# Bind("Name") %> />

</td>

<td>

<asp:DropDownList ID="ddlManufacture" runat="server" AutoPostBack="True" DataTextField="Manufacture"

DataValueField="Manufacture" Width="122px" SelectedValue=’<%# Bind("Manufacture") %>

DataSourceID="dsManufactures" >

</asp:DropDownList>

<asp:SqlDataSource ID="dsManufactures" runat="server" ConnectionString="<%$ ConnectionStrings:test_for_forumConnectionString %>"

SelectCommand="SELECT [Manufacture] FROM [Manufactures]" ></asp:SqlDataSource>

</td>

<td> <asp:Label ID="ModelLabel" runat="server" Text=’<%# Eval("Model") %> Visible="false" />

<asp:DropDownList ID="ddlModel" runat="server" DataSourceID="dsModels"

DataTextField="Model" DataValueField="Model" OnDataBound="ddlModel_DataBound" >

</asp:DropDownList>

<br />

<asp:SqlDataSource ID="dsModels" runat="server" ConnectionString="<%$ ConnectionStrings:test_for_forumConnectionString %>"

SelectCommand="SELECT * FROM [Models] where Manufacture =@Manufacture">

<SelectParameters>

<asp:ControlParameter ControlID="ddlManufacture" Name="Manufacture" />

</SelectParameters>

</asp:SqlDataSource>

</td>

</tr>

</EditItemTemplate>

</asp:ListView>

<asp:SqlDataSource ID="SqlDataSource1" runat="server"

ConnectionString="<%$ ConnectionStrings:test_for_forumConnectionString %>"

DeleteCommand="DELETE FROM [AutoCustomers] WHERE [CustomerId] = @CustomerId"

InsertCommand="INSERT INTO [AutoCustomers] ([Name], [Manufacture], [Model]) VALUES (@Name, @Manufacture, @Model)"

SelectCommand="SELECT [CustomerId], [Name], [Manufacture], [Model] FROM [AutoCustomers]"

UpdateCommand="UPDATE [AutoCustomers] SET [Name] = @Name, [Manufacture] = @Manufacture, [Model] = @Model WHERE [CustomerId] = @CustomerId">

<%

— <DeleteParameters>

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

</DeleteParameters>

<UpdateParameters>

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

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

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

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

</UpdateParameters>

<InsertParameters>

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

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

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

</InsertParameters>–

%>

</asp:SqlDataSource>

C# code:

protected

void ddlModel_DataBound(object sender, EventArgs e)

{

DropDownList ddlModel = (DropDownList)sender;

ListViewItem ListViewItem1 = (ListViewItem)ddlModel.NamingContainer;

if (ListViewItem1 != null)

{

Label lblModel = (Label)ListViewItem1.FindControl("ModelLabel");

string strModel = lblModel.Text;

ListItem lm = ddlModel.Items.FindByValue(strModel);

if (lm != null) lm.Selected = true;

}

}

protected void ListView1_ItemUpdating(object sender, ListViewUpdateEventArgs e)

{

ListViewItem item = ListView1.Items[e.ItemIndex];

string strName = ((TextBox)item.FindControl("NameTextBox")).Text;

string strModel = ((DropDownList)item.FindControl("ddlModel")).SelectedValue;

string strManufacturer = ((DropDownList)item.FindControl("ddlManufacture")).SelectedValue;

e.NewValues["Name"] = strName;

e.NewValues[

"Model"] = strModel;

e.NewValues["Manufacture"] = strManufacturer;

e.Cancel = false;

}

protected void ListView1_OnItemInserting(object sender, ListViewInsertEventArgs e)

{

string strName=((TextBox)e.Item.FindControl("NameTextBox")).Text;

string strModel = ((DropDownList)e.Item.FindControl("ddlModel")).SelectedValue;

string strManufacture = ((DropDownList)e.Item.FindControl("ddlManufacture")).SelectedValue;

e.Values[

"Name"] = strName;

e.Values[

"Model"] = strModel;

e.Values[

"Manufacture"] = strManufacture;

e.Cancel =

false;

}

Advertisements


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s