样式属性未出现
我在 ListView 的每个项目中显示图像。我希望图像在框中水平和垂直居中。我的代码如下:
Protected Sub MembersLV_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewItemEventArgs) Handles MembersLV.ItemDataBound
If e.Item.ItemType = ListViewItemType.DataItem Then
Dim LogoImage As System.Web.UI.WebControls.Image = e.Item.FindControl("LogoImage")
Dim LogoLink As HtmlControl = e.Item.FindControl("LogoLink")
Dim di As ListViewDataItem = DirectCast(e.Item, ListViewDataItem)
Dim ImageFileName As String = di.DataItem("FileName")
Dim FilePath As String = String.Format("/Uploads/MembersDirectory/w125h85/{0}", ImageFileName)
If File.Exists(Server.MapPath(FilePath)) Then
LogoImage.ImageUrl = FilePath
Dim BoxHeight As Integer = 101
Dim BoxWidth As Integer = 138
Dim B As New Bitmap(Server.MapPath(FilePath))
Dim BHeight As Integer = B.Height
Dim BWidth As Integer = B.Width
Dim PaddingTop As Integer = Math.Ceiling(BoxHeight - BHeight) / 2
Dim PaddingLeft As Integer = Math.Ceiling(BoxWidth - BWidth) / 2
LogoLink.Attributes.Add("style", String.Format("padding: {0} 0 0 {1};", PaddingTop, PaddingLeft))
End If
End If
End Sub
我简单地计算图像顶部和左侧居中所需的填充量,然后将该填充作为样式属性添加到周围的标签。
aspx 文件中的代码如下:
<a href='<%#Eval("Website") %>' id="LogoLink" runat="server">
<asp:Image ID="LogoImage" runat="server" AlternateText='<%#Eval("Name") %>' />
</a>
当我运行此代码时,没有对 标记应用填充。因此,我进行了快速检查,以确保使用 Response.Write 正确计算了 PaddingTop 和 PaddingLeft。奇怪的是,这次运行的时候,style属性却正确出现了!但如果没有 Response.Write,就没有样式属性。
我做错了什么?
I am displaying an image within each itme of a ListView. I want the image to be centered horizontally as well as vertically in a box. My code is as follows:
Protected Sub MembersLV_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewItemEventArgs) Handles MembersLV.ItemDataBound
If e.Item.ItemType = ListViewItemType.DataItem Then
Dim LogoImage As System.Web.UI.WebControls.Image = e.Item.FindControl("LogoImage")
Dim LogoLink As HtmlControl = e.Item.FindControl("LogoLink")
Dim di As ListViewDataItem = DirectCast(e.Item, ListViewDataItem)
Dim ImageFileName As String = di.DataItem("FileName")
Dim FilePath As String = String.Format("/Uploads/MembersDirectory/w125h85/{0}", ImageFileName)
If File.Exists(Server.MapPath(FilePath)) Then
LogoImage.ImageUrl = FilePath
Dim BoxHeight As Integer = 101
Dim BoxWidth As Integer = 138
Dim B As New Bitmap(Server.MapPath(FilePath))
Dim BHeight As Integer = B.Height
Dim BWidth As Integer = B.Width
Dim PaddingTop As Integer = Math.Ceiling(BoxHeight - BHeight) / 2
Dim PaddingLeft As Integer = Math.Ceiling(BoxWidth - BWidth) / 2
LogoLink.Attributes.Add("style", String.Format("padding: {0} 0 0 {1};", PaddingTop, PaddingLeft))
End If
End If
End Sub
I simply calculate the amount of padding needed for the top and left of the image for it to be centered, and then add that padding as a style attribute to the surrounding tag.
The code in the aspx file is as follows:
<a href='<%#Eval("Website") %>' id="LogoLink" runat="server">
<asp:Image ID="LogoImage" runat="server" AlternateText='<%#Eval("Name") %>' />
</a>
When I ran this code, no padding had been applied to the <a>
tags. So I did a quick check to ensure that PaddingTop
and PaddingLeft
were being calculted properly using Response.Write
. Strangely, when I ran it this time, the style attribute appeared correctly! But without Response.Write
, there is no style attribute.
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我刚刚休息完回来再看看这个。花了 5 分钟查看它,我突然意识到我没有指定填充的单位!我已将代码从: 更改
为:
现在可以了!另外,我发现
style
属性实际上显示在源代码中(如果您查看页面的源代码),但在 Firebug 中检查时它并不存在,这让我感到困惑。我仍然不明白为什么添加 Response.Write 行意味着应用样式属性,所以如果有人可以解释我将有兴趣知道为什么。I have just come back to have another look at this after a break from it. Having spent 5 minutes looking at it I suddenly realised I haven't specified the units for the padding! I have changed the code from:
to:
This now works! Also, I have found that the
style
attribute is actually displayed in the source (if you View Source of the page), it's just not there if inspected in Firebug which threw me off the scent. I still don't understand why adding the line of Response.Write meant that the style attribute was applied, so if anyone could explain I would be interested to know why.