簡體   English   中英

隱藏和顯示標簽和按鈕

[英]Hide and Show Label and Button

我有2個標簽和2個文本框以及1個按鈕。

頁面加載時, NameButton (將最初顯示)。 稍后,當我單擊Button我需要顯示age labeltextbox 我怎樣才能做到這一點 ?

<ol>
     <li>
          <asp:Label runat="server" AssociatedControlID="Name">
             User name
          </asp:Label>
          <asp:TextBox runat="server" ID="Name" Width="167px" />
          <asp:Button ID="Button1" runat="server" Text="Button" />
     </li>                           
     <li>
          <asp:Label runat="server" AssociatedControlID="age">age</asp:Label>
          <asp:TextBox runat="server" ID="age" TextMode="age" Width="240px" />
     </li>                         
</ol>

按鍵代碼

protected void Button1_Click(object sender, EventArgs e)
{

}

您可以在服務器端將label / textbox的Visible屬性設置為True 另外,您可以使用JavaScript避免回發到服務器。

OnClientClick添加到您的button

<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="ShowLabel();"/>

並在頁面上聲明JavaScript函數:

<script type="text/javascript">
    function ShowLabel() {
      // Note that the client ID might be different from the server side ID
      document.getElementById('lblAge').style.display = 'inherit';
    }
</script>

最初需要將“ Label顯示”樣式設置為“ none

<asp:Label ID="lblAge" style="display: none;" runat="server" AssociatedControlID="age">age</asp:Label>

試試下面的代碼:

您需要根據需要將控件的Visible屬性設置為True或False。 默認情況下,只要將所有控件添加到頁面上,所有控件都將顯示在屏幕上。您需要執行以下操作:

  1. 您需要刪除TextMode="age"因為不存在任何類型為age的受支持文本模式。
  2. 如果要在后面的代碼中訪問控制服務器端,則需要定義控件的ID。 因此,定義與年齡文本框相對應放置的Label的ID。

默認情況下,使用以下代碼將看不到年齡標簽和文本框:

      <asp:Label ID="lblAge" runat="server" AssociatedControlID="age" Visible="false">age</asp:Label>
      <asp:TextBox runat="server" ID="age" Width="240px"  Visible="false"/>

后面的代碼:使用以下代碼,在按鈕上單擊年齡標簽后,文本框將可見:

protected void Button1_Click(object sender, EventArgs e)
    {
        lblAge.Visible = true;
        age.Visible = true;
    }

首先將id添加到元素並設置visible false

<asp:Label runat="server" AssociatedControlID="age" Visible="false" Id="lbl1">age</asp:Label>
<asp:TextBox runat="server" ID="age" TextMode="age" Width="240px" Visible="false" /> 

按鈕單擊事件集visible true

protected void Button1_Click(object sender, EventArgs e)
{
    lbl1.Visible = True;
    age.Visible = True;
}

這是asp.net的基本概念。 您可以使用控件的可見屬性。

  1. 您的TextMode枚舉錯誤。 沒有Textbox.TextMode TextMode的年齡枚舉

<li> <asp:Label runat="server" AssociatedControlID="age" id="lblAge">age</asp:Label> <asp:TextBox runat="server" ID="age" TextMode="age" Width="240px" /> </li>

在后面的代碼中

protected void Button1_Click(object sender, EventArgs e)
{
  lblAge.Visible=true;
  age.Visible=true;
}
    protected void Page_Load(object sender, EventArgs e)
    {
        NameLabel.Visible = false;
        NameBox.Visible = false;
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        NameLabel.Visible = true;
        NameBox.Visible = true;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM