2012-10-23 7 views
0

내 Default.aspx.cs 코드 숨김 내부에 탭으로 구분 된 텍스트 파일에서 읽은 데이터로 DataTable _dt를 채 웠습니다. Default.aspx에서 버튼을 다시 클릭하면 _dt의 모든 내용을 사용자에게 표시하려고합니다.form1 이름이 현재 컨텍스트에 없습니다.

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Threading; 
using System.Net; 
using System.Text; 
using System.Data; 
using System.IO; 

... 

     String strLine = String.Empty; 
     Int32 iLineCount = 0; 
     System.IO.StreamReader srdr = new System.IO.StreamReader(filePath); 
     do 
     { 
      strLine = srdr.ReadLine(); 
      if (strLine == null) 
      { break; } 
      if (0 == iLineCount++) 
      { 
       _dt = this.CreateDataTableForTabbedData(strLine); 
      } 
      this.AddDataRowToTable(strLine, _dt); 
     } 
     while (true); 
     form1.Controls.Add(_dt); 

하지만 VS는 다음과 같은 오류를 표시합니다. "이름이 현재 컨텍스트에 존재하지 않습니다." form1은 Default.aspx 내에 존재하지 않지만 Site.Master에는 존재합니다.

필자는 코드 숨김 기능을 사용하기 위해 Default.aspx 내에 form1이 있어야 할 필요가 있음을 알고 있지만, 그렇게하려고하면 Site.master에서 ID를 form2로 변경하면 오류 "페이지에는 서버 쪽 양식 태그가 하나만있을 수 있습니다." Site.Master에 양식을 보관해야하지만 Site.Master.cs에 테이블을 생성하는 코드 숨김을 이동할 수 없습니다.

+3

try this.Controls.Add (_dt); – Mate

+0

포럼 사이트와 달리 Google은 '감사합니다'또는 '도움을 주시면 감사하겠습니다'또는 [so]에 서명하지 않습니다. "[안녕하세요, '고마워,'태그 라인 및 인사말을 게시물에서 삭제해야합니까?] (http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be 참조) -removed-from-posts) –

+1

여기서'form1'이 정의되어 있다고 생각하십니까? –

답변

0

내 데이터를 위해 OleDbDataAdapter에 this tutorial을 사용하여 표시 얻을 : (여기 널 ;-)

를 확인하기 위해 기억 나는이 그런 물건을 처리하기 위해 썼다.

 DataSet dataset = Read_File_Into_Dataset("C:\\TEMP\\", "input.txt"); 
     DataGrid dg = new DataGrid(); 
     dg.DataSource = dataset; 
     dg.DataBind(); 
     this.Controls.Add(dg); 

     public DataSet Read_File_Into_Dataset(string fullpath, string file) 
     { 
     string sql = "SELECT * FROM `" + file + "`"; // Read all the data 
     OleDbConnection connection = new OleDbConnection // Connection 
      ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fullpath + ";" 
      + "Extended Properties=\"text;HDR=YES;FMT=Delimited\""); 
     OleDbDataAdapter ole = new OleDbDataAdapter(sql, connection); // Load the data into the adapter 
     DataSet dataset = new DataSet(); // To hold the data 
     ole.Fill(dataset); // Fill the dataset with the data from the adapter 
     connection.Close(); // Close the connection 
     connection.Dispose(); // Dispose of the connection 
     ole.Dispose(); // Get rid of the adapter 
     return dataset; 
     } 
+1

좋아요! "this"를 사용하는 것이 거의 필수적이라는 것을 알 수 있습니다 : P – Mate

0

페이지 컨트롤 (귀하의 this 또는 this.Page)에 마스터 자체에 대한 참조이므로 컨트롤 컬렉션을 탐색하여 마스터의 하위 컨트롤을 찾아야합니다. 그 중 하나가 양식입니다. 고유 한 것이 보장되지는 않으며 분명히 M * N의 순서대로되어 있지만 오랫동안 저를 위해 트릭을했습니다.

당신은 너무처럼 사용합니다 :

제어 oCtl = SomeUtilityClass.FindControlRecursive (this.Page, "Form1을"); 내가 할 수 있었다

/// <summary> 
     /// Recursive loop to find a control 
     /// </summary> 
     /// <param name="rootCtl">Control whose control collection we will begin traversing in search of the 
     ///  given ID</param> 
     /// <param name="Id">ID to search for</param> 
     /// <returns></returns> 
     public static Control FindControlRecursive(Control rootCtl, string desiredCtlID) 
     { 
      //Make sure this thing exists 
      if (rootCtl == null) 
       return null; 

      //See if it's the one we're after 
      if (rootCtl.ID == desiredCtlID) 
       return rootCtl; 

      //Make sure it has controls to loop over 
      if (!rootCtl.HasControls()) 
       return null; 

      foreach (Control oCtl in rootCtl.Controls) 
      { 
       Control FoundCtl = FindControlRecursive(oCtl, desiredCtlID); 
       if (FoundCtl != null) 
        return FoundCtl; 
      } 

      return null; 
     } 
+0

내 Site.Master.cs에 귀하의 메서드를 넣고 내 Default.aspx.cs'Control oCtl = SiteMaster.FindControlRecursive (this.Page, "form1"); oCtl에 다음 코드를 사용했습니다. Controls.Add (_dt);'하지만 VS는 "System.Da에서 변환 할 수 없습니다"라는 오류 메시지를 표시합니다. ta.DataTable 'to'System.Web.UI.Control '- 다른 조언은 무엇입니까? – Krondorian

+0

DataTable이 컨트롤이 아닙니다. Controls 컬렉션에 추가하려고하는 이유는 무엇입니까? –

+1

@ John이 맞습니다. GridView 또는 기타 데이터 바인딩 된 컨트롤을 만들어 데이터 테이블에 데이터 소스를 설정 한 다음 DataBind() 및 그리드 뷰를 호출 해 볼 수 있습니다. – fordareh

관련 문제