ASP.NET disable button at runtime

I have a button in a formview that I want to disable at runtime but I cant get it to work.

In my pageload I have:
  Button btnReturn = (Button)fwTest.FindControl("btnReturn");
 if (MyObject.Id == 1)
          btnReturn.Enabled = false;

I can debug and see that i passes the line that set it to disabled but it is not disabled
in the browser....
Who is Participating?
Paul JacksonConnect With a Mentor Software EngineerCommented:
I was going to suggest moving the code to Page_prerendercomplete to see if it worked there, I suggest there is something happing after the Page_Load probably in the fvTest_load that is enabling the control probably something to do with the formview being set into Edit mode.
Paul JacksonSoftware EngineerCommented:
In debug can you see that btnReturn is being set correctly and finding the button and not just returning null.
pucko73Author Commented:
Yes i can.
The new generation of project management tools

With’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Paul JacksonSoftware EngineerCommented:
Please post your complete pageload code and the formview markup.
pucko73Author Commented:
it seems to work if i put the button on a panel
and add the same code on pnlButtons_PreRender:

All I have in the pageload was the code abowe. (the same code that I moved to pre_render.
No other code that mess with the button.

the button is the last thing inside:
 <asp:FormView ID="fvTest" runat="server" DefaultMode="Edit" DataSourceID="odsVarianceDetails" Width="100%" >
some table with labels like:
<asp:Label ID="lblMyLablel" runat="server" Text='<% #Eval("TotalPrice", "{0:C}")%>' Width="120" Style="text-align: right"></asp:Label></td>
and a gridview....
<asp:GridView ID="gvMyGridView" runat="server" DataSourceID="odsMyGridview" AutoGenerateColumns="false" GridLines="None" OnRowDataBound="gvMyGridView_RowDataBound">

and my button:
<asp:Button ID="btnReturn" Text="Return" runat="server" OnClick="btnReturn_Click" />


pucko73Author Commented:
also tried to set it visible false and that did not work either.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.