Results 1 to 3 of 3

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

Threaded View

  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.

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