Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2004-11-22
4
Medium Priority
?
143 Views
Last Modified: 2010-04-23
Anyone?  My question is - is there a way to add list items into a dropdownlist within a datalist in code behind. Example please
0
Comment
Question by:musclejack
  • 2
  • 2
4 Comments
 
LVL 6

Accepted Solution

by:
platinumbay earned 1000 total points
ID: 12649773
Are you creating the DataList and DropDownList programatically (in the code-behind)?
0
 

Author Comment

by:musclejack
ID: 12650743
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
 
LVL 6

Expert Comment

by:platinumbay
ID: 12651917
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
 

Author Comment

by:musclejack
ID: 12657161
never mind, i figured out myself
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question