Retrieving control values from within a DataList


Hi everyone,

I've been programming for many years, but have just started teaching myself .NET within the last year.. and I have an issue, in which I could use some of your expert advice ;)

(This is developed in vb.net)

I'm populating a Datalist on page load (if not ispostback).  There are are a series of controls, within the datalist (labels, a textbox, a hyperlink, a button & a dropdownlist).

When the button is clicked (CommandName = AddToCart), I'm retrieving the values of these controls and populating some session variables.  It works great for the textbox & dropdownlist, but for some reason I'm unable to retrieve the values from the labels & hyperlink.

The code compiles properly and executes properly, but the values from the labels & hyperlink are blank.

At this point, all of the controls, including the datalist, have enableviewstate = true

Here is the code, that I have within the Itemlist_ItemCommand sub procedure:

**************
If e.CommandName = "AddToCart" Then

   Session("item_id") = CType(e.Item.FindControl("lbl_item_id"), Label).Text
   Session("item_desc") = CType(e.Item.FindControl("hlinkitem_title"), HyperLink).Text
   Session("price") = CType(e.Item.FindControl("price"), Label).Text
   Session("quantity") = CType(e.Item.FindControl("lblqty"), TextBox).Text

   If CType(e.Item.FindControl("ddlsizes"), DropDownList).Enabled = True Then
      Session("size_id") = CType(e.Item.FindControl("ddlsizes"), DropDownList).SelectedItem.Value
      Session("size") = CType(e.Item.FindControl("ddlsizes"), DropDownList).SelectedItem.Text
   Else
      Session("size_id") = "0"
      Session("size") = "No Size"
   End If

End If
***************

Here is the trace, of the itemlist:  (The problem controls seem to be the 'databoundliteralcontrols'...  This is where i could use some learning & your help)...

        Itemlist System.Web.UI.WebControls.DataList 5637 76
            Itemlist:_ctl0 System.Web.UI.WebControls.DataListItem 0 0
                Itemlist:_ctl0:_ctl0 System.Web.UI.LiteralControl 0 0
            Itemlist:_ctl1 System.Web.UI.WebControls.DataListItem 1288 0
                Itemlist:_ctl1:_ctl0 System.Web.UI.LiteralControl 39 0
                Itemlist:_ctl1:image System.Web.UI.WebControls.HyperLink 133 116
                Itemlist:_ctl1:_ctl1 System.Web.UI.LiteralControl 15 0
                Itemlist:_ctl1:title1 System.Web.UI.WebControls.Label 58 0
                    Itemlist:_ctl1:_ctl2 System.Web.UI.DataBoundLiteralControl 5 12
                Itemlist:_ctl1:_ctl3 System.Web.UI.LiteralControl 15 0
                Itemlist:_ctl1:hlinkitem_title System.Web.UI.WebControls.HyperLink 106 76
                    Itemlist:_ctl1:_ctl4 System.Web.UI.DataBoundLiteralControl 35 52
                Itemlist:_ctl1:_ctl5 System.Web.UI.LiteralControl 15 0
                Itemlist:_ctl1:price System.Web.UI.WebControls.Label 58 0
                    Itemlist:_ctl1:_ctl6 System.Web.UI.DataBoundLiteralControl 6 16
                Itemlist:_ctl1:_ctl7 System.Web.UI.LiteralControl 23 0
                Itemlist:_ctl1:lblqty System.Web.UI.WebControls.TextBox 136 0
                Itemlist:_ctl1:_ctl8 System.Web.UI.LiteralControl 24 0
                Itemlist:_ctl1:ddlsizes System.Web.UI.WebControls.DropDownList 0 124
                Itemlist:_ctl1:_ctl9 System.Web.UI.LiteralControl 15 0
                Itemlist:_ctl1:btnbuy System.Web.UI.WebControls.Button 260 68
                Itemlist:_ctl1:_ctl10 System.Web.UI.LiteralControl 15 0
                Itemlist:_ctl1:rval_qty System.Web.UI.WebControls.RangeValidator 289 0
                Itemlist:_ctl1:_ctl11 System.Web.UI.LiteralControl 15 0
                Itemlist:_ctl1:lbl_item_id System.Web.UI.WebControls.Label 59 0
                    Itemlist:_ctl1:_ctl12 System.Web.UI.DataBoundLiteralControl 1 8
                Itemlist:_ctl1:_ctl13 System.Web.UI.LiteralControl 13 0
*****************

Here are the session state traces, that I'm failing to retrieve values. (the dropdownlist and textbox return values correctly, but not the labels & hyperlink)

item_id System.String  
item_desc System.String  
price System.String  

*****************

Along with CType, I have also attempted Directcast, with no luck..

Can anyone offer some much needed advice?

I'm sure I'm doing something stupid, but please be gentle :D

..:: Bare ::..



Hey Bare,

Can you show us the HTML code?

--Live Chat


Here is the datalist portion of the html...

<asp:datalist id="Itemlist" runat="server" RepeatDirection="Horizontal" ShowFooter="False" RepeatColumns="3"
       Width="100%" ShowHeader="False">
       <HeaderTemplate>
        Recently Added Items
       </HeaderTemplate>
       <AlternatingItemStyle CssClass="tblinvis"></AlternatingItemStyle>
       <ItemStyle CssClass="tblinvis"></ItemStyle>
       <ItemTemplate>
        <P align="center">
         <asp:HyperLink id=image runat="server" EnableViewState="True" CssClass="btnhyper2" ImageUrl='<%# ConfigurationSettings.AppSettings("lfm_item_image_path")+GetThumbnail(DataBinder.Eval(Container.DataItem, "item_id")) %>' NavigateUrl='<%# "~/items/viewitem.aspx?item="&amp;DataBinder.Eval(Container.DataItem, "item_id") %>'>
         </asp:HyperLink><BR>
         <asp:Label id="title1" runat="server" EnableViewState="True" CssClass="txt1">
          <%# DataBinder.Eval(Container.DataItem, "band_name") %>
         </asp:Label><BR>
         <asp:HyperLink id=hlinkitem_title runat="server" EnableViewState="True" NavigateUrl='<%# "~/items/viewitem.aspx?item="&amp;DataBinder.Eval(Container.DataItem, "item_id") %>'>
          <%# DataBinder.Eval(Container.DataItem, "item_title") %>
         </asp:HyperLink><BR>
         <asp:Label id="price" runat="server" EnableViewState="True" CssClass="txt2">
          <%# "$"+Format(DataBinder.Eval(Container.DataItem, "price"), "##,##0.00") %>
         </asp:Label>&nbsp;&nbsp;
         <asp:TextBox id="lblqty" runat="server" EnableViewState="True" CssClass="txtboxinput" Width="25px"
          Columns="1">1</asp:TextBox>&nbsp; &nbsp;
         <asp:DropDownList id=ddlsizes runat="server" EnableViewState="True" CssClass="dropdown" Width="100px" DataValueField="size_id" DataTextField="size_desc" DataSource='<%# GetSizes(DataBinder.Eval(Container.DataItem, "item_id")) %>' visible="<%# AreThereSizes %>" >
         </asp:DropDownList><BR>
         <asp:Button id=btnbuy runat="server" EnableViewState="True" CssClass="btn2" Width="150px" CommandName="AddToCart" Text='<%# CheckStock(DataBinder.Eval(Container.DataItem, "item_id")) %>' enabled="<%# IsThereStock %>" >
         </asp:Button><BR>
         <asp:RangeValidator id="rval_qty" runat="server" EnableViewState="False" CssClass="val" Display="Dynamic"
          ForeColor=" " MinimumValue="1" MaximumValue="999" ControlToValidate="lblqty" ErrorMessage="RangeValidator">Please enter a valid quantity</asp:RangeValidator><BR>
         <asp:Label id="lbl_item_id" runat="server" EnableViewState="True" CssClass="txt1">
          <%# DataBinder.Eval(Container.DataItem, "item_id") %>
         </asp:Label><BR>
       </ItemTemplate>
       <HeaderStyle CssClass="thead"></HeaderStyle>
      </asp:datalist>

..:: Bare ::..



Visual Studio reformats the html and makes it look really pathetic :(

and I'm pretty new to this... :hehe:

..:: Bare ::..



Hmm... for the life of my I can't see it...

Try removing the spaces in the label and the link.. make em all on one line..

just a thought...

--Live Chat


is price pulling from its label correctly?

--

Edit.. oh never mind. you did say the labels aren't pulling.. duh..

--Live Chat

Edited
15 Years Ago by Cradz

I tried removing spaces and carriage returns, with no success (plus, Visual Studio just puts them right back anyway)....

Ya... I think i've been looking at this too long... arghhh...

On page load all the controls, within the datalist, bind to the correct data...  So, in testing... before retrieving the values of one of the labels...  I programmatically, changed the value of the label..  then when I retrieve the value of said label... it correctly returns the changed value... 

So, it seems for some reason, that even though the label is displaying and is bound to the correct data.. it truly isn't there to be retrieved.. (of course i'm saying this with the limited experience that i have)...

Any other ideas or suggestions would be greatly appreciated.

..:: Bare ::..



I'm a newb with .net too, but from what I understand about asp:Label, you have to set the Text if you want anything to show up.  It seems to me you aren't setting the text properly and that's why its not being pulled in.  Now of course you didn't state that so I don't know for sure. 

You currently have:

<asp:Label id="title1" runat="server" EnableViewState="True" CssClass="txt1"><%# DataBinder.EvalContainer.DataItem, "band_name") %></asp:Label>

I think you might need:

<asp:Label id="title1" runat="server" EnableViewState="True" CssClass="txt1" Text="<%# DataBinder.Eval(Container.DataItem, "band_name") %>"></asp:Label>

This would be similar to how you set the NavigateUrl of the Hyperlink.

As far as the hyperlink, you have:

NavigateUrl='...'

Should that be:

NavigateUrl="..."

Like I said, I'm a newb too, so take it all with a grain of salt.

Jimmy Stuart


heh.. aye... I didn't think that would be it.. but worth a quick check.

Any time I use a label to hold data.. I do set it to begin with so probably why I've never come across this issue...

--Live Chat


Actually Jimmy.. NavigationURL='...' is fine.. you can use single quotes so that you don't have to escape any double quotes inside..

but I think you're right about the text="" part... :)

--Live Chat

Edited
15 Years Ago by Cradz
GO

Login
Existing Account
Email Address:


Password:


Social Logins

Select a Forum....







InstantASP Forums


Search