0

Hello I want to make a button visibile when UserId stored in database match with current UserId.

            string clientId = Context.User.Identity.GetUserId();

            JobDescriptions job = new JobDescriptions();

            if (job.PostedBy == clientId)
            {
                Button2.Visible = true;
            else 
            {
                Button2.Visible = false;
            }

PostedBy is the Id of the user who posted on website saved on jobs table. Problem is that button is not visibile when my statement should work.

The solution

if (!String.IsNullOrWhiteSpace(Request.QueryString["id"]))
            {
                int id = Convert.ToInt32(Request.QueryString["id"]);
                JobReqModel model = new JobReqModel();
                JobDescriptions job = model.GetJob(id);

                string clientId = Context.User.Identity.GetUserId();
                if (job.PostedBy == clientId)
                {
                    Button2.Visible = true;
                }

                else
                {
                    Button2.Visible = false;
                }
            }
6
  • Your posted code will only run on the initial load of the page (!IsPostBack) - is that your intention?
    – Tim
    Commented Jun 13, 2015 at 18:36
  • I used it to test but If you mentioned now I removed that statement, and I edited the code adding an else and the button is gone everywhere even when my statement should match. Commented Jun 13, 2015 at 18:42
  • You are creating a new instance of your JobDescription class. You aren't actually getting a job from the database to compare to your clientId..so whatever the default value of your JobDescription (most likely 0) probably doesn't match your clientId.
    – ethorn10
    Commented Jun 13, 2015 at 18:49
  • Thank you kind sir, I have solved the problem with help from your suggestion. I will edit in few seconds how I done if everyone get in trouble with this problem. Commented Jun 13, 2015 at 18:57
  • It is perfectly acceptable, and preferred at Stack Overflow, to add your solution as an answer and mark it as the solution. This is much more preferred than adding an edit to your question with the solution. Consider doing that and glad you got it working.
    – ethorn10
    Commented Jun 13, 2015 at 19:01

0

Browse other questions tagged or ask your own question.