ASP.NET Data Controls Part 3: DataList
Subject:   Implementing a DropDownList in Edit Mode in a DataList
Date:   2003-11-05 14:32:59
From:   anonymous2
Hey your article is very nice and helped me build a datalist control. Although I have one problem I want to Implement a Drop Down List in the edit mode in order to let users update "Languages" or "Countries" through an existing DB table. Here is my code can you please help me?

.aspx FILE

<asp:DropDownList Height="18" Width="250" Font-Size="8" Runat="server" ID="ddlLibraryPrintLanguage"></asp:DropDownList>

.aspx.vb FILE

Public Sub GetLanguages()

Dim objLibrary_DAL As Library_DAL = New Library_DAL()

With ddlLibraryPrintLanguage
.DataSource = objLibrary_DAL.GetLibraryLanguage
.DataTextField = "LanguageDesc"
.DataValueField = "LanguageID"

Dim liLibraryLanguage As ListItem = New ListItem("- Select Language -")
.Items.Insert(0, liLibraryLanguage)

End With

End Sub

Sub Edit_CommandPrint(ByVal sender As Object, _
ByVal e As DataListCommandEventArgs)
' to rebind the DataList to the data source to
' refresh the control.
dlLibraryPrintDetail.EditItemIndex = e.Item.ItemIndex

Library_Dal.vb (Data Access Layer)

Public Function GetLibraryLanguage() As SqlDataReader

Dim objConn As New SqlConnection(ConfigurationSettings.AppSettings("strConnection"))
Dim objCmd As New SqlCommand("csp_GetLanguage", objConn)

objCmd.CommandType = CommandType.StoredProcedure

Dim Result As SqlDataReader = objCmd.ExecuteReader(CommandBehavior.CloseConnection)

Return Result

End Function

End Sub

Full Threads Oldest First

Showing messages 1 through 1 of 1.

  • Implementing a DropDownList in Edit Mode in a DataList
    2004-08-24 03:59:30  bertieCbass [View]

    In your HTML editItemTemplate add something like this for your listbox:
    <td<asp:DropDownList id="DropDownList1" runat="server" SelectedIndex='<%# DataBinder.Eval(Container.DataItem, "existingDataValue") %>'> <asp:ListItem>0</asp:ListItem> <asp:ListItem>1</asp:ListItem> <asp:ListItem>2</asp:ListItem> <asp:ListItem>3</asp:ListItem> </asp:DropDownList></td>

    Note you need to set the SelectedIndex if you want to position the listbox to the existing data value

    In the Update_Command code refer to the list like this:

    DimdropList As DropDownList
    dropList = CType(e.Item.FindControl("DropDownList1"), DropDownList)
    intValue = dropList.SelectedItem.Value

    Worked for me!