get gridview control inside loginview asp.net
Frage
I am working with loginview. There is a gridview inside loginview and I want to Bind it with databse table, but I couldn't access this gridview inside .cs code.
I tried this:
GridView GridView1 = (GridView)LoginView1.FindControl("GridView1");
but gridview again hasn't initialized. Note that in a web.config file I set
<authentication mode="Forms"/>
Does someone know the solution of this problem?
The part of code this:
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:LoginView ID="LoginView1" runat="server">
<LoggedInTemplate>
<div class="tab-content" id="mytabcontent">
<div class="tab-pane active" id="new1">
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</div>
</div>
</LoggedInTemplate>
<AnonymousTemplate>
<asp:HyperLink runat="server" navigationURL = "default.aspx">Login</asp:HyperLink>
</AnonymousTemplate>
</asp:LoginView>
</asp:Content>
Lösung
You may be getting this error as the user might not be authenticated.
Try
if(User.Identity.IsAuthenticated)
{
GridView GridView1 = (GridView)LoginView1.FindControl("GridView1");
}
Use Breakpoint and Check whether the the line to access Gridview is called.
UPdate:
To authenticate create a Auth Cookie
FormsAuthenticationTicket authTicket = new
FormsAuthenticationTicket(1, //version
username.ToString(), //user name
DateTime.Now, //creation
DateTime.Now.AddDays(365), //Expiration
false, ""
//Persistent
);
System.Web.Security.FormsAuthentication.RedirectFromLoginPage(username.ToString(), true);
// Redirect to Secured page after creating a cookie in login page
Andere Tipps
You could just handle the init event of the GridView:
<asp:GridView ID="GridView1" runat="server" OnInit="GridView1_Init">
</asp:GridView>
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow