Results 1 to 3 of 3

Thread: [Resolved] DropDownList in DataList (Dynamic ListItems as well!)

  1. #1

    Thread Starter
    Fanatic Member Redth's Avatar
    Join Date
    May 2001
    Location
    Ontario, Canada
    Posts
    551

    [Resolved] DropDownList in DataList (Dynamic ListItems as well!)

    Hello,

    I've only recently discovered the usefullness of datalists, datagrids, and repeaters only due to my stubborness to try new things because they sound "too easy" and not customizable enough...

    anyways, i'm trying to build a datalist where users can edit zipcodes... there's 4 columns, the expiry date, the checkbox of whether the zipcode is visible to public or not, the Update button, and the zipcode itself..

    now this is my problem.. i can manage it so the zipcode is editted through a normal textbox, but this is too unrestrictive.. i want to restrict my users to only certain zipcodes... to do this, i need to use a dropdownlist...

    now, this is ok... i can put a dropdown list in there, but how do i get the dropdownlist to select the right listitem when loading info into the datagrid? i don't know what the selectedindex will be, and the only way to do it would be to use the find method to find by text... not sure how to code this...

    and to complicate the situation more, the listitems to appear in the dropdownlists will not always be the same.. i have an xml file with the zipcodes in it... so, that needs to dynamically happen as well... and i have no clue how to go about this....

    here's my html code for the page right now:

    VB Code:
    1. <%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="webMSMX.WebForm1"%>
    2. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    3. <HTML>
    4.     <HEAD>
    5.         <title>WebForm1</title>
    6.         <meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0">
    7.         <meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
    8.         <meta name="vs_defaultClientScript" content="JavaScript">
    9.         <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    10.     </HEAD>
    11.     <body MS_POSITIONING="GridLayout" bgColor="#ffffff">
    12.         <form id="Form1" method="post" runat="server">
    13.             <table align="center" width="500" cellpadding="0" cellspacing="0">
    14.                 <asp:DataList id="dlZipCodes" OnEditCommand="dlZipCodes_Click" style="Z-INDEX: 101; LEFT: 295px; POSITION: absolute; TOP: 173px" runat="server" Width="105" Height="80">
    15.                     <ItemTemplate>
    16.                         <tr>
    17.                             <td><%# Container.DataItem("ExpireDate") %></td>
    18.                             <td><asp:CheckBox ID="chkVisible" Runat="server" Text="" Checked=<%# Container.DataItem("Visible") %> /></td>
    19.                             <td><asp:TextBox Runat="server" ID="lstZipCode" text='<%# Container.DataItem("ZipCode") %>' /></td>
    20.                             <td><asp:Button CommandName="edit" Runat="server" Text="Update" /></td>
    21.                         </tr>
    22.                        
    23.                         <asp:Label Runat="server" ID="lblID" Visible="False" Text='<%# Container.DataItem("ID") %>' />
    24.                    
    25.                     </ItemTemplate>
    26.            
    27.                 </asp:DataList>
    28.             </table>
    29.         </form>
    30.     </body>
    31. </HTML>

    no biggie here, just a simple datalist so far... notice that i'm using a textbox right now for the zipcode simply because i don't know how to use a dropdownlist yet :)


    and here's my codebehind code for the page right now:

    VB Code:
    1. Imports System.Data.SqlClient
    2. Public Class WebForm1
    3.     Inherits System.Web.UI.Page
    4.     Protected WithEvents dlZipCodes As System.Web.UI.WebControls.DataList
    5.  
    6. #Region " Web Form Designer Generated Code "
    7.  
    8.     'This call is required by the Web Form Designer.
    9.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    10.  
    11.     End Sub
    12.  
    13.     Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
    14.         'CODEGEN: This method call is required by the Web Form Designer
    15.         'Do not modify it using the code editor.
    16.         InitializeComponent()
    17.     End Sub
    18.  
    19. #End Region
    20.  
    21.     Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    22.         'Put user code to initialize the page here
    23.         If Not Page.IsPostBack Then
    24.             BindData()
    25.         End If
    26.  
    27.     End Sub
    28.     Sub BindData()
    29.  
    30.         'Create Connection
    31.         Dim objConn As SqlConnection
    32.         Dim objCmd As SqlCommand
    33.         Dim strSQL As String
    34.  
    35.         strSQL = "SELECT * FROM ZipCodes WHERE UserID = 2 ORDER BY ZipCode"
    36.  
    37.         objConn = New SqlConnection("Server=*********;UID=******;PWD=******;database=*******")
    38.         objCmd = New SqlCommand(strSQL, objConn)
    39.  
    40.         'Set the datagrid's datasource to the datareader and databind
    41.         objConn.Open()
    42.         dlZipCodes.DataSource = objCmd.ExecuteReader(CommandBehavior.CloseConnection)
    43.         dlZipCodes.DataBind()
    44.  
    45.     End Sub
    46.  
    47.     Sub dlZipCodes_Click(ByVal s As Object, ByVal e As DataListCommandEventArgs)
    48.  
    49.         Dim bolVisible As Boolean
    50.         bolVisible = CType(e.Item.FindControl("chkVisible"), CheckBox).Checked
    51.         Response.Write(bolVisible)
    52.  
    53.         Dim strZipCode As String
    54.         strZipCode = CType(e.Item.FindControl("lstZipCode"), TextBox).Text
    55.         Response.Write(strZipCode)
    56.  
    57.         Dim intID As Integer
    58.         intID = CType(e.Item.FindControl("lblID"), Label).Text
    59.         Response.Write(intID)
    60.  
    61.     End Sub
    62.  
    63. End Class

    that is just the basic page i'm working on right now simply to get things working before i plug it into the templates and such... so far, getting the checkbox value is simple, same with ID and well for now, the textbox...

    but i'd REALLY like to get the dropdownlist working... it's quite crucial! i really appreciate any thought towards this situation! thanks!
    Last edited by Redth; Dec 9th, 2002 at 12:22 AM.

  2. #2

    Thread Starter
    Fanatic Member Redth's Avatar
    Join Date
    May 2001
    Location
    Ontario, Canada
    Posts
    551
    well, wow,

    i'm sorry i wasted everyone's time reading this who did, but it just hit me right after i posted how to do this, and well, i got it working with relatively little trouble.. i'll post the solution in a minute.. hopefully it'll help someone in the future!

  3. #3

    Thread Starter
    Fanatic Member Redth's Avatar
    Join Date
    May 2001
    Location
    Ontario, Canada
    Posts
    551
    here's my html now:

    VB Code:
    1. <%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="webMSMX.WebForm1"%>
    2. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    3. <HTML>
    4.     <HEAD>
    5.         <title>WebForm1</title>
    6.         <meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0">
    7.         <meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
    8.         <meta name="vs_defaultClientScript" content="JavaScript">
    9.         <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    10.     </HEAD>
    11.     <body MS_POSITIONING="GridLayout" bgColor="#ffffff">
    12.         <form id="Form1" method="post" runat="server">
    13.             <table align="center" width="500" cellpadding="0" cellspacing="0">
    14.                 <asp:DataList id="dlZipCodes" OnEditCommand="dlZipCodes_Click" style="Z-INDEX: 101; LEFT: 295px; POSITION: absolute; TOP: 173px" runat="server" Width="105" Height="80">
    15.                     <ItemTemplate>
    16.                         <tr>
    17.                             <td><asp:Label Runat="server" ID="lblZIP_Date" Text='<%# Container.DataItem("ExpireDate") %>' /></td>
    18.                             <td><asp:CheckBox ID="chkVisible" Runat="server" Text="" Checked=<%# Container.DataItem("Visible") %> /></td>
    19.                             <td><asp:DropDownList Runat="server" ID="lstZipCodes" ></asp:DropDownList></td>
    20.                             <td><asp:Button CommandName="edit" Runat="server" Text="Update" /></td>
    21.                         </tr>
    22.                        
    23.                         <asp:Label Runat="server" ID="lblID" Visible="False" Text='<%# Container.DataItem("ID") %>' />
    24.                         <asp:Label Runat="server" ID="lblZip" Visible="False" text='<%# Container.DataItem("ZipCode") %>' />
    25.                     </ItemTemplate>
    26.            
    27.                 </asp:DataList>
    28.             </table>
    29.         </form>
    30.     </body>
    31. </HTML>

    notice here that i simply put an unfilled dropdownlist :)


    here's the codebehind now:
    VB Code:
    1. Imports System.Data.SqlClient
    2. Public Class WebForm1
    3.     Inherits System.Web.UI.Page
    4.     Protected WithEvents dlZipCodes As System.Web.UI.WebControls.DataList
    5.  
    6. #Region " Web Form Designer Generated Code "
    7.  
    8.     'This call is required by the Web Form Designer.
    9.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    10.  
    11.     End Sub
    12.  
    13.     Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
    14.         'CODEGEN: This method call is required by the Web Form Designer
    15.         'Do not modify it using the code editor.
    16.         InitializeComponent()
    17.     End Sub
    18.  
    19. #End Region
    20.  
    21.     Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    22.         'Put user code to initialize the page here
    23.         If Not Page.IsPostBack Then
    24.             BindData()
    25.             PopulateZipCodeLists()
    26.         End If
    27.  
    28.     End Sub
    29.     Sub BindData()
    30.  
    31.         'Create Connection
    32.         Dim objConn As SqlConnection
    33.         Dim objCmd As SqlCommand
    34.         Dim strSQL As String
    35.  
    36.         strSQL = "SELECT * FROM ZipCodes WHERE UserID = 2 ORDER BY ZipCode"
    37.  
    38.         objConn = New SqlConnection("Server=***.***.***.***;UID=****;PWD=****;database=******")
    39.         objCmd = New SqlCommand(strSQL, objConn)
    40.  
    41.         'Set the datagrid's datasource to the datareader and databind
    42.         objConn.Open()
    43.         dlZipCodes.DataSource = objCmd.ExecuteReader(CommandBehavior.CloseConnection)
    44.         dlZipCodes.DataBind()
    45.  
    46.     End Sub
    47.  
    48.     Sub PopulateZipCodeLists()
    49.         Dim dlItem As DataListItem
    50.         Dim dlList As DropDownList
    51.  
    52.         Dim dsZip As DataSet
    53.         Dim dRow As DataRow
    54.  
    55.         Dim lstItem As System.Web.UI.WebControls.ListItem
    56.  
    57.         'Load ZipCodes from xml file into dataset
    58.         dsZip = New DataSet()
    59.         dsZip.ReadXml("F:\WebSites\MSMX\Common\ZipCodes.xml")
    60.  
    61.         'For each row in the datalist
    62.         For Each dlItem In dlZipCodes.Items
    63.             'Set the dropdownlist control to the row's dropdownlist
    64.             dlList = dlItem.FindControl("lstZipCodes")
    65.  
    66.             'For each zipcode inthe xml file
    67.             For Each dRow In dsZip.Tables(0).Rows
    68.  
    69.                 'Create ListItem, assign its text and value from xml file
    70.                 lstItem = New System.Web.UI.WebControls.ListItem()
    71.                 lstItem.Text = dRow("Zip")
    72.                 lstItem.Value = dRow("Zip")
    73.  
    74.                 'Add listitem to the dropdownlist in the data row we're on
    75.                 dlList.Items.Add(lstItem)
    76.  
    77.             Next
    78.  
    79.             'Try to select the correct listitem by using the zipcode value we stored in the invisible label
    80.             Try
    81.                 dlList.Items.FindByValue(CType(dlItem.FindControl("lblZip"), Label).Text).Selected = True
    82.             Catch
    83.                 'The listitem or control could not be found, don't do anything
    84.             End Try
    85.         Next
    86.  
    87.     End Sub
    88.  
    89.     Sub dlZipCodes_Click(ByVal s As Object, ByVal e As DataListCommandEventArgs)
    90.  
    91.         Dim bolVisible As Boolean
    92.         bolVisible = CType(e.Item.FindControl("chkVisible"), CheckBox).Checked
    93.         Response.Write(bolVisible)
    94.  
    95.         Dim strZipCode As String
    96.         strZipCode = CType(e.Item.FindControl("lstZipCodes"), DropDownList).SelectedItem.Value
    97.         Response.Write(strZipCode)
    98.  
    99.         Dim intID As Integer
    100.         intID = CType(e.Item.FindControl("lblID"), Label).Text
    101.         Response.Write(intID)
    102.  
    103.         Dim dteDate As DateTime
    104.         dteDate = Convert.ToDateTime(CType(e.Item.FindControl("lblZIP_Date"), Label).Text)
    105.         Response.Write(dteDate.ToShortDateString())
    106.  
    107.  
    108.     End Sub
    109.  
    110. End Class


    This may or may not be the most efficient way of doing things, but one thing i know for sure, is that it works! and that fact makes me happy... so again, waste of forum space, but maybe someone will find it useful someday... it seems like something that could occur in other people's work.. and i now know i love datalists... wow, gonna be using them like crazy now.. say goodbye to coding that stuff manually! :) databinding is THE way to go!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width