2个按钮位于同一位置。在不同的时间显示!
问题描述:
是否可以将两个按钮放在aspx页面的相同位置。根据某些条件,它必须显示其中之一。2个按钮位于同一位置。在不同的时间显示!
<asp:ImageButton ID="btnapply" ImageUrl="../images/apply.gif" runat="server"
ImageAlign="Right" OnClick="imgApply_Click" ValidationGroup="0" />
<asp:ImageButton ID="btnremove" ImageUrl="../images/remove.gif" runat="server"
ImageAlign="Right" OnClick="imgremove_Click" ValidationGroup="0" />
答
选项1:如果你能做出决定,因为页面呈现,即服务器端:
在你的后台代码:
protected void Page_Load()
{
if (variableToSwitchOn == true)
{
button1.Visible = true;
button2.Visible = false;
}
else
{
button1.Visible = false;
button2.Visible = true;
}
}
在.aspx页面:
<div>
<asp:button runat="server" ID="button1" Text="Button 1" />
<asp:button runat="server" ID="button2" Text="Button 2" />
</div>
选项2:如果你需要做出决定的客户端
在.aspx页面:
<div>
<asp:button runat="server" ID="button1" Text="Button 1" />
<asp:button runat="server" ID="button2" Text="Button 2" />
</div>
<script language="javascript" type="text/javascript">
var button1Id = '<%=button1.ClientId%>';
var button2Id = '<%=button2.ClientId%>';
</script>
你现在可以有一段JavaScript控制的按钮是否可见,例如:
function ChangeWhichButtonIsVisible()
{
var button1 = document.getElementById(button1Id);
var button2 = document.getElementById(button2Id);
if (someCondition == true)
{
button1.style.display = 'none';
button2.style.display = 'block';
}
else
{
button1.style.display = 'block';
button2.style.display = 'none';
}
}
答
当然。将它们分别放在DIV中,并根据需要隐藏/显示这些DIV。为了扩大一点:
<div id="button1" style="display:none">Button 1 goes here</div>
<div id="button2" style="display:block">Button 2 goes here</div>
然后您可以切换使用JavaScript代码的显示:
function showHide() {
if (document.getElementById("button1").style.display == "none")) {
document.getElementById("button1").style.display = "block";
document.getElementById("button2").style.display = "none";
} else {
document.getElementById("button1").style.display = "none";
document.getElementById("button2").style.display = "block";
}
}
+0
为什么要用额外的div来污染页面? – Rob 2010-08-11 17:46:56
答
为什么不使用一个按钮,改变基于上述条件的文字和动作?
答
而不是使用两个按钮的改变基于代码背后的条件的按钮属性,或者在CSS/Java脚本的客户端执行相同的操作。
你有什么问题,你不觉得这是可能的。只需使用客户端的CSS来隐藏和显示其他。然后你可以切换一些JavaScript,如果你需要它们都来到页面。 – spinon 2010-08-11 17:35:15