2014-11-04 63 views
0

我有一個網頁,帶我到另一個頁面,其中包含有關按學生ID搜索的學生信息。我有一個學生ID列表。我應該從第二頁中檢索學生信息並將其存儲在數據表中。這裏是代碼暫停循環,直到WebBrowser導航並加載文檔

string id { get ; set ;} 
int lCount; 
List<string> ids = new List<string>{"A1201", "A1202", "A1203"}; 
Datatable result = new Datatable(); 

private void button1_Click(Object sender, EventArgs e) 
{ 
    for(int i = 0 ; i < totalids ; i++) 
    { 
     lCount = i; 
     webbrowser1.Navigate("http://www.sksit.com"); 
     webbrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(browser_DocumentCompleted) 
    } 
} 

private void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) 
{ 
    if(e.URL.toString() == "http://www.sksit.com") 
    { 
     HtmlElement el = webbrowser1.Document.GetElementById("sid"); 
     el.SetAttribute("value", ids[lCount]); 

     HtmlElement cl = webbrowser1.Document.GetElementById("Search"); 
     cl.InvokeMember("click"); 
    } 
    elseif(e.URL.toString() == "http://www.sksit.com/studentprofiles") 
    { 
     HtmlElement el = webbrowser1.Document.GetElementById("studentinfotab"); 
     string info = el.InnerHtml; 
     string[] tableinfo = info.Split(' '); 
     DataRow trow = result.newDataRow(); 
     for(int j =0 ; j < tableinfo.Count() ; j++) 
     { 
     trow[j] = tableinfo[j]; 
     } 
     result.Rows.Add(trow); 
    } 
} 

當我看到執行後的結果表時,它顯示沒有數據。這個代碼工作正常,當一個單一的ID傳遞。當它被放入一個循環時,沒有看到數據,並且它顯示lCount爲2.請關注如何暫停循環直到它完成加載和解析。謝謝。

+0

你正在瀏覽相同的Web瀏覽器來下循環按鈕的客戶端事件totalids時間..它似乎是錯誤的 – 2014-11-04 19:16:23

+0

你跟Web瀏覽器控件綁定...而不是使用開源圖書館..專爲網絡報廢 – 2014-11-04 19:17:12

+0

我visted http://www.sksit.com,但沒有找到與「sid」元素.. – 2014-11-04 19:19:39

回答

0

嘗試類似這樣的事情。給你粗略的想法..

string id { get ; set ;} 
    int lCount; 
    List<string> ids = new List<string>{"A1201", "A1202", "A1203"}; 
    Datatable result = new Datatable(); 

    private void button1_Click(Object sender, EventArgs e) 
    { 

      lCount = 0; 
      webbrowser1.Navigate("http://www.sksit.com"); 
      webbrowser1.DocumentCompleted += new      WebBrowserDocumentCompletedEventHandler(browser_DocumentCompleted) 

    } 

    private void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) 
    { 
     if(e.URL.toString() == "http://www.sksit.com") 
     { 
      HtmlElement el = webbrowser1.Document.GetElementById("sid"); 
      el.SetAttribute("value", ids[lCount]); 

      HtmlElement cl = webbrowser1.Document.GetElementById("Search"); 
      cl.InvokeMember("click"); 
     } 
     elseif(e.URL.toString() == "http://www.sksit.com/studentprofiles") 
     { 
      HtmlElement el = webbrowser1.Document.GetElementById("studentinfotab"); 
      string info = el.InnerHtml; 
      string[] tableinfo = info.Split(' '); 
      DataRow trow = result.newDataRow(); 
      for(int j =0 ; j < tableinfo.Count() ; j++) 
      { 
      trow[j] = tableinfo[j]; 
      } 
      result.Rows.Add(trow); 

      //it should be something like this. 

      if(iCount < ids.Count){ 
      iCount ++; 
      webbrowser1.Navigate("http://www.sksit.com"); 
      } 

     } 
    } 
+0

感謝哥們。像Charm一樣工作。你是救世主! @ sm.abdullah – Kon 2014-11-05 15:43:53