2010-08-11 35 views
0

是否可以將兩個按鈕放在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" /> 
+0

你有什麼問題,你不覺得這是可能的。只需使用客戶端的CSS來隱藏和顯示其他。然後你可以切換一些JavaScript,如果你需要它們都來到頁面。 – spinon 2010-08-11 17:35:15

回答

2

選項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'; 
    } 
} 
0

當然。將它們分別放在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

1

爲什麼不使用一個按鈕,改變基於上述條件的文字和動作?

0

而不是使用兩個按鈕的改變基於代碼背後的條件的按鈕屬性,或者在CSS/Java腳本的客戶端執行相同的操作。

相關問題