C# 隐藏和显示标签和按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14455371/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hide and Show Label and Button
提问by sharon Hwk
I have 2 labels and 2 text boxes and 1 buttons displayed.
我显示了 2 个标签和 2 个文本框和 1 个按钮。
When the page loads the Name
and Button
(will be initially displayed). Later when i click on the Button
i need to display the age
label
and textbox
. How can i do this ?
当页面加载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>
code for button press
按钮按下代码
protected void Button1_Click(object sender, EventArgs e)
{
}
采纳答案by Abdusalam Ben Haj
You could set the label/textbox Visible
property to True
in server side. Alternatively, you could use JavaScript
to avoid post backs to the server.
您可以将标签/文本框Visible
属性设置为True
在服务器端。或者,您可以使用JavaScript
来避免回发到服务器。
Add OnClientClick
to your button
:
添加OnClientClick
到您的button
:
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="ShowLabel();"/>
and declare the JavaScript
function on page:
并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>
You need to set the Label
Display style to none
initially.
您需要将Label
显示样式设置为none
初始。
<asp:Label ID="lblAge" style="display: none;" runat="server" AssociatedControlID="age">age</asp:Label>
回答by Ravi Gadag
this is the basic concept of asp.net. you can use visible property of the control.
这是asp.net的基本概念。您可以使用控件的可见属性。
- your TextMode enumeration is wrong. there is no Age enumeration for Textbox.TextMode TextMode
- 您的 TextMode 枚举是错误的。Textbox.TextMode TextMode没有 Age 枚举
<li>
<asp:Label runat="server" AssociatedControlID="age" id="lblAge">age</asp:Label>
<asp:TextBox runat="server" ID="age" TextMode="age" Width="240px" />
</li>
<li>
<asp:Label runat="server" AssociatedControlID="age" id="lblAge">age</asp:Label>
<asp:TextBox runat="server" ID="age" TextMode="age" Width="240px" />
</li>
in code behind
在后面的代码中
protected void Button1_Click(object sender, EventArgs e)
{
lblAge.Visible=true;
age.Visible=true;
}
回答by AliR?za Ad?yah?i
First add id
to elements and set visible
false
首先添加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" />
button click event set visible
true
按钮点击事件设置为visible
真
protected void Button1_Click(object sender, EventArgs e)
{
lbl1.Visible = True;
age.Visible = True;
}
回答by Gaurav Rajput
Try below code:
试试下面的代码:
You need to set Visible
property of controls to True or False according to your requirement. By default, all controls are visible on the screen whenever they are added on the page.You need to do following thing:
您需要Visible
根据您的要求将控件的属性设置为 True 或 False。默认情况下,所有控件在页面上添加时都在屏幕上可见。您需要执行以下操作:
- You need to remove
TextMode="age"
as there is not any supported textmode of type age. - Need to define id of control if you want to access a control server side in code behind. So define the ID of Label that you put corresponding to Age textbox.
- 您需要删除,
TextMode="age"
因为没有任何受支持的年龄类型的文本模式。 - 如果要在后面的代码中访问控件服务器端,则需要定义控件的 id。所以定义你放置的对应于 Age 文本框的 Label 的 ID。
By Default age label and textbox will not be visible by using below code:
默认情况下,使用以下代码将看不到年龄标签和文本框:
<asp:Label ID="lblAge" runat="server" AssociatedControlID="age" Visible="false">age</asp:Label>
<asp:TextBox runat="server" ID="age" Width="240px" Visible="false"/>
Code behind: After button click age label and the textbox will be visible by using below code:
代码隐藏:按钮单击年龄标签后,文本框将通过使用以下代码可见:
protected void Button1_Click(object sender, EventArgs e)
{
lblAge.Visible = true;
age.Visible = true;
}
回答by Pritam Panda
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;
}