Anyone? My question is - is there a way to add list items into a dropdownlist within a datalist in code behind. Example please

Anyone?  My question is - is there a way to add list items into a dropdownlist within a datalist in code behind. Example please
musclejackAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

platinumbayCommented:
Are you creating the DataList and DropDownList programatically (in the code-behind)?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
musclejackAuthor Commented:
I have a aspx page below.  within the datalist, inside <EditItemTemplate>, there is a dropdownlist.  What I want to do is, for all the row that "Container.DataItem( "phone" ) " has the areacode of "415", add 2 options to the dropdownlist.  1. the value from Container.DataItem( "phone" ) , 2. 888-888-1234, and for the rest, add only 1 option to the dropdownlist, the value from Container.DataItem( "phone" ) <%@ Import Namespace="System.Data.SqlClient" %>
------------------------------------------------------------------------------------------------------------------------------------------------------------------
<Script Runat="Server">

Sub Page_Load
  If Not IsPostBack Then
    BindDataList
  End If
End Sub

Sub BindDataList
  Dim conPubs As SqlConnection
  Dim cmdSelect As SqlCommand
  Dim dtrAuthors As SqlDataReader

  conPubs = New SqlConnection( "Server=localhost;UID=sa;PWD=secret;Database=Pubs" )
  cmdSelect = New SqlCommand( "Select au_id, au_lname, phone From Authors Order by au_lname", conPubs )
  conPubs.Open()
  dtrAuthors = cmdSelect.ExecuteReader()

  dlstAuthors.DataSource = dtrAuthors
  dlstAuthors.DataBind()

  dtrAuthors.Close()
  conPubs.Close()
End Sub

Sub dlstAuthors_EditCommand( s As Object, e As DataListCommandEventArgs )
  dlstAuthors.EditItemIndex = e.Item.ItemIndex
  BindDataList
End Sub

Sub dlstAuthors_CancelCommand( s As Object, e As DataListCommandEventArgs )
  dlstAuthors.EditItemIndex = -1
  BindDataList
End Sub

Sub dlstAuthors_DeleteCommand( s As Object, e As DataListCommandEventArgs )
  Dim conPubs As SqlConnection
  Dim strDelete As String
  Dim cmdDelete As SqlCommand
  Dim strAuthorID As String

  strAuthorID = dlstAuthors.DataKeys( e.Item.ItemIndex )
  conPubs = New SqlConnection( "Server=localhost;UID=sa;PWD=secret;Database=Pubs" )
  strDelete = "Delete Authors Where au_id=@authorID"
  cmdDelete = New SqlCommand( strDelete, conPubs )
  cmdDelete.Parameters.Add( "@authorID", strAuthorID )
  conPubs.Open()
  cmdDelete.ExecuteNonQuery()
  conPubs.Close()
  dlstAuthors.EditItemIndex = -1
  BindDataList
End Sub

Sub dlstAuthors_UpdateCommand( s As Object, e As DataListCommandEventArgs )
  Dim conPubs As SqlConnection
  Dim strUpdate As String
  Dim cmdUpdate As SqlCommand
  Dim strAuthorID As String
  Dim txtLastName As TextBox
  Dim txtPhone As TextBox

  strAuthorID = dlstAuthors.DataKeys( e.Item.ItemIndex )
  txtLastName = e.Item.FindControl( "txtLastName" )
  txtPhone = e.Item.FindControl( "txtPhone" )
  conPubs = New SqlConnection( "Server=localhost;UID=sa;PWD=secret;Database=Pubs" )
  strUpdate = "Update Authors Set au_lname=@lastname, phone=@phone Where au_id=@authorID"
  cmdUpdate = New SqlCommand( strUpdate, conPubs )
  cmdUpdate.Parameters.Add( "@authorID", strAuthorID )
  cmdUpdate.Parameters.Add( "@lastname", txtLastName.Text )
  cmdUpdate.Parameters.Add( "@phone", txtPhone.Text )
  conPubs.Open()
  cmdUpdate.ExecuteNonQuery()
  conPubs.Close()
  dlstAuthors.EditItemIndex = -1
  BindDataList
End Sub

</Script>

<html>
<head><title>DataListEdit.aspx</title></head>
<body>
<form Runat="Server" ID="Form1">

<asp:DataList
  ID="dlstAuthors"
  DataKeyField="au_id"
  OnEditCommand="dlstAuthors_EditCommand"
  OnCancelCommand="dlstAuthors_CancelCommand"
  OnDeleteCommand="dlstAuthors_DeleteCommand"
  OnUpdateCommand="dlstAuthors_UpdateCommand"
  RepeatColumns="4"
  GridLines="Both"
  CellPadding="10"
  EditItemStyle-BackColor="lightgrey"
  Runat="Server">

<ItemTemplate>
  <%# Container.DataItem( "au_lname" )%>
  - <%# Container.DataItem( "phone" )%>
  <br>
  <asp:LinkButton
    Text="Edit!"
    CommandName="edit"
    Runat="Server" ID="Linkbutton1"/>
</ItemTemplate>

<EditItemTemplate>
  <b>Last Name:</b>
  <br>
  <asp:TextBox
    ID="txtLastName"
    Text='<%# Container.DataItem( "au_lname" )%>'
    Runat="Server" />
  <p>
  <b>Phone:</b>
  <br>
 
   <asp:DropDownList
     id="ddlPhone"
     runat="server"></asp:DropDownList>
  <p>
  <asp:LinkButton
    Text="Update!"
    CommandName="update"
    Runat="Server" ID="Linkbutton2"/>
  <asp:LinkButton
    Text="Delete!"
    CommandName="delete"
    Runat="Server" ID="Linkbutton3"/>
  <asp:LinkButton
    Text="Cancel!"
    CommandName="cancel"
    Runat="Server" ID="Linkbutton4"/>

</EditItemTemplate>

</asp:DataList>

</form>
</body>
</html>
0
platinumbayCommented:
What about using:

Private Sub dlstAuthors_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles dlstAuthors.ItemDataBound

or the ItemCreated event to get the object when it is created, and do your processing and logic there?
0
musclejackAuthor Commented:
never mind, i figured out myself
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.