Skip to main content
 首页 » 编程设计

c#之2个按钮在同一位置。不同时间展示!

2024年02月24日24duanxz

是否可以将两个按钮放在 aspx 页面中的相同位置。根据某些条件,它必须显示其中之一。

<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';    
    } 
}