Using FindControl to find Gridview in LoginView's loggedInTemplate

It seems like it should be very simply and staight fforward, but I can figure it out.
I want to fill a gridview with info frrom a dataset for an authenticated user.
I put the gridview in the loggedintemplate of a loginview, but now I can't find it.
What is the proper way to use findcontrol in this situation?
mackeyrjAsked:
Who is Participating?
 
Juan_BarreraConnect With a Mentor Commented:
Hi there,
Can you please try the following:

Dim gv As GridView
If HttpContext.Current.User.Identity.IsAuthenticated Then
 Dim gv As GridView = CType(LoginView1.FindControl("GridViewId"),GridView)
End If

Open in new window

0
 
lorgeronCommented:
Hi!

Have you tried like this:
GridView GridView1 = (GridView)this.LoginView1.FindControl("GridView1");

Be aware, that you can only access controls, that are in the currently active template. You cannot access anything in the LoggedInTemplate, if no user is logged in.

(I hope i was clear, this is my first advice as an 'expert')
0
 
mackeyrjAuthor Commented:
Thank you. That worked near perfectly.
0
 
mackeyrjAuthor Commented:
You dimmed gv twice which was a minor problem. I removed the first one and changed the GridViewID to match the name of my Gridview.
0
 
Juan_BarreraCommented:
Yeah, sorry about that, just fast typing. I'm glad it helped otherwise. It should've been:

Dim gv As GridView
If HttpContext.Current.User.Identity.IsAuthenticated Then
  gv = CType(LoginView1.FindControl("GridViewId"),GridView)
End If

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.