?
Solved

Inline Code Problem

Posted on 2006-05-09
10
Medium Priority
?
336 Views
Last Modified: 2012-08-13
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
0
Comment
Question by:REA_ANDREW
  • 5
  • 5
10 Comments
 
LVL 9

Expert Comment

by:valkyrie_nc
ID: 16641713
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
0
 
LVL 20

Author Comment

by:REA_ANDREW
ID: 16641949
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
0
 
LVL 9

Expert Comment

by:valkyrie_nc
ID: 16642045
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
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 20

Author Comment

by:REA_ANDREW
ID: 16642171
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>
0
 
LVL 9

Expert Comment

by:valkyrie_nc
ID: 16642207
How are you binding data to the repeater?  Can you post that code?
0
 
LVL 20

Author Comment

by:REA_ANDREW
ID: 16642255
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);
    }

}
0
 
LVL 20

Author Comment

by:REA_ANDREW
ID: 16642370
all i want to say is if prod_name for each data item is "CONFIRMED" then show an image else show a default image
0
 
LVL 9

Expert Comment

by:valkyrie_nc
ID: 16642473
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
0
 
LVL 9

Accepted Solution

by:
valkyrie_nc earned 2000 total points
ID: 16642479
Oops, need one more set of quotes:

if ('<%#Eval("prod_image")%>' == "CONFIRMED")
...



valkyrie_nc
0
 
LVL 20

Author Comment

by:REA_ANDREW
ID: 16642534
you are a star

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

Andy
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question