ASP.NET C#单击按钮时如何隐藏/显示文本

时间:2012-10-14 14:03:54

标签: c# asp.net

我想显示标签(隐藏可见)并在点击时更改按钮的文字,并且能够再次点击该按钮以显示其原始文本并隐藏标签。“

    public partial class TestClick : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack) 
        {

            btnShowButtonText.Text = "Button";
            lblShowText.Visible = false;

        }
    }
    protected void btnShowButtonText_Click(object sender, EventArgs e)
    {
        if (!Session.IsNewSession)
        {
            btnShowButtonText.Text = "Hide gift voucher details";
            lblShowText.Visible = true;
        }
    }      

2 个答案:

答案 0 :(得分:0)

你可以使用一个计数变量。 像这样:

static  int cnt=0;
    protected void btnShowButtonText_Click(object sender, EventArgs e)
        {
    cnt++;
            if (cnt%2!=0)
            {
                btnShowButtonText.Text = "Hide gift voucher details";
                lblShowText.Visible = true;
            }
    if (cnt%2==0) 
            {

                btnShowButtonText.Text = "Button";
                lblShowText.Visible = false;

            }
        }  

当您第一次点击时,按钮文字为"隐藏礼券详细信息"
下一次按钮文本是"按钮"

答案 1 :(得分:0)

您可以使用隐藏字段来确定按钮和文本的状态:

  public partial class TestClick : System.Web.UI.Page
  {
    protected void Page_Load(object sender, EventArgs e)
    {
    if (!Page.IsPostBack) 
    {

        btnShowButtonText.Text = "Button";
        lblShowText.Visible = false;
    }
   }

protected void btnShowButtonText_Click(object sender, EventArgs e)
{
    if(hiddenField.Value == "0")
    {
       btnShowButtonText.Text = "Hide gift voucher details";
       lblShowText.Visible = true;
       hiddenField.Value = "1";
    }
    else
    {
       btnShowButtonText.Text = "Button";
       lblShowText.Visible = false;
       hiddenField.Value = "0";
    }
}