locked
Visibility of Button by Session User RRS feed

  • Question

  • User-1901014284 posted

    Hi,

    I would like to set the visibility of a button to be true or false depending if the session user

    (gathered by the signed in user using the following code: sessionData = Session["User"] as LoginDetail;)

    equals the CreatedByTextbox value which would contain the username of the user which has added the entry to the database. If the session user equals the same to the CreatedByTextbox then I would like to set the button's visibility to be true, else if not false.

    At the moment I am using the below to check whether a user is admin to access certain textboxes, please see below code. 

    ( if (sessionData.hasRole("ROLE_ADMIN"))
    {
    TravelTimeStartStartTextBox.ReadOnly = false;
    }
    else
    {
    TravelTimeStartStartTextBox.ReadOnly = true;
    }

    I am wondering if I can use a similar process to this for the visibility of the button?

    Any help would be greatly appreciated.

    Many thanks

    Jonny 

    Wednesday, November 14, 2018 2:38 PM

Answers

  • User283571144 posted

    Hi jonnygareth30,

    Do you mean if the session user's username equals with the CreatedByTextbox's text value then show the button?

    If this is your requirement, I suggest you could try below codes:

            protected void Page_Load(object sender, EventArgs e)
            {
                LoginDetail detail=  Session["user"] as LoginDetail; //get LoginDetail from session
                if (!IsPostBack)
                {
                    var expenses = selectEntryById(Convert.ToInt32(Request["id"]));//I modify your method to selectentrybyid, you could directly use your codes.
                    CreatByTextbox.Text = expenses.Created_By; //set  CreatByTextbox's text to selected Expense_Entry_Form's Create_By
                }
                    if (CreatByTextbox.Text ==detail.Username)// check whether LoginDetail's Username equals to  expenses.Created_By
                    {
                        Button1.Visible = true;
                    }
                    else
                    {
                        Button1.Visible = false;
                    }
            }
    

    Result:

    PageA:

    PageB

    If I use user3 to access page:

    If I use user4 to access the page:

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 20, 2018 8:02 AM

All replies

  • User283571144 posted

    Hi  jonnygareth30,

    According to your description, I couldn’t understand your requirement clearly.

    Could you please post the details information about the LoginDetail class?

    Could you please tell me how you define CreateByTextbox variable and how we select the CreateByTextbox value from database? According to control id?

    If you could post more details information, it will be more easily for us to understand your requirement and find out a solution.

    Best Regards,

    Brando

    Thursday, November 15, 2018 5:33 AM
  • User-1901014284 posted

    Hi Brando ZWZ,

    The LoginDetail class is used to get the username of the user that has signed into the system, see below code within the LoginDetail class:

    public static LoginDetail GetOne(int id)
    {
    return _db.Query<LoginDetail>("SELECT * FROM LoginDetails WHERE ID = @ID", new { id }).SingleOrDefault();
    }

    public static LoginDetail GetOneByName(string userName)
    {
    string sql =
    @"SELECT
    *
    FROM
    LoginDetails AS ld
    WHERE
    ld.username = @userName";

    Also the CreatedbyTextbox is populated using a class also, please see below code behind within the ASPX.cs page:

    if (!Page.IsPostBack)
    {
    var expenses = repo.Find(Int32.Parse(Request.QueryString["id"]));

    CreatedByTextBox.Text = expenses.Created_By;

    Please see below code from the Expense_Entry_Form class which is used within the above code:

    public class Expense_Entry_Form
    {
    public string Created_By { get; set; }

    public interface IExpensesRepository
    {
    List<Expense_Entry_Form> GetAll();
    List<Expense_Entry_Form> FindByCreatedBy(string Created_By);
    Expense_Entry_Form Add(Expense_Entry_Form ExpensesCustClass);
    Expense_Entry_Form Update(Expense_Entry_Form ExpensesCustClass);
    void Remove(int id);
    Expense_Entry_Form GetExpenseCustInformatiom(int id);
    }

    public class ExpenseRepository : IExpensesRepository
    {

    public List<Expense_Entry_Form> GetAll()
    {
    return this._db.Query<Expense_Entry_Form>("SELECT * FROM Expense_Entry_Form").ToList();
    }

    public Expense_Entry_Form Find(int id)
    {
    return this._db.Query<Expense_Entry_Form>("SELECT * FROM Expense_Entry_Form WHERE ID = @ID ", new { id }).SingleOrDefault();
    }

    I hope the above has helped clear up the LoginDetail class and also how the CreatedByTextbox has been populated from the textbox.

    What I am trying to achieve is if the signed in user matched the record of the CreatedByTextbox then I want the button to be visible (visible = true) for the user to click. If the signed in user does not match the CreatedByTextbox then I would like the button to be hidden (visible = false) so the user is unable to use the button function.

    I hope this helps clear up my query,

    Many thanks

    Jonny

    Thursday, November 15, 2018 10:19 AM
  • User283571144 posted

    Hi jonnygareth30,

    Do you mean if the session user's username equals with the CreatedByTextbox's text value then show the button?

    If this is your requirement, I suggest you could try below codes:

            protected void Page_Load(object sender, EventArgs e)
            {
                LoginDetail detail=  Session["user"] as LoginDetail; //get LoginDetail from session
                if (!IsPostBack)
                {
                    var expenses = selectEntryById(Convert.ToInt32(Request["id"]));//I modify your method to selectentrybyid, you could directly use your codes.
                    CreatByTextbox.Text = expenses.Created_By; //set  CreatByTextbox's text to selected Expense_Entry_Form's Create_By
                }
                    if (CreatByTextbox.Text ==detail.Username)// check whether LoginDetail's Username equals to  expenses.Created_By
                    {
                        Button1.Visible = true;
                    }
                    else
                    {
                        Button1.Visible = false;
                    }
            }
    

    Result:

    PageA:

    PageB

    If I use user3 to access page:

    If I use user4 to access the page:

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 20, 2018 8:02 AM
  • User-1901014284 posted

    Thank you for your help, greatly appreciated.

    Wednesday, November 21, 2018 1:11 PM