代码之家  ›  专栏  ›  技术社区  ›  vondip

从服务器端绑定int值-asp.net

  •  1
  • vondip  · 技术社区  · 15 年前

    这里有个简短的问题。我肯定这是可能的,只是不能让它工作。

    无法创建类型为的对象 '系统.Web.UI.网络控制单元“从 “<%#Bind(”GetImageHeight()“)%”表示

    <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;
        }
    
    3 回复  |  直到 15 年前
        1
  •  1
  •   Sky Sanders    15 年前

    哦!我第一次错过了。。尝试删除绑定中的引号

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

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

    返回一个int就可以了。不需要这些方法中的所有繁杂工作。

    我可能错了。。。

        2
  •  0
  •   womp    15 年前
        3
  •  0
  •   Nick Craver    15 年前

    <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;" />
    
    推荐文章