|
-
Dec 8th, 2002, 11:37 PM
#1
Thread Starter
Fanatic Member
[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:
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="webMSMX.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0">
<meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout" bgColor="#ffffff">
<form id="Form1" method="post" runat="server">
<table align="center" width="500" cellpadding="0" cellspacing="0">
<asp:DataList id="dlZipCodes" OnEditCommand="dlZipCodes_Click" style="Z-INDEX: 101; LEFT: 295px; POSITION: absolute; TOP: 173px" runat="server" Width="105" Height="80">
<ItemTemplate>
<tr>
<td><%# Container.DataItem("ExpireDate") %></td>
<td><asp:CheckBox ID="chkVisible" Runat="server" Text="" Checked=<%# Container.DataItem("Visible") %> /></td>
<td><asp:TextBox Runat="server" ID="lstZipCode" text='<%# Container.DataItem("ZipCode") %>' /></td>
<td><asp:Button CommandName="edit" Runat="server" Text="Update" /></td>
</tr>
<asp:Label Runat="server" ID="lblID" Visible="False" Text='<%# Container.DataItem("ID") %>' />
</ItemTemplate>
</asp:DataList>
</table>
</form>
</body>
</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:
Imports System.Data.SqlClient
Public Class WebForm1
Inherits System.Web.UI.Page
Protected WithEvents dlZipCodes As System.Web.UI.WebControls.DataList
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Not Page.IsPostBack Then
BindData()
End If
End Sub
Sub BindData()
'Create Connection
Dim objConn As SqlConnection
Dim objCmd As SqlCommand
Dim strSQL As String
strSQL = "SELECT * FROM ZipCodes WHERE UserID = 2 ORDER BY ZipCode"
objConn = New SqlConnection("Server=*********;UID=******;PWD=******;database=*******")
objCmd = New SqlCommand(strSQL, objConn)
'Set the datagrid's datasource to the datareader and databind
objConn.Open()
dlZipCodes.DataSource = objCmd.ExecuteReader(CommandBehavior.CloseConnection)
dlZipCodes.DataBind()
End Sub
Sub dlZipCodes_Click(ByVal s As Object, ByVal e As DataListCommandEventArgs)
Dim bolVisible As Boolean
bolVisible = CType(e.Item.FindControl("chkVisible"), CheckBox).Checked
Response.Write(bolVisible)
Dim strZipCode As String
strZipCode = CType(e.Item.FindControl("lstZipCode"), TextBox).Text
Response.Write(strZipCode)
Dim intID As Integer
intID = CType(e.Item.FindControl("lblID"), Label).Text
Response.Write(intID)
End Sub
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|