Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 1st, 2004, 02:25 PM
Authorized User
 
Join Date: Nov 2003
Location: Norco, CA, USA.
Posts: 73
Thanks: 0
Thanked 0 Times in 0 Posts
Default Labels in DataLists

Please help again. I have a
<asp:datalist id="ProdList" runat="server" DataKeyField="productTypeID" OnItemCommand="ProdList_ItemCommand">
<ItemTemplate>
<asp:Label id="lblproductTypeID" runat="server">
<%# DataBinder.Eval(Container.DataItem, "productTypeID") %>
</asp:Label>
<asp:Button id="Button1" runat="server" CssClass="ProductListButton" ToolTip="Show/Hide Details" Width="65px" Text="Details" CommandName="expand"></asp:Button>


and when I run this the label does contain the correct productTypeID.
However in the C# code behind page when the button is clicked I try to get the contents of the label and fail.
public void ProdList_ItemCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e) {
//get the list index of the product selected
ProdList.SelectedIndex = e.Item.ItemIndex;
// using that index, get a handle on the Label
Label ptypeID = (Label)ProdList.Items[ProdList.SelectedIndex].FindControl("lblProductTypeID");

......
int productTypeID1 = Convert.ToInt32(ptypeID.Text);

ptypeID.Text = "" and
productTypeID1 ""

First, Why doesn't this work?
Second, could I somehow get the selected index of the DataKeyField more easily than using a label?

Thanks in advance for your help.







Sandy
__________________
Sandy
 
Old April 1st, 2004, 02:41 PM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Don't you just wish that C# wasn't case sensitive, every now and then??? ;)

<asp:Label id="lblproductTypeID" runat="server">

Vs

// using that index, get a handle on the Label
Label ptypeID = (Label)ProdList.Items[ProdList.SelectedIndex].FindControl("lblProductTypeID");

Does this help?

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old April 1st, 2004, 03:17 PM
Authorized User
 
Join Date: Nov 2003
Location: Norco, CA, USA.
Posts: 73
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Arrrrrrgh!!! (Sound of chewing nails)

But - - now
<asp:Label id="lblProductTypeID" runat="server">
<%# DataBinder.Eval(Container.DataItem, "productTypeID") %>
</asp:Label>

and I get the same results:
ptypeID.Text = "" and
productTypeID1 ""

and
int productTypeID1 = Convert.ToInt32(ptypeID.Text);
produces an error "not in correct format"


Sandy
 
Old April 1st, 2004, 03:30 PM
Authorized User
 
Join Date: Nov 2003
Location: Norco, CA, USA.
Posts: 73
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I found the problem. This syntax must be used:
<asp:Label id=lblProductTypeID runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "productTypeID") %>'>
</asp:Label>

and then it works.

On the second question, is there indeed a way to get the DataList DataKeyField with an ItemCopmmand?


Sandy
 
Old April 2nd, 2004, 03:28 AM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Oh, yes there is. You can retrieve the DataKey from the List control using its DataKeys collection and the ItemIndex for the "current" item, like this:

int selectedIndex = e.Item.ItemIndex;
int dataKey = (int) ProdList.DataKeys[selectedIndex];

Cheers,

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.




Similar Threads
Thread Thread Starter Forum Replies Last Post
Vertical Labels bfoley CSS Cascading Style Sheets 2 November 26th, 2007 02:06 PM
Labels in Access taraj Access 2 May 16th, 2005 01:35 PM
Labels slgknjn Beginning VB 6 9 February 25th, 2005 04:12 PM
UserControls in DataLists shmacgregor ASP.NET 1.0 and 1.1 Basics 12 April 5th, 2004 04:11 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.