【问题标题】:binding int values from server side - asp.net从服务器端绑定 int 值 - asp.net
【发布时间】:2010-02-13 22:06:52
【问题描述】:

在这里快速提问。我确定它是可能的,只是无法让它工作。 我有一个网格视图。在有一个gridview。 gridview 绑定到我的自定义类的列表。该类公开了指向图像的链接以及图像的高度和宽度。我在 gridview 中有一个图像控件。我已将 Image Url 绑定到正确的属性。现在,我也想绑定高度和宽度属性。但每次我这样做时,我都会收到以下错误:

无法创建类型的对象 'System.Web.UI.WebControls.Unit' 来自 它的字符串表示 '"

这是一个标签的例子:

<asp:Image runat="server" ID="imgProduct" ImageUrl='<%#Bind("ImageUrl")%>' 
     Height="<%#Bind("GetImageHeight()")%>" Width="<%#Bind("GetImageWidth()")%>">
</asp:Image>

那么,简而言之……我如何绑定不是字符串的属性?

GetImageHeight 和 GetImageWidth 方法示例:

   public Unit GetImageHeight()
    {
        Unit u = new Unit(Convert.ToString(ImageHeight) + "px");
        return u;
    }

    public Unit GetImageWidth()
    {
        Unit u = new Unit(Convert.ToString(ImageWidth) + "px");
        return u;
    }

【问题讨论】:

  • 您的绑定似乎格式不正确。检查我编辑的答案

标签: asp.net data-binding gridview


【解决方案1】:

DOH!我第一次错过了..尝试从绑定中删除引号

 Height="<%#Bind("GetImageHeight()")%>"

应该看起来像

 Height="<%#Bind(GetImageHeight())%>"

并返回一个 int 应该可以正常工作。方法中的所有繁琐工作都不需要。

我可能是错的......

【讨论】:

    【解决方案2】:

    您能否更改您的 GetImageHeight/Width() 方法以返回 Unit

    【讨论】:

      【解决方案3】:

      这对我有用,试一试:

      <asp:Image runat="server" ID="imgProduct" ImageUrl='test.jpg' 
       Height='<%# GetImageHeight() %>' Width='<%# GetImageWidth() %>' />
      

      在.cs中:

      protected int GetImageHeight()
      {
          return 5;
      }
      protected int GetImageWidth()
      {
          return 5;
      }
      

      渲染这个:

      <img id="imgProduct" src="test.jpg" style="height:5px;width:5px;border-width:0px;" />
      

      【讨论】:

      • 但它没有绑定到带下划线的对象。我仍然需要绑定它,该对象正在保存该信息。不是页面...谢谢...
      猜你喜欢
      • 1970-01-01
      • 2018-03-29
      • 1970-01-01
      • 2011-04-26
      • 1970-01-01
      • 2012-02-02
      • 1970-01-01
      • 2011-05-16
      • 2016-10-05
      相关资源
      最近更新 更多