如何在代码隐藏中调用Eval?

发布于 2024-08-11 12:39:55 字数 557 浏览 1 评论 0原文

我到底如何从代码隐藏中调用 Eval() ?我知道这是 DataBinder.Eval(Container.DataItem, "name") 之类的前端快捷方式,但我无法编译这个该死的东西。我认为我缺少正确的方法参数。我想把这样的东西:

<asp:Image ID="imgLogo" runat="server" ImageUrl='<%# Eval("name") %>'/>

变成:

<asp:Image ID="imgLogo" runat="server" ImageUrl='<%# GetImagePath(???) %>'/>

w/代码隐藏:

protected string GetImagePath(????)
{
    //some code
    return "some/logical/path" + Eval("name");
}

asp:Image 控件位于绑定到 SqlDataReader 的中继器中。

How the heck do I call Eval() from codebehind? I know this is a front-end shortcut for something like DataBinder.Eval(Container.DataItem, "name"), but I cannot get this damn thing to compile. I am missing the proper method parameters, I think. I want to turn something like this:

<asp:Image ID="imgLogo" runat="server" ImageUrl='<%# Eval("name") %>'/>

into:

<asp:Image ID="imgLogo" runat="server" ImageUrl='<%# GetImagePath(???) %>'/>

w/ code-behind:

protected string GetImagePath(????)
{
    //some code
    return "some/logical/path" + Eval("name");
}

The asp:Image control is in a repeater that is bound to a SqlDataReader.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

美人迟暮 2024-08-18 12:39:55

EvalPage 上的一个方法(实际上是在 TemplateControl 上,这意味着它也存在于 UserControl 上)。因此,您不需要传递任何特殊参数:

protected string GetImagePath()
{
    //some code
    return "some/logical/path" + Eval("name");
}

注意:在数据绑定时只能调用 Eval,但 <%# Eval(...) %> 的情况隐式如此; 也是如此,所以这不会造成问题。

如果您想要绑定到的实际对象,请使用 GetDataItem(),因为 Eval(x) 基本上意味着 DataBinder.Eval(GetDataItem(), x )

Eval is a method on Page (on TemplateControl actually, which means that it's also present on UserControl). So you don't need to pass any special parameters:

protected string GetImagePath()
{
    //some code
    return "some/logical/path" + Eval("name");
}

Caution: you can only call Eval while databinding, but that's implicitly the case for <%# Eval(...) %> too, so that shouldn't pose a problem.

If you want the actual object you're binding to, use GetDataItem(), as Eval(x) basically means DataBinder.Eval(GetDataItem(), x).

抹茶夏天i‖ 2024-08-18 12:39:55

你有没有尝试过类似的事情:

<asp:Image ID="imgLogo" runat="server' ImageUrl='<%# GetImagePath( Convert.ToString( Eval("name") ) ) %>' />

protected string GetImagePath( string image )
{
    return "some/logical/path/" + image;
}

Have you tried something like:

<asp:Image ID="imgLogo" runat="server' ImageUrl='<%# GetImagePath( Convert.ToString( Eval("name") ) ) %>' />

and

protected string GetImagePath( string image )
{
    return "some/logical/path/" + image;
}
难得心□动 2024-08-18 12:39:55
protected void rProducts_ItemDataBound(object sender, RepeaterItemEventArgs e) {
    if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item) {

        Image ProductImage = (Image)e.Item.FindControl("ProductImage");
        ProductImage.ImageUrl = DataBinder.Eval(e.Item.DataItem, "ProductImageUrl");

        // Or strongly typed
        Product product = (Product)e.Item.DataItem;
        ProductImage.ImageUrl = product.ProductImageUrl;
    }
}
protected void rProducts_ItemDataBound(object sender, RepeaterItemEventArgs e) {
    if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item) {

        Image ProductImage = (Image)e.Item.FindControl("ProductImage");
        ProductImage.ImageUrl = DataBinder.Eval(e.Item.DataItem, "ProductImageUrl");

        // Or strongly typed
        Product product = (Product)e.Item.DataItem;
        ProductImage.ImageUrl = product.ProductImageUrl;
    }
}
有深☉意 2024-08-18 12:39:55

另一种简单的方法是使用采用格式的 Eval 重载,aspx 变为:

<asp:Image ID="imgLogo" runat="server" ImageUrl='<%# Eval("name", "some/logical/path/{0}") %>'/>

Another simple way to do this is to use the overload of Eval that takes a format, the aspx becomes:

<asp:Image ID="imgLogo" runat="server" ImageUrl='<%# Eval("name", "some/logical/path/{0}") %>'/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文