Hello,
I'm new to asp.net programming and need some help. I have a repeator control that I need to change dynamically based on the sql data returned. I'm using a 2008 sql express Entity Data Source to populate the control.
I have two fields in my database, "UnitCost" and "SalePrice", and I want to change this code:
Text='<%#
Eval("UnitCost", "{0:C}") %>'Font-Strikeout="False"/>
and
Text='<%#
Eval("SalePrice", "{0:C}") %>'ForeColor="#FF3300"Visible="False"/>
to
Protected Sub repPrices_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles repPrices.ItemDataBound
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
Dim SalePriceFromDB as int = e.Item.DataItem("SalePrice").ToString
Dim lblUnitcost As Label = Nothing
Dim SalePrice As Label = Nothing
lblUnitcost = CType(e.Item.FindControl("lblUnitcost "), Label)
SalePrice = CType(e.Item.FindControl("SalePrice "), Label)
if SalePriceFromDB > 0 then
lblUnitcost.font.strikethrough = true
lblUnitcost.Visible= True
else
lblUnitcost.font.strikethrough = false
lblUnitcost.Visible= false
end if
end if
end sub
You may need to use a vb to C# translator to get what you need exatcly. Also, I just wrote this freehand so I may havea syntax error or two, but the point was just to show you what I was thinking.
I hope this helps.
Visit my asp.Net site at http://everymanprogrammer.com and while you're there, visit a Sponsor, it helps me pay my bills.
***
Mark the replies as Answers if they answered your question.
Description:
An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS0246: The type or namespace name 'int' could not be found (are you missing a using directive or an assembly reference?)
Source Error:
Line 18: if (e.Item.ItemType == ListItemType.Item | e.Item.ItemType == ListItemType.AlternatingItem)
Line 19: {
Line 20: @int SalePriceFromDB = e.Item.DataItem("SalePrice").ToString; Line 21: Label lblUnitcost = null;
Line 22: Label SalePrice = null;
Superguppie.
Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
When all you've got is a Hammer, Every Problem looks like a Nail. Michael Swain.
JimKoh
0 Points
12 Posts
How to check data value for repeator control?
Aug 03, 2012 05:29 PM|LINK
Hello,
I'm new to asp.net programming and need some help. I have a repeator control that I need to change dynamically based on the sql data returned. I'm using a 2008 sql express Entity Data Source to populate the control.
I have two fields in my database, "UnitCost" and "SalePrice", and I want to change this code:
Text='<%# Eval("UnitCost", "{0:C}") %>'Font-Strikeout="False" /> and
Text='<%# Eval("SalePrice", "{0:C}") %>'ForeColor="#FF3300" Visible="False" /> to
Text='<%# Eval("UnitCost", "{0:C}") %>'Font-Strikeout="True" />
Text='<%# Eval("SalePrice", "{0:C}") %>'ForeColor="#FF3300" Visible="True" /> if SalePrice >0.
I'm using C# and need an example of the best way to do this.
Thanks
Jim
Tim Cadieux
Participant
966 Points
317 Posts
Re: How to check data value for repeator control?
Aug 03, 2012 11:57 PM|LINK
This might be easier to do in the Item_DataBound event in the code-behind.
***
Mark the replies as Answers if they answered your question.
JimKoh
0 Points
12 Posts
Re: How to check data value for repeator control?
Aug 04, 2012 02:46 PM|LINK
Can you post an example?
Tim Cadieux
Participant
966 Points
317 Posts
Re: How to check data value for repeator control?
Aug 04, 2012 03:11 PM|LINK
Protected Sub repPrices_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles repPrices.ItemDataBound If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then Dim SalePriceFromDB as int = e.Item.DataItem("SalePrice").ToString Dim lblUnitcost As Label = Nothing Dim SalePrice As Label = Nothing lblUnitcost = CType(e.Item.FindControl("lblUnitcost "), Label) SalePrice = CType(e.Item.FindControl("SalePrice "), Label) if SalePriceFromDB > 0 then lblUnitcost.font.strikethrough = true lblUnitcost.Visible= True else lblUnitcost.font.strikethrough = false lblUnitcost.Visible= false end if end if end subYou may need to use a vb to C# translator to get what you need exatcly. Also, I just wrote this freehand so I may havea syntax error or two, but the point was just to show you what I was thinking.
I hope this helps.
***
Mark the replies as Answers if they answered your question.
JimKoh
0 Points
12 Posts
Re: How to check data value for repeator control?
Aug 05, 2012 03:51 PM|LINK
This is what I have in the code behind after using a converter:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Products : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void repPrices_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item | e.Item.ItemType == ListItemType.AlternatingItem)
{
@int SalePriceFromDB = e.Item.DataItem("SalePrice").ToString;
Label lblUnitcost = null;
Label SalePrice = null;
lblUnitcost = (Label)e.Item.FindControl("lblUnitcost ");
SalePrice = (Label)e.Item.FindControl("SalePrice ");
if (SalePriceFromDB > 0)
{
lblUnitcost.font.strikethrough = true;
lblUnitcost.Visible = true;
}
else
{
lblUnitcost.font.strikethrough = false;
lblUnitcost.Visible = false;
}
}
}
}
And this is the error I'm getting
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS0246: The type or namespace name 'int' could not be found (are you missing a using directive or an assembly reference?)
Source Error:
Line 18: if (e.Item.ItemType == ListItemType.Item | e.Item.ItemType == ListItemType.AlternatingItem) Line 19: { Line 20: @int SalePriceFromDB = e.Item.DataItem("SalePrice").ToString; Line 21: Label lblUnitcost = null; Line 22: Label SalePrice = null;Tim Cadieux
Participant
966 Points
317 Posts
Re: How to check data value for repeator control?
Aug 06, 2012 12:52 AM|LINK
I'm not a C# guy but it looks to me like it doesn't like your SalePriceFromDB as Int declaration?
***
Mark the replies as Answers if they answered your question.
superguppie
All-Star
48225 Points
8679 Posts
Re: How to check data value for repeator control?
Aug 06, 2012 03:30 PM|LINK
Looking at your original code. You can use
Visible='<%# (decimal)Eval("SalesPrice") > 100 %>'Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
When all you've got is a Hammer, Every Problem looks like a Nail. Michael Swain.
JimKoh
0 Points
12 Posts
Re: How to check data value for repeator control?
Aug 06, 2012 04:04 PM|LINK
If I use that line of code, the SalePrice field shows up for all products. I need it to show up "only" if the SalePrice is greater than zero.
Thanks
JimKoh
0 Points
12 Posts
Re: How to check data value for repeator control?
Aug 06, 2012 05:56 PM|LINK
I got it. I had to change the code to read:
Visible='<%# (decimal)Eval("SalePrice") > 0 %>'
Thanks again