隐藏和显示标签和按钮 [英] Hide and Show Label and Button

查看:288
本文介绍了隐藏和显示标签和按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个标签,两个文本框和显示1按钮。

在页面加载名称按钮(将初步显示)。当我点击按钮后我需要显示年龄 标签文本。我怎样才能做到这一点?

 <&OL GT;
     <立GT;
          < ASP:标签=服务器AssociatedControlID =名称>
             用户名
          < / ASP:标签>
          < ASP:文本框=服务器ID =名称WIDTH =167p​​x/>
          < ASP:按钮的ID =Button1的=服务器文本=按钮/>
     < /李>
     <立GT;
          < ASP:标签=服务器AssociatedControlID =时代>年龄< / ASP:标签>
          < ASP:文本框=服务器ID =年龄的TextMode =年龄WIDTH =240像素/>
     < /李>
< / OL>

code为按钮preSS

 保护无效的button1_Click(对象发件人,EventArgs的发送)
{}


解决方案

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

添加的OnClientClick 你的按钮

 < ASP:按钮的ID =Button1的=服务器文本=按钮的OnClientClick =ShowLabel();/>

和申报页面上的的JavaScript 功能:

 <脚本类型=文/ JavaScript的>
    功能ShowLabel(){
      //注意,客户端ID可能是从服务器端ID不同
      。的document.getElementById('lblAge')的style.display ='继承';
    }
< / SCRIPT>

您需要将标签显示样式设置为开始。

 < ASP:标签ID =lblAge的风格=显示:无; =服务器AssociatedControlID =时代>年龄< / ASP:标签>

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

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 ?

<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)
{

}

解决方案

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.

Add OnClientClick to your button :

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

and declare the JavaScript function on page:

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

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

这篇关于隐藏和显示标签和按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆