user1989
user1989

Reputation: 217

Button becomes invisible on its click

I have a button and I make it visible =false when, I declared it. But when the page loads, in the pre-render function, I make it visible=true. And it works fine, but when I click on the button(Insert), the button again becomes invisible, but I want that button to be visible.

<asp:Button ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert" Visible="false"
                        Text="Insert"  />

 protected void Page_PreRender(object sender, EventArgs e)
    {
         Button btValue = (Button)FormView1.FindControl("InsertButton");
            if (btValue != null)
        {
            btValue.Visible = true; //IT COMES HERE, WHEN I DEBUG
        }
    }

As the page loads, the button becomes visible, but as soon as i click insert button, it becomes invisible.

Upvotes: 0

Views: 312

Answers (1)

Pankaj Kapare
Pankaj Kapare

Reputation: 7802

What is happening is you have wirrten code in Page_Render event (which is not an event in reality) which is getting executed first and then your button's render fires up which is causing visibility setting it back to false (as per dictated in markup). If you handle Button's render/prerender event and set visibility there then you should be good.

Code should look like below

    <asp:Button ID="myButton" runat="server" onprerender="myButton_PreRender" /> 

    protected void myButton_PreRender(object sender, EventArgs e)
    {

    }

Upvotes: 2

Related Questions