attipa
asked on
Obtaining item specific data in a datalist
Hello,
I am trying to get this to work and want to know if it is possible. I have a table called Store. In the table, I have columns that not all of the rows have to use. For example, the Address2 column is only used if the address of the store needs a second line. I am creating a datalist to display all stores in the state of CA. Some of the stores contain the Address2 column. In easier words, if I have 10 items in the datalist, I want 6 of them to call the data from Address2 column. How can I control each item individually? Is there any way I can call the data in the code behind file? How can I call it only when I need it? The code is below:
aspx file
-------------------------- --------
<asp:DataList ID="Retailers" Runat="server" RepeatColumns="2" RepeatDirection="Horizonta l" HorizontalAlign="Left">
<ItemTemplate>
<table width="180" border="0" cellpadding="0" cellspacing="0">
<tr> <td align="left" valign="top"> <font class="RetailerTitle"> <%# Container.DataItem("Name") %><br> </font><font class="RetailerAddress"> <%# Container.DataItem("Addres s1") %><br> </font><font class="RetailerAddress">
<%# Container.DataItem("City") %>
<%# Container.DataItem("State" ) %>
<%# Container.DataItem("Postal Code") %><br>
</font><font class="RetailerAddress">
<%# Container.DataItem("Teleph one") %><br>
<br>
</font>
</td></tr>
</table>
</ItemTemplate>
</datalist>
aspx.vb file
-------------------------- ----------
If location = "CA" Then
LocationLabel.Text = "California"
Dim store As New StoreManager
Dim reader As SqlDataReader
reader = store.GetStoresByState("CA ")
Retailers.DataSource = reader.Read()
Retailers.DataKeyField = "StoreID"
Retailers.DataBind()
End If
I am trying to get this to work and want to know if it is possible. I have a table called Store. In the table, I have columns that not all of the rows have to use. For example, the Address2 column is only used if the address of the store needs a second line. I am creating a datalist to display all stores in the state of CA. Some of the stores contain the Address2 column. In easier words, if I have 10 items in the datalist, I want 6 of them to call the data from Address2 column. How can I control each item individually? Is there any way I can call the data in the code behind file? How can I call it only when I need it? The code is below:
aspx file
--------------------------
<asp:DataList ID="Retailers" Runat="server" RepeatColumns="2" RepeatDirection="Horizonta
<ItemTemplate>
<table width="180" border="0" cellpadding="0" cellspacing="0">
<tr> <td align="left" valign="top"> <font class="RetailerTitle"> <%# Container.DataItem("Name")
<%# Container.DataItem("City")
<%# Container.DataItem("State"
<%# Container.DataItem("Postal
</font><font class="RetailerAddress">
<%# Container.DataItem("Teleph
<br>
</font>
</td></tr>
</table>
</ItemTemplate>
</datalist>
aspx.vb file
--------------------------
If location = "CA" Then
LocationLabel.Text = "California"
Dim store As New StoreManager
Dim reader As SqlDataReader
reader = store.GetStoresByState("CA
Retailers.DataSource = reader.Read()
Retailers.DataKeyField = "StoreID"
Retailers.DataBind()
End If
ASKER
Hi,
Thanks for the response. I read the entire topic and I am even more confused on what to do. Is there anyway you can incorporate it into the code I provided before. I would really appreciate it. The thing I am trying to do is on the top code where it says..........
<%# Container.DataItem("Addres s1") %>
If there is something in the Address2 Column of the table for that specific item
Add <%# Container.DataItem("Addres s2") %><br>
End If
Any help is greatly appreciated.
Thanks for the response. I read the entire topic and I am even more confused on what to do. Is there anyway you can incorporate it into the code I provided before. I would really appreciate it. The thing I am trying to do is on the top code where it says..........
<%# Container.DataItem("Addres
If there is something in the Address2 Column of the table for that specific item
Add <%# Container.DataItem("Addres
End If
Any help is greatly appreciated.
ASKER
Can someone please help me...I really have to get this project done soon. Thanks a million in advance.
The easiest way is to have your item template always include the Address2 info, but make it invisible if there is no data. After you associate the ItemDataBound event handler with the control, it would go something like this:
Sub Item_Bound(sender As Object, e As DataListItemEventArgs)
If e.Item.ItemType = ListItemType.Item Or _
e.Item.ItemType = ListItemType.AlternatingIt em Then
If (CType(e.Item.DataItem, DataRowView)).Row.ItemArra y(2) = System.DbNull.Value
' Retrieve a Label control in the current DataListItem.
Dim lblAddress2 As Label = _
CType(e.Item.FindControl(" Address2") , Label)
lblAddress2.Visible = false
End If
End If
End Sub
Sub Item_Bound(sender As Object, e As DataListItemEventArgs)
If e.Item.ItemType = ListItemType.Item Or _
e.Item.ItemType = ListItemType.AlternatingIt
If (CType(e.Item.DataItem, DataRowView)).Row.ItemArra
' Retrieve a Label control in the current DataListItem.
Dim lblAddress2 As Label = _
CType(e.Item.FindControl("
lblAddress2.Visible = false
End If
End If
End Sub
<asp:DataList ID="Retailers" Runat="server" RepeatColumns="2" RepeatDirection="Horizonta l" HorizontalAlign="Left" OnItemDataBound="Item_Boun d">
<ItemTemplate>
<table width="180" border="0" cellpadding="0" cellspacing="0">
<tr> <td align="left" valign="top"> <font class="RetailerTitle"> <%# Container.DataItem("Name") %><br> </font><font class="RetailerAddress"> <asp:Label id="lblAddr1" runat="server" Text='<%# DataBinder.Eval(Container. DataItem, "Address1") %>' />
<br> </font>
<asp:Label id="lblAddr2" runat="server" Text='<%# DataBinder.Eval(Container. DataItem, "Address2") %>' />
<font class="RetailerAddress">
<%# Container.DataItem("City") %>
<%# Container.DataItem("State" ) %>
<%# Container.DataItem("Postal Code") %><br>
</font><font class="RetailerAddress">
<%# Container.DataItem("Teleph one") %><br>
<br>
</font>
</td></tr>
</table>
</ItemTemplate>
</datalist>
create itemdatabound event
Sub Item_Bound(sender As Object, e As RepeatertemEventArgs)
If e.Item.ItemType = ListItemType.Item Or _
e.Item.ItemType = ListItemType.AlternatingIt em Then
' Retrieve the Label control in the current DataListItem.
Dim addr1 As Label = _
CType(e.Item.FindControl(" lblAddr1") , Label)
Dim addr2 As Label = _
CType(e.Item.FindControl(" lblAddr2") , Label)
if Not addr1.Text = String.Empty Then
addr2.Visible = True
else
addr2.Visible = False
End If
End If
End Sub
B..M
mmarinov
<ItemTemplate>
<table width="180" border="0" cellpadding="0" cellspacing="0">
<tr> <td align="left" valign="top"> <font class="RetailerTitle"> <%# Container.DataItem("Name")
<br> </font>
<asp:Label id="lblAddr2" runat="server" Text='<%# DataBinder.Eval(Container.
<font class="RetailerAddress">
<%# Container.DataItem("City")
<%# Container.DataItem("State"
<%# Container.DataItem("Postal
</font><font class="RetailerAddress">
<%# Container.DataItem("Teleph
<br>
</font>
</td></tr>
</table>
</ItemTemplate>
</datalist>
create itemdatabound event
Sub Item_Bound(sender As Object, e As RepeatertemEventArgs)
If e.Item.ItemType = ListItemType.Item Or _
e.Item.ItemType = ListItemType.AlternatingIt
' Retrieve the Label control in the current DataListItem.
Dim addr1 As Label = _
CType(e.Item.FindControl("
Dim addr2 As Label = _
CType(e.Item.FindControl("
if Not addr1.Text = String.Empty Then
addr2.Visible = True
else
addr2.Visible = False
End If
End If
End Sub
B..M
mmarinov
ASKER
Okay.....I got that portion working. Thanks a million mmarinov. Now, I have pasted my code below, it will only work for one database row. When there is two or more, it won't work. Here is what I have..........
.aspx page
-------------------------- ---------- ----------
<asp:datalist id="Retailers" OnItemDataBound="Item_Boun d" HorizontalAlign="Left" RepeatDirection="Horizonta l"
RepeatColumns="2" Runat="server">
<ItemTemplate>
<table width="180" border="0" cellpadding="0" cellspacing="0">
<tr>
<td align="left" valign="top">
<font class="RetailerTitle">
<asp:Label ID="StoreName" Runat="server" CssClass="RetailerTitle" Visible="True"></asp:Label ><br>
</font>
<font class="RetailerSubTitle">
<asp:Label ID="StoreLocation" Runat="server" CssClass="RetailerSubTitle " Visible="True"></asp:Label >
</font>
<font class="RetailerAddress">
<asp:Label ID="StoreInformation" Runat="server" CssClass="RetailerAddress" Visible="True"></asp:Label >
</font><font class="RetailerAddress">
<br>
<br>
</font>
</td>
</tr>
</table>
</ItemTemplate>
<SeparatorTemplate>
<table width="20" cellpadding="0" cellspacing="0" border="0">
<tr>
<td align="center" valign="top">
</td>
</tr>
</table>
</SeparatorTemplate>
</asp:datalist>
.aspx.vb file (code-behind)
-------------------------- ---------- ---------- ---------- ---------- ---------
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim location As String = Request.Params("Location")
AddHandler Retailers.ItemDataBound, AddressOf Item_Bound
If Not Page.IsPostBack Then
If location = "" Then
Response.Redirect("store-l ocator.asp x")
ElseIf location = "CA" Then
Retailers.DataSource = CreateDataSource()
Retailers.DataBind()
End If
End If
End Sub
Public Function CreateDataSource() As ICollection
Dim dt As DataTable = New DataTable
Dim dr As DataRow
Dim location As String = Request.Params("Location")
Dim store As New StoreManager
Dim reader As SqlDataReader
reader = store.GetStoresByState("CA ")
dt.Columns.Add(New DataColumn("Name", GetType(String)))
dt.Columns.Add(New DataColumn("Location", GetType(String)))
dt.Columns.Add(New DataColumn("Address1", GetType(String)))
dt.Columns.Add(New DataColumn("Address2", GetType(String)))
dt.Columns.Add(New DataColumn("City", GetType(String)))
dt.Columns.Add(New DataColumn("State", GetType(String)))
dt.Columns.Add(New DataColumn("PostalCode", GetType(String)))
dt.Columns.Add(New DataColumn("Telephone", GetType(String)))
dt.Columns.Add(New DataColumn("Website", GetType(String)))
Dim i As Integer
reader.Read()
<------------------------- -SOMETHING HAS TO BE ENTERED HERE...I THINK...
For i = 0 To 0 <---------------I PUT A ZERO HERE JUST SO IT DOES IT ONCE...I TRIED A WHILE LOOP
<------------------WITH COUNT AND READER.READ() AND IT DIDNT WORK
dr = dt.NewRow()
dr(0) = reader("Name").ToString()
dr(1) = reader("Location").ToStrin g()
dr(2) = reader("Address1").ToStrin g()
dr(3) = reader("Address2").ToStrin g()
dr(4) = reader("City").ToString()
dr(5) = reader("State").ToString()
dr(6) = reader("PostalCode").ToStr ing()
dr(7) = reader("Telephone").ToStri ng()
dr(8) = reader("Website").ToString ()
dt.Rows.Add(dr)
Next i
Dim dv As DataView = New DataView(dt)
Return dv
End Function
Public Sub Item_Bound(ByVal sender As Object, ByVal e As DataListItemEventArgs)
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingIt em Then
Dim StoreName As Label = CType(e.Item.FindControl(" StoreName" ), Label)
Dim StoreLocation As Label = CType(e.Item.FindControl(" StoreLocat ion"), Label)
Dim StoreInformation As Label = CType(e.Item.FindControl(" StoreInfor mation"), Label)
StoreName.Text = Convert.ToString((CType(e. Item.DataI tem, DataRowView)).Row.ItemArra y(0).ToStr ing())
Dim StoreLocationString As StringBuilder = New StringBuilder
If Not (Convert.ToString((CType(e .Item.Data Item, DataRowView)).Row.ItemArra y(1).ToStr ing())) = "" Then
StoreLocationString.Append (Convert.T oString((C Type(e.Ite m.DataItem , DataRowView)).Row.ItemArra y(1).ToStr ing()))
StoreLocationString.Append ("<br>")
End If
StoreLocation.Text = StoreLocationString.ToStri ng()
Dim StoreInformationString As StringBuilder = New StringBuilder
StoreInformationString.App end(Conver t.ToString ((CType(e. Item.DataI tem, DataRowView)).Row.ItemArra y(2).ToStr ing()))
StoreInformationString.App end("<br>" )
If Not (Convert.ToString((CType(e .Item.Data Item, DataRowView)).Row.ItemArra y(3).ToStr ing())) = "" Then
StoreInformationString.App end(Conver t.ToString ((CType(e. Item.DataI tem, DataRowView)).Row.ItemArra y(3).ToStr ing()))
StoreInformationString.App end("<br>" )
End If
StoreInformationString.App end((CType (e.Item.Da taItem, DataRowView)).Row.ItemArra y(4).ToStr ing())
StoreInformationString.App end(" ")
StoreInformationString.App end((CType (e.Item.Da taItem, DataRowView)).Row.ItemArra y(5).ToStr ing())
StoreInformationString.App end(" ")
StoreInformationString.App end((CType (e.Item.Da taItem, DataRowView)).Row.ItemArra y(6).ToStr ing())
StoreInformationString.App end("<br>" )
If Not (Convert.ToString((CType(e .Item.Data Item, DataRowView)).Row.ItemArra y(7).ToStr ing())) = "" Then
StoreInformationString.App end(Conver t.ToString ((CType(e. Item.DataI tem, DataRowView)).Row.ItemArra y(7).ToStr ing()))
StoreInformationString.App end("<br>" )
End If
If Not (Convert.ToString((CType(e .Item.Data Item, DataRowView)).Row.ItemArra y(8).ToStr ing())) = "" Then
StoreInformationString.App end(Conver t.ToString ((CType(e. Item.DataI tem, DataRowView)).Row.ItemArra y(8).ToStr ing()))
StoreInformationString.App end("<br>" )
End If
StoreInformation.Text = StoreInformationString.ToS tring()
End If
End Sub
.aspx page
--------------------------
<asp:datalist id="Retailers" OnItemDataBound="Item_Boun
RepeatColumns="2" Runat="server">
<ItemTemplate>
<table width="180" border="0" cellpadding="0" cellspacing="0">
<tr>
<td align="left" valign="top">
<font class="RetailerTitle">
<asp:Label ID="StoreName" Runat="server" CssClass="RetailerTitle" Visible="True"></asp:Label
</font>
<font class="RetailerSubTitle">
<asp:Label ID="StoreLocation" Runat="server" CssClass="RetailerSubTitle
</font>
<font class="RetailerAddress">
<asp:Label ID="StoreInformation" Runat="server" CssClass="RetailerAddress"
</font><font class="RetailerAddress">
<br>
<br>
</font>
</td>
</tr>
</table>
</ItemTemplate>
<SeparatorTemplate>
<table width="20" cellpadding="0" cellspacing="0" border="0">
<tr>
<td align="center" valign="top">
</td>
</tr>
</table>
</SeparatorTemplate>
</asp:datalist>
.aspx.vb file (code-behind)
--------------------------
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim location As String = Request.Params("Location")
AddHandler Retailers.ItemDataBound, AddressOf Item_Bound
If Not Page.IsPostBack Then
If location = "" Then
Response.Redirect("store-l
ElseIf location = "CA" Then
Retailers.DataSource = CreateDataSource()
Retailers.DataBind()
End If
End If
End Sub
Public Function CreateDataSource() As ICollection
Dim dt As DataTable = New DataTable
Dim dr As DataRow
Dim location As String = Request.Params("Location")
Dim store As New StoreManager
Dim reader As SqlDataReader
reader = store.GetStoresByState("CA
dt.Columns.Add(New DataColumn("Name", GetType(String)))
dt.Columns.Add(New DataColumn("Location", GetType(String)))
dt.Columns.Add(New DataColumn("Address1", GetType(String)))
dt.Columns.Add(New DataColumn("Address2", GetType(String)))
dt.Columns.Add(New DataColumn("City", GetType(String)))
dt.Columns.Add(New DataColumn("State", GetType(String)))
dt.Columns.Add(New DataColumn("PostalCode", GetType(String)))
dt.Columns.Add(New DataColumn("Telephone", GetType(String)))
dt.Columns.Add(New DataColumn("Website", GetType(String)))
Dim i As Integer
reader.Read()
<-------------------------
For i = 0 To 0 <---------------I PUT A ZERO HERE JUST SO IT DOES IT ONCE...I TRIED A WHILE LOOP
<------------------WITH COUNT AND READER.READ() AND IT DIDNT WORK
dr = dt.NewRow()
dr(0) = reader("Name").ToString()
dr(1) = reader("Location").ToStrin
dr(2) = reader("Address1").ToStrin
dr(3) = reader("Address2").ToStrin
dr(4) = reader("City").ToString()
dr(5) = reader("State").ToString()
dr(6) = reader("PostalCode").ToStr
dr(7) = reader("Telephone").ToStri
dr(8) = reader("Website").ToString
dt.Rows.Add(dr)
Next i
Dim dv As DataView = New DataView(dt)
Return dv
End Function
Public Sub Item_Bound(ByVal sender As Object, ByVal e As DataListItemEventArgs)
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingIt
Dim StoreName As Label = CType(e.Item.FindControl("
Dim StoreLocation As Label = CType(e.Item.FindControl("
Dim StoreInformation As Label = CType(e.Item.FindControl("
StoreName.Text = Convert.ToString((CType(e.
Dim StoreLocationString As StringBuilder = New StringBuilder
If Not (Convert.ToString((CType(e
StoreLocationString.Append
StoreLocationString.Append
End If
StoreLocation.Text = StoreLocationString.ToStri
Dim StoreInformationString As StringBuilder = New StringBuilder
StoreInformationString.App
StoreInformationString.App
If Not (Convert.ToString((CType(e
StoreInformationString.App
StoreInformationString.App
End If
StoreInformationString.App
StoreInformationString.App
StoreInformationString.App
StoreInformationString.App
StoreInformationString.App
StoreInformationString.App
If Not (Convert.ToString((CType(e
StoreInformationString.App
StoreInformationString.App
End If
If Not (Convert.ToString((CType(e
StoreInformationString.App
StoreInformationString.App
End If
StoreInformation.Text = StoreInformationString.ToS
End If
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Wait, backup. You don't need to create a table via a SqlDataReader then bind to the table. Just bind directly to the reader: it's much better performing and simpler.
'initialize reader
myDataList.DataSource = reader
myDataList.DataBind()
'initialize reader
myDataList.DataSource = reader
myDataList.DataBind()
ASKER
thanks a milliom mmarinov
Hey! I also provided the specific implementation algorithm and a way for you to boost performance and throw away 30+ lines of useless overhead.
Somedays I just don't feel appreciated. ;-) I guess you can't please everyone.
Somedays I just don't feel appreciated. ;-) I guess you can't please everyone.
ASKER
Im sorry about the points, but you can not use that method if you are not implementing every column in every datalist entry.
No worries, though a thank you would have been nice. :-)
Actually since the processing happens in the ItemDataBound event, the methods are equivalent. You simply bind, then optionally set a control's visibility to false if a certain piece of data is blank/null. The nature of the DataSource is irrelevant. It could be a SqlDataReader or a DataTable (or something else).
In other words, whereas you're initializing a DataReader, using it to build a table, then binding to the table (letting ItemDataBound do its magic), you could have just bound directly to the DateReader for the same effect.
Actually since the processing happens in the ItemDataBound event, the methods are equivalent. You simply bind, then optionally set a control's visibility to false if a certain piece of data is blank/null. The nature of the DataSource is irrelevant. It could be a SqlDataReader or a DataTable (or something else).
In other words, whereas you're initializing a DataReader, using it to build a table, then binding to the table (letting ItemDataBound do its magic), you could have just bound directly to the DateReader for the same effect.
there is an event ItemDataBound to work with data in DataList before show it to the user
DataList.ItemDataBound Event: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebuiwebcontrolsdatalistclassitemdataboundtopic.asp
Regards!
B..M
mmarinov