[英]Hide and Show Label and Button
我有2個標簽和2個文本框以及1個按鈕。
頁面加載時, Name
和Button
(將最初顯示)。 稍后,當我單擊Button
我需要顯示age
label
和textbox
。 我怎樣才能做到這一點 ?
<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。 默認情況下,只要將所有控件添加到頁面上,所有控件都將顯示在屏幕上。您需要執行以下操作:
TextMode="age"
因為不存在任何類型為age的受支持文本模式。 默認情況下,使用以下代碼將看不到年齡標簽和文本框:
<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的基本概念。 您可以使用控件的可見屬性。
<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.