在访问HTML元素的foreach变量,RUNAT ="服务器" [英] Accessing foreach variable in HTML element with runat="server"

查看:161
本文介绍了在访问HTML元素的foreach变量,RUNAT ="服务器"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户控件具有对包含照片产品列表,列表中的属性。在的.ascx 我有一个 DIV 作为容器的所有照片。这种工作方式是正确的了。

 <%的foreach(在产品增值版产品){%GT;
    < D​​IV CLASS =产品>
        < D​​IV CLASS =产品照片>
            <%的foreach(VAR照片product.Photos){%GT;
                &所述; IMG SRC =&其中;%= photo.Url%>中ALT =<%= product.Name%>的照片/>
            <%}%GT;
        < / DIV>
        <! - 其他产品标记/这里的数据...  - >
    < / DIV>
<%}%GT;
 

我现在想隐藏 DIV 如果没有照片。我的想法是,使以产品为照片 DIV 有一个 =服务器,并给它一个可见属性。这并不工作,因为它解决不了我的产品变量,来自于我的的foreach 循环。看来, =服务器的foreach 循环无法访问,这样就没有意义了,因为让变量循环也必须在服务器上运行。

问:
我如何可以访问一个的foreach 循环定义内的变量。如* X 文件时,该元素有一个 =服务器?这甚至可能吗?如果没有,什么是完成我想要的东西的另一种方式?

ProductsControl.ascx.cs

 公共部分类ProductsControl:用户控件
{
    公众的IList<产品>产品{获得;组; }
}
 

Product.cs

 公共类产品
{
    公众的IList<图片>照片{获得;组; }
    //这里的其他特性...
}
 

解决方案

为什么不检查照片的伯爵在创建分区前?

 <%的foreach(在产品增值版产品){%GT;
    < D​​IV CLASS =产品>
        &其中;如果%(product.Photos.Count大于0){%GT;
            < D​​IV CLASS =产品照片>
                <%的foreach(VAR照片product.Photos){%GT;
                &所述; IMG SRC =&其中;%= photo.Url%>中ALT =<%= product.Name%>的照片/>
                <%}%GT;
            < / DIV>
        <%}%GT;
        <! - 其他产品标记/这里的数据...  - >
    < / DIV>
<%}%GT;
 

I have a UserControl which has a Property for a list of products which contain a list of photos. In the .ascx I have a DIV as a container for all the photos. This works as is right now.

<% foreach (var product in Products) { %>
    <div class="product">
        <div class="product-photos">
            <% foreach (var photo in product.Photos) { %>
                <img src="<%= photo.Url %>" alt="<%= product.Name %> Photo"/>
            <% } %>
        </div>
        <!-- Other product markup/data here... -->
    </div>
<% } %>

I am now trying to hide the DIV if there are no photos. My idea was to make the "product-photos" DIV have a runat="server" and give it a Visible attribute. This doesn't work because it can't resolve my product variable which comes from my foreach loop. It appears that the runat="server" makes the variables from the foreach loops inaccessible which doesn't make sense because the loop must also be running on the server.

Question:
How can I access variables defined in a foreach loop inside an .as*x file when the element has a runat="server"? Is this even possible? If not, what is an alternative way of accomplishing what I want?

ProductsControl.ascx.cs

public partial class ProductsControl: UserControl
{
    public IList<Product> Products { get; set; }
}

Product.cs

public class Product
{
    public IList<Photo> Photos { get; set; }
    // Other properties here...
}

解决方案

Why not check the Count of the Photos before you create the div?

<% foreach (var product in Products) { %>
    <div class="product">
        <% if (product.Photos.Count > 0) { %>
            <div class="product-photos">
                <% foreach (var photo in product.Photos) { %>
                <img src="<%= photo.Url %>" alt="<%= product.Name %> Photo"/>
                <% } %>
            </div>
        <% } %> 
        <!-- Other product markup/data here... -->
    </div>
<% } %> 

这篇关于在访问HTML元素的foreach变量,RUNAT =&QUOT;服务器&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆