asp:label in asp:datalist - Setting Text Value Does not work!

H

Harry

Hi,

Can anyone tell me why the below code does not work.
I dont get any errors, but nor does the Text for the asp:label get
set.

Thanks in advance as always
H



protected void Page_Load(object source,
System.Web.UI.WebControls.DataListCommandEventArgs e)
{
if (Request.QueryString["Range"] == "Plat_Price"){
string PlatLabel = "Platino";
Label hypSelectC = (Label)e.Item.FindControl("hypSelectC");
hypSelectC.Text = "PlatLabel";
}
}


And in the html I have the following label, which is contained within
a Datalist.

<ASP:DataList ID="dlListCases"
RepeatColumns="2"
RepeatDirection="Horizontal"
RepeatLayout="Table" ItemStyle-CssClass="CaseRightBorder"
runat="server">
<ItemTemplate>
<table border="0" cellpadding="0" cellspacing="0"
class="CaseTable">
<tr>
<td colspan="2" class="CaseTableHeader"><%#
DataBinder.Eval(Container.DataItem, "CaseName") %>
</td>
</tr>
<tr>
<td width="75" valign="middle">
<asp:image runat="server" AlternateText="Thumbnail" ImageUrl='<%#
DataBinder.Eval(Container.DataItem, "image_thumb") %>'/>
<br>
</td>
<td width="134" valign="top"> <%#
DataBinder.Eval(Container.DataItem,
"Bullet_Points").ToString().Replace("\n","<br>") %>
<br>
<b>Price:
<%# DataBinder.Eval(Container.DataItem, "PriceCol", "{0:c}") %>
<asp:label CssClass="BoldLabel" ID="hypSelectC"
runat="server"></asp:label>
</b>
</td>
</tr>
</table>
</ItemTemplate>
</ASP:DataList>
 
M

Matt Berther

Hello Harry,
Can anyone tell me why the below code does not work.
I dont get any errors, but nor does the Text for the asp:label get
set.
Thanks in advance as always
H
protected void Page_Load(object source,
System.Web.UI.WebControls.DataListCommandEventArgs e)
{
if (Request.QueryString["Range"] == "Plat_Price"){
string PlatLabel = "Platino";
Label hypSelectC = (Label)e.Item.FindControl("hypSelectC");
hypSelectC.Text = "PlatLabel";
}
}

This is beside the point, but would help in troubleshooting your problem. Your method name in the above example is Page_Load, but the method has a signature indicative of a datalist command method. At what point are you attempting to refresh the label (ie: EditCommand, UpdateCommand, etc).
 
H

Harry Singh

Hi Matt,

I actually have two Page_Load functions.
(I am still getting my head around this bit!)

The First:
protected void Page_Load(Object Src, EventArgs E)
{
Lots of code here
}

I tried putting the code in this block, but it kept on chucking out
errors. (Error: CS0117 System.EventArgs does not contain a definition
for 'item')

I couldnt work out how to referance a asp:lable that is located in a
datalist on the intial page load.

Am i doing this completely the wrong way?

Any light you can shed on this would be appriciated.

Thanks
H
 
M

Matt Berther

Hello Harry,
I couldnt work out how to referance a asp:lable that is located in a
datalist on the intial page load.

First of all, in your Page_Load, you'll want to do something like this:

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
// hook up your datalist's datasource and bind it
datalist.DataSource = whatever;
datalist.DataBind();
}

// The rest of your code
}

and then handle the datalist's ItemDataBound event via something like this:

protected void DataList_ItemDataBound(object sender, DataListItemEventArgs e)
{
// this is where you would put the stuff for populating the label
}

Make sure that you've hooked up the event handler through the designer (via the properties window for the DataList) or programmatically in the code.
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top