locked
FormView code to hid button if username exist it table showing error when used on Datalist RRS feed

  • Question

  • User-2074858223 posted

    I tried to use this workable code used to hide button in FormView on DataList but got error on .Row.RowType, i think i got it wrong trying to modify it

    protected void Datalist1_DataBound(object sender, EventArgs e)
        {
            
    
               // string username8 = this.Page.User.Identity.Name;
                GetProfile7(username8);
            }
    
    
            if (Datalist1.Row.RowType == DataControlRowType.DataRow)
            {
                string username67 = (FormView1.Row.FindControl("lblUserName") as Label).Text;
                if (username67 == Session["userName"].ToString())
                {
                    (Datalist1.Row.FindControl("btnEdit") as LinkButton).Visible = true;
                }
                else
                {
                    (Datalist1.Row.FindControl("btnEdit") as LinkButton).Visible = true;
                }
            }
    
        }

    Thursday, January 25, 2018 11:16 AM

All replies

  • User475983607 posted

    Let's start with the basics.  What is the error?  Is it an exception or is the code not working as expected?

    A quick glance at the posted code shows that the link button is never hidden.  It is always set to true.   Use the Visual Studio debugger to verify your syntax.

    if (username67 == Session["userName"].ToString())
    {
    	(Datalist1.Row.FindControl("btnEdit") as LinkButton).Visible = true;
    }
    else
    {
    	(Datalist1.Row.FindControl("btnEdit") as LinkButton).Visible = true;
    }

    See the Visual Studio debugger docs at the following link.

    https://msdn.microsoft.com/en-us/library/y740d9d3.aspx

    Thursday, January 25, 2018 11:31 AM
  • User-2074858223 posted

    i did didnt see the error, i even put a brake line on if statement nothing still

     protected void GetMergedAll_ItemDataBound(object sender, DataListItemEventArgs e)
        {
    
            string usernameprof = (GetMergedAll.FindControl("lblUserName") as Label).Text;
                if (usernameprof == Session["userName"].ToString())
                {
                    (GetMergedAll.FindControl("btndelet") as LinkButton).Visible = true;
                }
                else
                {
                    (GetMergedAll.FindControl("btndelet") as LinkButton).Visible = false;
                }
            

    Sunday, January 28, 2018 10:00 AM
  • User475983607 posted

    Like many of your posts I suspect you are not posting the code that causes the error and you have not told what the error is.

    When using the debugger...

    • what is the values of usernameprof?
    • Does find control find the control; GetMergedAll.FindControl("lblUserName") as Label?
    • Does Session["userName"] have a vaue?  Is so what is it?
    • Does find control find the control, GetMergedAll.FindControl("btndelet") as LinkButton?

    Set aside the time to learn how to use the debugger.

    Sunday, January 28, 2018 2:01 PM