2011-03-17 74 views
0

如果在loginView之外,gridview可以正确显示!如何显示GridView内部使用数据绑定登录?

把里面的登录查看并使用下面的代码

<LoggedInTemplate> 
    <asp:GridView ID="GridView1" runat="server"> 
    </asp:GridView>   
</LoggedInTemplate> 

((GridView)LoginView1.FindControl("GridView1")).DataSource = query; 
((GridView)LoginView1.FindControl("GridView1")).DataBind(); 

Object reference not set to an instance of an object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

源错误:

Line 22: ((GridView)LoginView1.FindControl("GridView1")).DataSource = query;

如何显示内登录的GridView C#中的数据绑定?

+0

你试过了吗?* GridView1.DataSource = query; *?和* GridView1.DataBind(); *因为*((GridView)LoginView1.FindControl(“GridView1”))*为空。 – 2011-03-17 11:29:13

+0

在检查Request.IsAuthenticated可能在页面加载后尝试找到控件。 – Aravind 2011-03-17 11:54:49

回答

0

如果登录是应用程序中的按钮,那么在该代码文件的末尾,您必须编写并且必须进行一次查询才能选择该表。

事情是这样的:

SELECT * FROM [table_name]; // here, you can take table name which you want to bind 

在这之后,你必须通过数据集来填充DataAdapter的,以及该数据集是通过下面的代码约束:

ds = databind(); 
+0

这是来自ToolBox的LoginView,标准组件,在登录视图中,只有启用组角色才能查看 – jenifer 2011-03-17 12:02:03

+0

顺便说一下你在这里使用的qurry? – 2011-03-17 12:54:57

+0

为什么你需要这个? – 2011-03-17 13:15:52

0

<LoggedInTemplate />只是用户后可用登录。如果您尝试在用户登录之前访问网格,将发生NullReferenceException。我建议您添加一个这样的支票

if(Request.IsAuthenticated) 
{ 
    GridView gv = ((GridView)LoginView1.FindControl("GridView1")); 
    if(gv != null) 
    { 
     gv.DataSource = query; 
     gv.DataBind(); 
    } 

} 
相关问题