How to read from data list it value into a label?


E

Ed Dror

Hi there,

I'm using asp.net 2.0 with SQL Server 2005

I created a datalist control and I would like to read the results into a
lable

So I wrote

Partial Class ProductDetails

Inherits System.Web.UI.Page

Public test_bar As String

Protected Sub DataList1_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataListItemEventArgs) Handles
DataList1.ItemDataBound
' Highlight the product name and unit price Labels

' First, get a reference to the two Label Web controls

Dim PNL1 As Label = CType(e.Item.FindControl("Sub_typeIDLabel"), Label)

Dim PNL2 As Label = CType(e.Item.FindControl("Sub_typeNameLabel"), Label)

Dim PNL3 As Label = CType(e.Item.FindControl("Label1"), Label)

Dim PNL4 As Label = CType(e.Item.FindControl("BSPLabel"), Label)

Dim PNL5 As Label = CType(e.Item.FindControl("RequiredLabel"), Label)

Dim PNL6 As Label = CType(e.Item.FindControl("Product_DescriptionLabel"),
Label)

Dim PNL7 As Label = CType(e.Item.FindControl("AmountLabel"), Label)

Dim PNL8 As Label = CType(e.Item.FindControl("LabelColor"), Label)

Dim PNL9 As Label = CType(e.Item.FindControl("Labelvendor_Name"), Label)

Dim PNL10 As Label = CType(e.Item.FindControl("GNLabel"), Label)

Dim PNL11 As Label = CType(e.Item.FindControl("NotesLabel"), Label)

Dim ProductNameLabel As Label = CType(e.Item.FindControl("GNLabel"), Label)

Dim PNL12 As DropDownList = CType(e.Item.FindControl("DLColor"),
DropDownList)

' Next, set their CssClass properties

If ProductNameLabel IsNot Nothing Then

'ProductNameLabel.CssClass = "AffordablePriceEmphasis"

Me.Label2.Text = ProductNameLabel.Text

End If

If PNL1 IsNot Nothing Then

'either chr(9) or vbTab works

Me.lblData.Text = PNL2.Text + Chr(9) + PNL7.Text + vbTab + Chr(49) + vbTab +
PNL11.Text + vbTab + PNL8.Text + vbTab + Chr(49) + vbTab + "00090" + vbTab +
PNL12.Text

test_bar = Me.lblData.Text

End If



Everything was fine but my Color field has multiple colors so I created a
Dropdown list inside the Datalist control and it show all the colors with no
problems

But when I tried to read into my Label (lblData) it will not reed (PNL12) -
Dim PNL12 As DropDownList = CType(e.Item.FindControl("DLColor"),
DropDownList)

My question is how to read from Dropdown list into a label inside the
datalist?

It read the PNL8 but not the PNL12

Thanks,

Ed Dror

Andrew Lauren
 
Ad

Advertisements

N

Nathan Sokalski

When getting the value from a DropDownList, you need to use one of the
following properties:

Text
SelectedValue
SelectedItem.Value
SelectedItem.Text

The first three get the Value property of the selected item, and the third
gets the Text property of the selected item. I prefer not to use the Text
property because it returns the Value property of the selected item, not the
Text property. Also, make sure that your DropDownList has ListItems, and
make sure the ListItems have a Value if you plan on using any of the first
three properties listed above. Which one you want to use depends on exactly
how you want to use it and your personal preferences. Hopefully this will
help.
 
O

Oded Dror

Nathan,

All I want is how to make this

Dim PNL12 As DropDownList = CType(e.Item.FindControl("DLColor"),
DropDownList)

To work ? what did I do wrong

Thanks,

Ed Dror
And when I put

Dim PNL12 As DropDownList = CType(e.Item.FindControl("DLColor"),
DropDownList).text

I'm getting Cant convert system.web.ui.controls.dropdownlist to a text or
somthing like that!
 
M

Mark Rae [MVP]

All I want is how to make this

Dim PNL12 As DropDownList = CType(e.Item.FindControl("DLColor"),
DropDownList)

To work ? what did I do wrong

1) Put a breakpoint on the above line

2) When the breakpoint is hit, inspect e.Item.FindControl("DLColor") in the
Immediate window - does it return a control reference...?

3) Assuming it does return a control reference, what type of control is
it...? If it's not a DropDownList, you won't be able to cast it into a
DropDownList variable...
And when I put

Dim PNL12 As DropDownList = CType(e.Item.FindControl("DLColor"),
DropDownList).text

I'm getting Cant convert system.web.ui.controls.dropdownlist to a text or
somthing like that!

Which would be correct - a DropDownList variable needs to be populated with
a reference to a DropDownList control, not a string...
 
W

Walter Wang [MSFT]

Hi,

FindControl is not recursive, therefore it's probably that the DLColor
isn't in the direct child hierarchy of e.Item. You can learn the control
tree hirearchy by turn on Trace option of the @ Page directive.

#In Search Of ASP.Net Controls
http://www.odetocode.com/Articles/116.aspx


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
E

Ed Dror

Walter,

I open a trace and I goted

Control UniqeId: DataList1$ctl00$DLColor
Type: System.Web.UI.WebControls.DropDownList
Render Size Byte (Including Children): 296
ViewState Size Byte(Excluding Childrens): 80
Control State Size Byte (excluding children): 0

So we can not retrieve a value of a control that resign inside other
control?
In this case DataList control inside DataList control

The reson behind that is my product has multiple colors
Lets say product1 has two colors black and white
In the Datalist it show only one record (black) and I want to show all color
per this product
So I created a dropdown list and all the information go into barcode
generator (I created a label that hold all the information for the barcode)
when the user choose different color it show empty in my label? it read all
the other labels from the Datalist control!

Thanks,
Ed Dror
 
Ad

Advertisements

W

Walter Wang [MSFT]

Hi Ed,

The trace output looks fine, but I'm afraid it's a little difficult to tell
the root cause without a reproducible project and debugging. Would you
please send me a smaller and reproducible project? Thanks.

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top