2010-07-12 67 views
0

我正在嘗試返回用戶表並將其顯示在使用母版頁的頁面上。所謂AjaxMaster.Master當我嘗試抓取GridView以顯示數據時,GridView返回null

我的母版頁文件看起來像這樣 -

  <%@ Master Language="C#" AutoEventWireup="true" CodeBehind="AjaxMaster.master.cs" 
       Inherits="e.AjaxMaster" %> 

      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
      <html xmlns="http://www.w3.org/1999/xhtml"> 
      <head id="AjaxHead1" runat="server"> 
       <title></title> 

       <link rel="stylesheet" type="text/css" href="Global.css" /> 
       <style type="text/css"> 
        #backtoblog a 
        { 
         position: absolute; 
         top: 7px; 
         left: 15px; 
         text-decoration: none; 
        } 
       </style> 
       <asp:ContentPlaceHolder ID="ajaxhead" runat="server"> 
       </asp:ContentPlaceHolder> 
      </head> 
      <body> 
       <form id="ajaxform1" runat="server"> 

       <asp:ToolkitScriptManager ID="AjaxToolkitScriptManager2" runat="server" /> 
       <div> 
       </div> 
       <asp:LoginView ID="AjaxLoginView1" runat="server"> 
        <LoggedInTemplate> 
         <div id="companyLogo"> 
          <img src="images/company_logo.png" alt="Company Logo" border="0" width="123" border="0" 
           height="40" /> 
         </div> 
         <div id="header"> 
          <!-- start of main --> 

          <div id="welcome"> 
           <a href="Welcome.aspx">Welcome</a> <strong> 
            <asp:LoginName ID="LoginName1" runat="server" /> 
           </strong>, [ 
           <asp:LoginStatus ID="LoginStatus1" runat="server" LogoutAction="RedirectToLoginPage" /> 
           ] 
          </div> 
          <span id='sm_holder'></span> 
          <div class="clear"> 
          </div> 
          <% 

           string sPagePath = System.Web.HttpContext.Current.Request.Url.AbsolutePath; 
           System.IO.FileInfo oFileInfo = new System.IO.FileInfo(sPagePath); 
           string sPageName = oFileInfo.Name; 
          %> 

          <div> 
           <!-- start of TabContainer --> 
           <asp:TabContainer ID="TabContainer1" runat="server" ActiveTabIndex="1"> 
            <asp:TabPanel runat="server" HeaderText="Home" ID="TabPanel1"> 
             <HeaderTemplate> 
              <span> 
               <img src="iconimages/home001.png" alt="Home" border="0" height="10px" />&nbsp;Home</span> 
             </HeaderTemplate> 
             <ContentTemplate> 
              <!-- Start of Home --> 
              <% Response.WriteFile("HomeTab.aspx"); %> 
              <!-- End of Home --> 
             </ContentTemplate> 
            </asp:TabPanel> 
            <asp:TabPanel runat="server" HeaderText="Dashboard" ID="TabPanel7"> 
             <HeaderTemplate> 
              <p> 
               Dashboard</p> 
             </HeaderTemplate> 
             <ContentTemplate> 
              <!-- Start of Dashboard --> 
              <% Response.WriteFile("DashboardTab.aspx"); %> 
              <!-- End of Dashboard --> 
             </ContentTemplate> 
            </asp:TabPanel> 
            <asp:TabPanel ID="TabPanel2" runat="server" HeaderText="Locations"> 
             <HeaderTemplate> 
              <p> 
               Locations</p> 
             </HeaderTemplate> 
             <ContentTemplate> 
              <!-- Start of Locations --> 
              <% Response.WriteFile("LocationsTab.aspx"); %> 
              <!-- End of Locations --> 
             </ContentTemplate> 
            </asp:TabPanel> 
            <asp:TabPanel ID="TabPanel3" runat="server" HeaderText="Users"> 
             <HeaderTemplate> 
              <p> 
               Users</p> 
             </HeaderTemplate> 
             <ContentTemplate> 
              <!-- Start of Users --> 
              <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings: SiteSQL %>" 
               SelectCommand="SELECT * FROM aspnet_Users"></asp:SqlDataSource> 
              <asp:GridView ID="GridView1" runat="server"> 
              </asp:GridView> 
              <!-- End of Users --> 
             </ContentTemplate> 
            </asp:TabPanel> 
            <asp:TabPanel ID="TabPanel4" runat="server" HeaderText="Reports"> 
             <HeaderTemplate> 
              <p> 
               Reports</p> 
             </HeaderTemplate> 
             <ContentTemplate> 
              <!-- Start of Reports --> 
              <% Response.WriteFile("ReportsTab.aspx"); %> 
              <!-- End of Reports --> 
             </ContentTemplate> 
            </asp:TabPanel> 
            <asp:TabPanel ID="TabPanel5" runat="server" HeaderText="Alerts"> 
             <HeaderTemplate> 
              <p> 
               Alerts</p> 
             </HeaderTemplate> 
             <ContentTemplate> 

             </ContentTemplate> 
            </asp:TabPanel> 
            <asp:TabPanel ID="TabPanel6" runat="server" HeaderText="Preferences"> 
             <HeaderTemplate> 
              <p> 
               Preferences</p> 
             </HeaderTemplate> 
             <ContentTemplate> 
              <!-- Start of Preferences --> 
              <!-- start of left Panel for Preferences --> 
              <%-- <div id="leftColumn">--%> 

              <%-- </div>--%> 
              <!-- end of left Panel for Preferences --> 
              <!-- End of Preferences --> 
             </ContentTemplate> 
            </asp:TabPanel> 
           </asp:TabContainer> 
           <!-- end of TabContainer --> 

           <div class="clear"> 
           </div> 
           <div class="line"> 
           </div> 
           <div> 
            <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server"> 
            </asp:ContentPlaceHolder> 
           </div> 

           <div id="footer"> 

           </div> 
        </LoggedInTemplate> 
        <AnonymousTemplate> 
         You are not logged in. 
         <br /> 
         Please login to access eservice 
        </AnonymousTemplate> 
       </asp:LoginView> 
       </form> 
      </body> 
      </html> 

和代碼隱藏文件看起來像這樣 -

  using System; 
      using System.Collections.Generic; 
      using System.Linq; 
      using System.Web; 
      using System.Web.UI; 
      using System.Web.UI.WebControls; 
      using System.Data.SqlClient; 

      namespace e 
      { 
       public partial class AjaxMaster : System.Web.UI.MasterPage 
       { 
        protected void Page_Load(object sender, EventArgs e) 
        { 



         if (!IsPostBack) 
         { 
          BindUsersGrid(); 
         } 
        } 


        private void BindUsersGrid() 
        { 
         SqlConnection conn; 
         SqlCommand comm; 

         SqlDataReader reader; 

         string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["SiteSQL"].ConnectionString; 

         conn = new SqlConnection(connectionString); 


         comm = new SqlCommand("SELECT * FROM aspnet_Users", conn); 

         try 
         { 
          conn.Open(); 
          reader = comm.ExecuteReader(); 

          GridView gridviewusers = (GridView)AjaxLoginView1.FindControl("GridView1"); 



          gridviewusers.DataSource = reader; 

          gridviewusers.DataBind(); 


          reader.Close(); 


         } 

         finally 
         { 
          conn.Close(); 
         } 


        } 


       } 
      } 

它編譯好的,但是當我運行它,該行 -

GridView gridviewusers = (GridView)AjaxLoginView1.FindControl("GridView1"); 

返回空,下面的行會引發異常 -

  gridviewusers.DataSource = reader; 

      gridviewusers.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. 

      Source Error: 

      An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. 

      Stack Trace: 

      [NullReferenceException: Object reference not set to an instance of an object.] 
       eservice.AjaxMaster.BindUsersGrid() in AjaxMaster.Master.cs:115 
       eservice.AjaxMaster.Page_Load(Object sender, EventArgs e) in AjaxMaster.Master.cs:27 
       System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14 
       System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35 
       System.Web.UI.Control.OnLoad(EventArgs e) +99 
       System.Web.UI.Control.LoadRecursive() +50 
       System.Web.UI.Control.LoadRecursive() +141 
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627 


      Version Information: Microsoft .NET Framework Version:2.0.50727.3603; ASP.NET Version:2.0.50727.3614 

線115 AjaxMaster.Master.cs的是 -

gridviewusers.DataSource = reader; 
+0

哪裏是AjaxLoginView1在你的頁面的標記? – 2010-07-13 00:11:53

+0

之後的第5行。我不小心將它重命名爲MyLoginView1,但當我將其更改爲AjaxLoginView1時,我仍然面臨同樣的問題。 – Das 2010-07-13 00:15:40

回答

0

你可以試試this.Master.GridView1嗎? 我想你可以試試這個.Master.FindControl(「GridView1」)

-1

你並不需要使用的FindControl。您可以將其稱爲GridView1。

GridView1.DataSource = reader; 
GridView1.DataBind(); 

將填充GridView。

它爲空的原因是因爲您沒有指向AjaxLoginView1內部的GridView。在這裏發佈的代碼中,它是MasterPage的子代。

+0

如果我直接引用它,它的抱怨如下 - 名稱'GridView1'在當前上下文中不存在 – Das 2010-07-13 00:12:49

+0

因此,假設我有另一個名爲UsersGridView.aspx的頁面,那麼最好的方法是讓它顯示在上述情況下,用戶在選項卡中從該頁面生成的網格? – Das 2010-07-13 00:25:56

+0

無法通過名稱引用它,因爲它是Tabpanel中的容器 – 2010-07-13 15:59:48

0

經典的命名容器問題。 GridView不在當前上下文中,因爲它包含在選項卡面板中。 FindControl不是遞歸的。

嘗試TabContainer1.ActiveTab.FindControl( 「GridView1」)

+0

它說「名稱'TabPanel3'在當前上下文中不存在。 – Das 2010-07-13 14:14:29

+0

可能需要使用TabContainer1.ActiveTab.FindControl(」GridView1「) – 2010-07-13 15:58:42

相關問題