REA_ANDREW
asked on
Inline Code Problem
Hi,
I am having trouble writing code inline in my aspx page. Basically I am trying to say that if a datitem = "CONFIRMED" do this else do that. Here is what I have, but which does not work
<%
if (Eval("prod_image") =="CONFIRMED") //<----------------------- --It does not like the eval.
{
%>
<img src="product_images/Medium _<%#Eval(" prod_id")% >.jpg" alt='<%#Eval("prod_name") %>' />
<%
}
else
{
%>
<img src="product_images/test_p roduct.jpg " alt='<%#Eval("prod_name") %>'/>
<%
}
%>
Thanks in advance
Andrew
I am having trouble writing code inline in my aspx page. Basically I am trying to say that if a datitem = "CONFIRMED" do this else do that. Here is what I have, but which does not work
<%
if (Eval("prod_image") =="CONFIRMED") //<-----------------------
{
%>
<img src="product_images/Medium
<%
}
else
{
%>
<img src="product_images/test_p
<%
}
%>
Thanks in advance
Andrew
ASKER
well it populates the repeater, and it says we cannot use the eval function in that respect. How would we do this using the asp image
Thanks
Andy
Thanks
Andy
Oh wait , I think I see what's going on. You're binding data to the repeater, right? If so, you need to use the DataBinder object instead of Eval to assign values:
<% DataBinder.Eval(Container. DataItem, "prod_image") %> instead of <%#Eval("prod_image")%>
You can also use your ItemDataBound event to assign the appropriate values & such to the controls in your Repeater.
Let me know if I just went way off into left field.
hth
valkyrie_nc
<% DataBinder.Eval(Container.
You can also use your ItemDataBound event to assign the appropriate values & such to the controls in your Repeater.
Let me know if I just went way off into left field.
hth
valkyrie_nc
ASKER
no no you are right. But I need the code inline, or taught how to in code behind. Here is the lat error I got with your code
Compiler Error Message: CS0103: The name 'Container' does not exist in the current context
Source Error:
Line 25:
Line 26: <%
Line 27: if (DataBinder.Eval(Container .DataItem, "prod_image") =="CONFIRMED")
Line 28: {
Line 29: %>
here is my repeater.
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<div class="ProductBox">
<div class="Prod_Image">
<%
if (DataBinder.Eval(Container .DataItem, "prod_image") =="CONFIRMED")
{
%>
<img src="product_images/Medium _<%#Eval(" prod_id")% >.jpg" alt='<%#Eval("prod_name") %>' />
<%
}
else
{
%>
<img src="product_images/test_p roduct.jpg " alt='<%#Eval("prod_name") %>'/>
<%
}
%>
</div>
<div class="ProductTitle">
<img src="images/Small_Black_Ar row.jpg" alt="Tiny Test Product" align="absmiddle" /> <%#Eval("prod_name") %>
</div>
<div class="ProductPrice">
<%#String.Format("{0:£ ##.00}",Eval("unit_price") ) %>
</div>
<div class="more">
<a href="ProductDetail.aspx?p rod_id=<%# Eval("prod _id") %>" class="more"><img src="images/More_Button.jp g" alt="More Details About <%#Eval("prod_name") %>"</a>
</div>
</div>
</ItemTemplate>
</asp:Repeater>
Compiler Error Message: CS0103: The name 'Container' does not exist in the current context
Source Error:
Line 25:
Line 26: <%
Line 27: if (DataBinder.Eval(Container
Line 28: {
Line 29: %>
here is my repeater.
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<div class="ProductBox">
<div class="Prod_Image">
<%
if (DataBinder.Eval(Container
{
%>
<img src="product_images/Medium
<%
}
else
{
%>
<img src="product_images/test_p
<%
}
%>
</div>
<div class="ProductTitle">
<img src="images/Small_Black_Ar
</div>
<div class="ProductPrice">
<%#String.Format("{0:£ ##.00}",Eval("unit_price")
</div>
<div class="more">
<a href="ProductDetail.aspx?p
</div>
</div>
</ItemTemplate>
</asp:Repeater>
How are you binding data to the repeater? Can you post that code?
ASKER
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls. WebParts;
using System.Web.UI.HtmlControls ;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
public PagedDataSource objPds = new PagedDataSource();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["CurPageVar"] = 0;
Session["PageSizeVar"] = 6;
Session["LoadSql"] = "select * from tbl_products where cat_id = " + Request.QueryString["catid "] ;
LoadPageData(Session["Load Sql"].ToSt ring(), Repeater1);
}
}
protected void LoadPageData(string SqlString, Repeater R)
{
//John, I have to comment this to keep me sane haha
// 1. Get the COnnection
SqlConnection objConn = new SqlConnection(Configuratio nManager.C onnectionS trings["te stConn"].C onnectionS tring);
SqlDataAdapter objCommand = new SqlDataAdapter(SqlString, objConn);
DataSet ds = new DataSet();
objCommand.Fill(ds);
// 2. Set up the paging Object
objPds.DataSource = ds.Tables[0].DefaultView;
objPds.AllowPaging = true;
// 3. Set the Page Size.
objPds.PageSize = Convert.ToInt32(Session["P ageSizeVar "].ToStrin g());
// 4. Set the Current Page
//if(Convert.ToInt32(Sessi on["CurPag eVar"].ToS tring()) == 0)
//{
//Session["CurPageVar"] = 1;
// objPds.CurrentPageIndex = Convert.ToInt32(Session["C urPageVar" ].ToString ());
//}
//else{
objPds.CurrentPageIndex = Convert.ToInt32(Session["C urPageVar" ].ToString ());
//}
// 5. Bind the Data and display the results
R.DataSource = objPds;
R.DataBind();
Session["CurrentPage"] = objPds.CurrentPageIndex;
Session["PageCount"] = objPds.PageCount;
if (Session["CurrentPage"] == null)
{
Session["CurrentPage"] = 0;
DropDownList1.SelectedItem .Selected = false;
DropDownList1.Items.FindBy Text("6"). Selected = true;
Session["PageSizeVar"] = 6;
}
if (Convert.ToInt32(Session[" CurrentPag e"].ToStri ng()) == (Convert.ToInt32(Session[" PageCount" ].ToString ()) - 1))
{
lnkNext.Visible = false;
lnkNext2.Visible = false;
}
else
{
lnkNext.Visible = true;
lnkNext2.Visible = true;
}
if (Session["CurrentPage"].To String() == "0")
{
lnkPrev.Visible = false;
lnkPrev2.Visible = false;
}
else
{
lnkPrev.Visible = true;
lnkPrev2.Visible = true;
}
if (objPds.DataSourceCount > 0)
{
lblCurrentPage.Text = "You are viewing Page " + (Convert.ToInt32(Session[" CurPageVar "].ToStrin g()) + 1) + " of " + Session["PageCount"].ToStr ing();
lblCurrentPage2.Text = "You are viewing Page " + (Convert.ToInt32(Session[" CurPageVar "].ToStrin g()) + 1) + " of " + Session["PageCount"].ToStr ing();
}
else
{
SearchLabel.Text = "The are currently no products listed";
}
}
protected void NextPage(Object Sender, EventArgs e)
{
if (Convert.ToInt32(Session[" CurrentPag e"].ToStri ng()) != Convert.ToInt32(Session["P ageCount"] .ToString( )))
{
int value1 = Convert.ToInt32(Session["C urPageVar" ].ToString ());
Session["CurPageVar"] = value1 + 1;
}
LoadPageData(Session["Load Sql"].ToSt ring(), Repeater1);
}
protected void PrevPage(Object Sender, EventArgs e)
{
if (Session["CurrentPage"].To String() != "0")
{
int value2 = Convert.ToInt32(Session["C urPageVar" ].ToString ());
Session["CurPageVar"] = value2 - 1;
}
LoadPageData(Session["Load Sql"].ToSt ring(), Repeater1);
}
protected void ChangePageSize(Object Sender, EventArgs e)
{
Session["CurPageVar"] = 0;
Session["PageSizeVar"] = Convert.ToInt32(DropDownLi st1.Select edItem.Tex t);
LoadPageData(Session["Load Sql"].ToSt ring(), Repeater1);
}
}
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.
using System.Web.UI.HtmlControls
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
public PagedDataSource objPds = new PagedDataSource();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["CurPageVar"] = 0;
Session["PageSizeVar"] = 6;
Session["LoadSql"] = "select * from tbl_products where cat_id = " + Request.QueryString["catid
LoadPageData(Session["Load
}
}
protected void LoadPageData(string SqlString, Repeater R)
{
//John, I have to comment this to keep me sane haha
// 1. Get the COnnection
SqlConnection objConn = new SqlConnection(Configuratio
SqlDataAdapter objCommand = new SqlDataAdapter(SqlString, objConn);
DataSet ds = new DataSet();
objCommand.Fill(ds);
// 2. Set up the paging Object
objPds.DataSource = ds.Tables[0].DefaultView;
objPds.AllowPaging = true;
// 3. Set the Page Size.
objPds.PageSize = Convert.ToInt32(Session["P
// 4. Set the Current Page
//if(Convert.ToInt32(Sessi
//{
//Session["CurPageVar"] = 1;
// objPds.CurrentPageIndex = Convert.ToInt32(Session["C
//}
//else{
objPds.CurrentPageIndex = Convert.ToInt32(Session["C
//}
// 5. Bind the Data and display the results
R.DataSource = objPds;
R.DataBind();
Session["CurrentPage"] = objPds.CurrentPageIndex;
Session["PageCount"] = objPds.PageCount;
if (Session["CurrentPage"] == null)
{
Session["CurrentPage"] = 0;
DropDownList1.SelectedItem
DropDownList1.Items.FindBy
Session["PageSizeVar"] = 6;
}
if (Convert.ToInt32(Session["
{
lnkNext.Visible = false;
lnkNext2.Visible = false;
}
else
{
lnkNext.Visible = true;
lnkNext2.Visible = true;
}
if (Session["CurrentPage"].To
{
lnkPrev.Visible = false;
lnkPrev2.Visible = false;
}
else
{
lnkPrev.Visible = true;
lnkPrev2.Visible = true;
}
if (objPds.DataSourceCount > 0)
{
lblCurrentPage.Text = "You are viewing Page " + (Convert.ToInt32(Session["
lblCurrentPage2.Text = "You are viewing Page " + (Convert.ToInt32(Session["
}
else
{
SearchLabel.Text = "The are currently no products listed";
}
}
protected void NextPage(Object Sender, EventArgs e)
{
if (Convert.ToInt32(Session["
{
int value1 = Convert.ToInt32(Session["C
Session["CurPageVar"] = value1 + 1;
}
LoadPageData(Session["Load
}
protected void PrevPage(Object Sender, EventArgs e)
{
if (Session["CurrentPage"].To
{
int value2 = Convert.ToInt32(Session["C
Session["CurPageVar"] = value2 - 1;
}
LoadPageData(Session["Load
}
protected void ChangePageSize(Object Sender, EventArgs e)
{
Session["CurPageVar"] = 0;
Session["PageSizeVar"] = Convert.ToInt32(DropDownLi
LoadPageData(Session["Load
}
}
ASKER
all i want to say is if prod_name for each data item is "CONFIRMED" then show an image else show a default image
Okay, the DataItem.Container broke because you're not using named columns in your querystring but are just pulling them all; that threw me. I looked back over everything and think I see a quick 'n' dirty way to fix the problem.
<script language='javascript' type='text/javascript'>
if (<%#Eval("prod_image")%> == "CONFIRMED")
{
document.write('<img src=\'product_images/Mediu m_<%#Eval( "prod_id") %>.jpg\' alt=\'<%#Eval("prod_name") %>\' />');
}
else
{
document.write('<img src="product_images/test_p roduct.jpg " alt=\'<%#Eval("prod_name") %>\'/>');
}
</script>
Sorry to make you post so much; guess it took the ol' gray matter a while to clear up. :)
hth
valkyrie_nc
<script language='javascript' type='text/javascript'>
if (<%#Eval("prod_image")%> == "CONFIRMED")
{
document.write('<img src=\'product_images/Mediu
}
else
{
document.write('<img src="product_images/test_p
}
</script>
Sorry to make you post so much; guess it took the ol' gray matter a while to clear up. :)
hth
valkyrie_nc
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
you are a star
thanks very much. All the posting totally worth it!!! lol cheers again.
Andy
thanks very much. All the posting totally worth it!!! lol cheers again.
Andy
What error message are you getting from this code?
valkyrie_nc