ASP.NET设置Visible = false不会停留

时间:2018-01-24 21:51:35

标签: asp.net webforms click visible

当我告诉他们时,我想要显示很多按钮,到目前为止,我一直使用Visible = true或false,具体取决于我希望它们出现的时间。我希望按钮在发生单击事件后消失。我在代码中有两个按钮,当我点击btnOption131时,它会消失,但是当我点击btnOption132时,它会消失,并且btnOption131会重新出现。无论如何我可以阻止这种情况发生吗?

<asp:Button ID="btnOption131" runat="server" Text="Think" OnClick="btnOption131_Click" Visible="False" CssClass="button"/>
<asp:Button ID="btnOption132" runat="server" Text="Go Crazy" OnClick="btnOption132_Click" Visible="False"/>

代码背后:

protected void btnOption131_Click(object sender, EventArgs e)
{
    txt1.Text = statementArray1[8].ToString();
    btnOption131.EnableViewState = false;
    btnOption131.Visible = false;
}

protected void btnOption132_Click(object sender, EventArgs e)
{
    txt1.Text = statementArray1[9].ToString();
    btnOption132.EnableViewState = false;
    btnOption132.Visible = false;
}

我也尝试过:

    protected void btnOption131_Click(object sender, EventArgs e)
{
    txt1.Text = statementArray1[8].ToString();
    btnOption131.Visible = false;
}

protected void btnOption132_Click(object sender, EventArgs e)
{
    txt1.Text = statementArray1[9].ToString();
    btnOption132.Visible = false;
}

1 个答案:

答案 0 :(得分:0)

您必须将按钮Visibility设置为&#39; true&#39;某处。我的猜测是它没有包含在IsPostBack支票中:

protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack == false)
    {
        btnOption131.Visible = true;
    }
}

如果不这样做,当下一个PostBack(=按钮点击)发生时,可见性将始终设置为true