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

Drop membership tables

I need to move all my membership tables from one database to another, so I want to drop all tables in the taget database in one click.
Here is a script I found from http://aspzone.com/blogs/john/archive/2006/07/12/1793.aspx (thanks) and I add a little bit to the WHERE condtion:

USE [mydb]

GO

WHILE EXISTS(SELECT [name] FROM sys.tables WHERE [type] = ‘U’ and [name] like ‘aspnet%’)

BEGIN

DECLARE

@table_name varchar(50)

DECLARE

table_cursor CURSOR FOR SELECT [name] FROM sys.tables WHERE [type] = ‘U’ and [name] like ‘aspnet%’

OPEN

table_cursor

FETCH

NEXT FROM table_cursor INTO @table_name

WHILE @@FETCH_STATUS = 0

BEGIN

BEGIN

TRY

EXEC

(‘DROP TABLE [‘ + @table_name + ‘]’)

PRINT ‘Dropped Table ‘ + @table_name

END

TRY

BEGIN

CATCH END CATCH

FETCH

NEXT FROM table_cursor INTO @table_name

END

CLOSE

table_cursor

DEALLOCATE

table_cursor

END