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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:51:55  来源:igfitidea点击:

Hide and Show Label and Button

c#asp.net

提问by sharon Hwk

I have 2 labels and 2 text boxes and 1 buttons displayed.

我显示了 2 个标签和 2 个文本框和 1 个按钮。

When the page loads the Nameand Button(will be initially displayed). Later when i click on the Buttoni need to display the agelabeland textbox. How can i do this ?

当页面加载NameButton(将最初显示)。稍后当我点击Button我需要显示agelabeltextbox。我怎样才能做到这一点 ?

<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 Visibleproperty to Truein server side. Alternatively, you could use JavaScriptto avoid post backs to the server.

您可以将标签/文本框Visible属性设置为True在服务器端。或者,您可以使用JavaScript来避免回发到服务器。

Add OnClientClickto your button:

添加OnClientClick到您的button

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

and declare the JavaScriptfunction 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 LabelDisplay style to noneinitially.

您需要将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的基本概念。您可以使用控件的可见属性。

  1. your TextMode enumeration is wrong. there is no Age enumeration for Textbox.TextMode TextMode
  1. 您的 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 idto elements and set visiblefalse

首先添加id到元素并设置visiblefalse

<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 visibletrue

按钮点击事件设置为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 Visibleproperty 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。默认情况下,所有控件在页面上添加时都在屏幕上可见。您需要执行以下操作:

  1. You need to remove TextMode="age"as there is not any supported textmode of type age.
  2. 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.
  1. 您需要删除,TextMode="age"因为没有任何受支持的年龄类型的文本模式。
  2. 如果要在后面的代码中访问控件服务器端,则需要定义控件的 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;
    }