Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
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
  #2 (permalink)  
Old April 1st, 2004, 02:41 PM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 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.
  #3 (permalink)  
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
  #4 (permalink)  
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
  #5 (permalink)  
Old April 2nd, 2004, 03:28 AM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 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.
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 01:29 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.