I am using the below code in ASP.NET to hide action buttons for simple users who can see only data but can’t change it;
<asp:LoginView ViewStateMode="Disabled" runat="server">
<RoleGroups>
<asp:RoleGroup Roles="HRAdmin">
<ContentTemplate>
<asp:Button ID="BtnAddnew" CssClass="bluebutton" title="Click to Add New Employee" runat="server" Text="Add New" OnClick="BtnAddnew_Click" />
<asp:Button ID="BtnSaveNew" CssClass="bluebutton" title="Click to Save New Record" runat="server" Text="Save New" Visible="false" OnClick="BtnSaveNew_Click" />
<asp:Button ID="BtnUpdate" CssClass="bluebutton" title="Click to Update Record" runat="server" Text="Update" OnClick="BtnUpdate_Click" />
<asp:Button ID="BtnSaveUpdate" CssClass="bluebutton" title="Click to Save Record" runat="server" Text="Save Update" Visible="false" OnClick="BtnSaveUpdate_Click" />
</ContentTemplate>
</asp:RoleGroup>
</RoleGroups>
</asp:LoginView>
But when I load page or build project, then I get errors:
The name ‘BtnAddnew’ does not exist in the current context.
The name ‘BtnSaveNew’ does not exist in the current context.
The name ‘BtnUpdate’ does not exist in the current context.
The name ‘BtnSaveUpdate’ does not exist in the current context.
It means in the C# code it is not loading OnClick
event of these buttons. How can I fix this problem?
3
Answers
This is because I am using
<asp:LoginView
and buttons was inside it.That's why you may use below code:This suggests that you don’t in code behind
have the button click events.
Try removing the this part of each button:
So, change:
to:
Now, do a build-rebuild Solution
If you want to add a click event to each button, then in markup, type on OnClick=, and THEN let intel-sense suggest to you to create new event (a click event)
eg this:
It will seem like nothing occurred, but if you flip over to code behind, then the button click event should now exist.
In fact, before you try any of above, exit VS. Re-load. Do a bio;d ->rebuild solution. Then try the page – it it still don’t work, then you need to do above.