Working with DDL on a FormView.

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="pubDetails.aspx.vb" Inherits="pubDetails" %>

<!

DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

<

html xmlns="http://www.w3.org/1999/xhtml&quot; >

<

head runat="server">

<title>Untitled Page</title>

</

head>

<

body>

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

<b>Choose a state:</b>

<asp:DropDownList ID="ddlState" DataSourceID="SqlDSState" AutoPostBack="true"

DataTextField="state" runat="server" OnSelectedIndexChanged="ddlState_SelectedIndexChanged" />

<asp:SqlDataSource ID="SqlDSState" runat="server" SelectCommand="SELECT DISTINCT [state] FROM [authors]"

ConnectionString="<%$ ConnectionStrings:Pubs %>" />

<br />

<br />

<table>

<tr>

<td valign="top">

<asp:GridView ID="GridView1" AllowSorting="True" AllowPaging="True" runat="server"

DataSourceID="SqlDataSource1" DataKeyNames="au_id"

AutoGenerateColumns="False" Width="427px" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" OnSorted="GridView1_Sorted" OnPageIndexChanged="GridView1_PageIndexChanged">

<Columns>

<asp:CommandField ShowSelectButton="True" />

<asp:BoundField DataField="au_id" HeaderText="au_id" ReadOnly="True" SortExpression="au_id" />

<asp:BoundField DataField="au_lname" HeaderText="au_lname" SortExpression="au_lname" />

<asp:BoundField DataField="LogoFile" HeaderText="LogoFile" SortExpression="LogoFile" />

<asp:BoundField DataField="state" HeaderText="state" SortExpression="state" />

</Columns>

</asp:GridView>

<asp:SqlDataSource ID="SqlDataSource1" runat="server" SelectCommand="SELECT [au_id], [au_lname], [LogoFile], [state] FROM [authors] WHERE ([state] = @state)"

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

<SelectParameters>

<asp:ControlParameter ControlID="ddlState" Name="state" PropertyName="SelectedValue"

Type="String" />

</SelectParameters>

</asp:SqlDataSource>

</td>

<td valign="top">

</td>

</tr>

</table>

<br />

<asp:SqlDataSource ID="SqlDS" runat="server" ConnectionString="<%$ ConnectionStrings:Pubs %>"

SelectCommand="SELECT [au_id], [au_lname], [state], [LogoFile] FROM [authors] WHERE [au_id] = @au_id"

UpdateCommand="UPDATE [authors] SET [au_lname] = @au_lname, [state]=@state, [LogoFile] = @LogoFile WHERE [au_id] = @au_id"

DeleteCommand="DELETE FROM [authors] WHERE [au_id] = @au_id"

InsertCommand="INSERT INTO [authors] ([au_id], [au_lname],[state], [LogoFile]) VALUES (@au_id, @au_lname,@state, @LogoFile)">

<SelectParameters>

<asp:ControlParameter Name="au_id" Type="String" ControlID="GridView1" PropertyName="SelectedValue" />

</SelectParameters>

<UpdateParameters>

<asp:Parameter Name="au_lname" Type="String" />

<asp:ControlParameter Name="state" Type="String" ControlID="fv1$stateDropDown" PropertyName="SelectedValue" />

<asp:ControlParameter Name="LogoFile" Type="String" ControlID="fv1$LogoFileDropDown" PropertyName="SelectedValue" />

<asp:Parameter Name="au_id" Type="String" />

</UpdateParameters>

<DeleteParameters>

<asp:Parameter Name="au_id" Type="String" />

</DeleteParameters>

<InsertParameters>

<asp:Parameter Name="au_id" Type="String" />

<asp:Parameter Name="au_lname" Type="String" />

<asp:ControlParameter Name="state" Type="String" ControlID="fv1$stateDropDown" PropertyName="SelectedValue" />

<asp:ControlParameter Name="LogoFile" Type="String" ControlID="fv1$LogoFileDropDown" PropertyName="SelectedValue" />

</InsertParameters>

</asp:SqlDataSource>

<asp:FormView ID="fv1" runat="server" DataSourceID="SqlDS" DataKeyNames="au_id" OnDataBound="fv1_OnDataBound" OnItemUpdated="fv1_OnItemUpdated"

OnItemInserted="fv1_OnItemInserted" >

<EditItemTemplate>

au_id:

<asp:Label ID="au_idLabel1" runat="server" Text=’<%# Eval("au_id") %>‘></asp:Label><br />

au_lname:

<asp:TextBox ID="au_lnameTextBox" runat="server" Text=’<%# Bind("au_lname") %>‘>

</asp:TextBox><br />

<asp:Label ID="state_Label" runat="server" Text=’<%# Bind("state") %> Visible="false"></asp:Label>

State:

<asp:DropDownList ID="StateDropDown" DataTextField="state" runat="server" /> <br />

<asp:Label ID="LogoFile_Label" runat="server" Text=’<%# Bind("LogoFile") %> Visible="false"></asp:Label>

LogoFile:

<asp:DropDownList ID="LogoFileDropDown" runat="server" ></asp:DropDownList>

<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>

au_id:

<asp:TextBox ID="au_idTextBox" runat="server" Text=’<%# Bind("au_id") %>‘>

</asp:TextBox><br />

au_lname:

<asp:TextBox ID="au_lnameTextBox" runat="server" Text=’<%# Bind("au_lname") %>‘>

</asp:TextBox><br />

State:

<asp:DropDownList ID="StateDropDown" DataTextField="state" runat="server" /> <br />

LogoFile:

<asp:DropDownList ID="LogoFileDropDown" runat="server" ></asp:DropDownList>

<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>

au_id:

<asp:Label ID="au_idLabel" runat="server" Text=’<%# Eval("au_id") %>‘></asp:Label><br />

au_lname:

<asp:Label ID="au_lnameLabel" runat="server" Text=’<%# Bind("au_lname") %>‘></asp:Label><br />

state:

<asp:Label ID="state_Label" runat="server" Text=’<%# Bind("state") %>‘></asp:Label><br />

LogoFile:

<asp:Label ID="LogoFileLabel" runat="server" Text=’<%# Bind("LogoFile") %>‘></asp:Label>

<br />

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

Text="Edit">

</asp:LinkButton>

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

Text="New">

</asp:LinkButton>

</ItemTemplate>

</asp:FormView>

</form>

</

body>

</

html>

 

Protected Sub fv1_OnItemUpdated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewUpdatedEventArgs)

GridView1.DataBind()

End Sub

Protected Sub fv1_OnItemInserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewInsertedEventArgs)

GridView1.DataBind()

End Sub

Protected Sub fv1_OnDataBound(ByVal sender As Object, ByVal e As System.EventArgs)

PopulateDL()

End Sub

Protected Sub PopulateDL()

Dim IMAGE_DIRECTORY As String = "images/logos/"

Dim pics As ArrayList = New ArrayList()

Dim s As String, files As String

For Each s In System.IO.Directory.GetFiles(Server.MapPath(IMAGE_DIRECTORY), "*.*")

files = System.IO.Path.GetFileName(s)

pics.Add(files)

Next

 

Dim LogoFileDropDown1 As DropDownList

LogoFileDropDown1 = CType(fv1.FindControl("LogoFileDropDown"), DropDownList)

Dim LogoFile_Label1 As Label

LogoFile_Label1 =

CType(fv1.FindControl("LogoFile_Label"), Label)

If Not LogoFileDropDown1 Is Nothing Then

LogoFileDropDown1.DataSource = pics

LogoFileDropDown1.DataBind()

If Not LogoFile_Label1 Is Nothing Then

LogoFileDropDown1.SelectedValue = LogoFile_Label1.Text

LogoFile_Label1.Text = Nothing

End If

End If

 

Dim StateDropDown1 As DropDownList

StateDropDown1 = CType(fv1.FindControl("StateDropDown"), DropDownList)

Dim state_Label1 As Label

state_Label1 =

CType(fv1.FindControl("state_Label"), Label)

If Not StateDropDown1 Is Nothing Then

StateDropDown1.DataSource = SqlDSState

StateDropDown1.DataBind()

If Not state_Label1 Is Nothing Then

StateDropDown1.SelectedValue = state_Label1.Text

state_Label1.Text =

Nothing

End If

 

End If

End Sub

Advertisements