Link to home
Start Free TrialLog in
Avatar of REA_ANDREW
REA_ANDREWFlag for United Kingdom of Great Britain and Northern Ireland

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_product.jpg" alt='<%#Eval("prod_name") %>'/>
            <%
            }
%>

Thanks in advance

Andrew
Avatar of valkyrie_nc
valkyrie_nc
Flag of United States of America image

Is there a reason you can't use an <asp:Image> tag and do the processing in the code-behind?

What error message are you getting from this code?



valkyrie_nc
Avatar of REA_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
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
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_product.jpg" alt='<%#Eval("prod_name") %>'/>
            <%
            }
            %>
                </div>
                <div class="ProductTitle">
                    <img src="images/Small_Black_Arrow.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?prod_id=<%#Eval("prod_id") %>" class="more"><img src="images/More_Button.jpg" alt="More Details About <%#Eval("prod_name") %>"</a>
                </div>
           
            </div>
    </ItemTemplate>
    </asp:Repeater>
How are you binding data to the repeater?  Can you post that code?
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["LoadSql"].ToString(), 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(ConfigurationManager.ConnectionStrings["testConn"].ConnectionString);
        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["PageSizeVar"].ToString());

        // 4. Set the Current Page
        //if(Convert.ToInt32(Session["CurPageVar"].ToString()) == 0)
        //{
        //Session["CurPageVar"] = 1;
        //            objPds.CurrentPageIndex = Convert.ToInt32(Session["CurPageVar"].ToString());
        //}
        //else{
        objPds.CurrentPageIndex = Convert.ToInt32(Session["CurPageVar"].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.FindByText("6").Selected = true;
            Session["PageSizeVar"] = 6;
        }

        if (Convert.ToInt32(Session["CurrentPage"].ToString()) == (Convert.ToInt32(Session["PageCount"].ToString()) - 1))
        {
            lnkNext.Visible = false;
            lnkNext2.Visible = false;
        }
        else
        {
            lnkNext.Visible = true;
            lnkNext2.Visible = true;
        }
        if (Session["CurrentPage"].ToString() == "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"].ToString()) + 1) + " of " + Session["PageCount"].ToString();
            lblCurrentPage2.Text = "You are viewing Page " + (Convert.ToInt32(Session["CurPageVar"].ToString()) + 1) + " of " + Session["PageCount"].ToString();
        }
        else
        {
            SearchLabel.Text = "The are currently no products listed";
        }
    }

    protected void NextPage(Object Sender, EventArgs e)
    {
        if (Convert.ToInt32(Session["CurrentPage"].ToString()) != Convert.ToInt32(Session["PageCount"].ToString()))
        {
            int value1 = Convert.ToInt32(Session["CurPageVar"].ToString());
            Session["CurPageVar"] = value1 + 1;
        }
        LoadPageData(Session["LoadSql"].ToString(), Repeater1);
    }
    protected void PrevPage(Object Sender, EventArgs e)
    {
        if (Session["CurrentPage"].ToString() != "0")
        {
            int value2 = Convert.ToInt32(Session["CurPageVar"].ToString());
            Session["CurPageVar"] = value2 - 1;
        }
        LoadPageData(Session["LoadSql"].ToString(), Repeater1);
    }
    protected void ChangePageSize(Object Sender, EventArgs e)
    {
      Session["CurPageVar"] = 0;
        Session["PageSizeVar"] = Convert.ToInt32(DropDownList1.SelectedItem.Text);
        LoadPageData(Session["LoadSql"].ToString(), Repeater1);
    }

}
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/Medium_<%#Eval("prod_id")%>.jpg\' alt=\'<%#Eval("prod_name") %>\' />');
   }
   else
   {
      document.write('<img src="product_images/test_product.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
ASKER CERTIFIED SOLUTION
Avatar of valkyrie_nc
valkyrie_nc
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
you are a star

thanks very much. All the posting totally worth it!!! lol cheers again.

Andy