我有一个锚点设置为显示:内联块,它包裹文本紧如我想,没有指定高度或宽度。现在我需要从我的代码隐藏中获得这个宽度和高度。在代码中没有指定锚高度和宽度的代码后面?
有人可以向我解释我怎样才能得到这些值,即使他们没有指定,只是从里面的文本的宽度/高度继承?
UPDATE:找到解决方案
这可以使用JavaScript属性的offsetHeight和element.This的offsetWidth是代码,我放在一起仓促完成:
JS:
<script language="javascript" type="text/javascript">
function GetAnchorSize() {
var buttonWidth = document.getElementById('<%= button.ClientID %>').offsetWidth;
var buttonHeight = document.getElementById('<%= button.ClientID %>').offsetHeight;
document.getElementById('<%= buttonWidth.ClientID %>').value = buttonWidth;
document.getElementById('<%= buttonHeight.ClientID %>').value = buttonHeight;
}
</script>
元我需要的尺寸和两个隐藏字段以输入数值:
<input type="hidden" id="buttonWidth" runat="server" />
<input type="hidden" id="buttonHeight" runat="server" />
<a id="button" href="#" onmousemove="GetAnchorSize();" runat="server"
style="display: inline-block; border-color: Black;
text-decoration: none; border-style: solid; border-width: 1px; background-repeat:no-repeat;">Text</a>
我将其设置为在鼠标移动时得到更新,您可以将其设置为您需要更新值的事件。
并获得在代码隐藏值:
int btnWidth = Convert.ToInt32(buttonWidth.Value);
int btnHeight= Convert.ToInt32(buttonHeight.Value);
我想重申这是一个JavaScript问题。在页面被浏览器呈现之前,高度和宽度都没有最终确定。 –
锚点位于“更新面板”中,因此它会根据用户设置进行渲染。 – formatc
如果您没有明确强制大小,则只有在客户端上呈现控件之后才能确定像素尺寸。除非您通过JavaScript从DOM检索并将其发送回服务器,否则无法从服务器获取该信息。 –