skip to Main Content

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


  1. Chosen as BEST ANSWER

    This is because I am using <asp:LoginView and buttons was inside it.That's why you may use below code:

    Button BtnAddnew = ((Button)(this.LVButton.FindControl("BtnAddnew")));
    Button BtnSaveNew = ((Button)(this.LVButton.FindControl("BtnSaveNew")));
    Button BtnUpdate = ((Button)(this.LVButton.FindControl("BtnUpdate")));
    Button BtnSaveUpdate = ((Button)(this.LVButton.FindControl("BtnSaveUpdate")));
    

  2. This suggests that you don’t in code behind
    have the button click events.

    Try removing the this part of each button:

    So, change:

     <asp:Button ID="BtnAddnew" CssClass="bluebutton" 
        title="Click to Add New Employee" runat="server" 
       Text="Add New" OnClick="BtnAddnew_Click" />
    

    to:

      <asp:Button ID="BtnAddnew" CssClass="bluebutton" 
        title="Click to Add New Employee" runat="server"
        Text="Add New" />
    

    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:

    enter image description here

    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.

    Login or Signup to reply.
  3. you should check you aspx page cs refrence class.
    
    <%@ Page Title="" Language="C#" MasterPageFile="~/Test/Test/Test.master" AutoEventWireup="true" CodeFile="Test_Update.aspx.cs" Inherits="ProjectName_FolderName_Test_Update" %>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search