Click here to Skip to main content
15,913,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi everybody
i have windows form where there is webbrower control which read aweb page this page have html table which have id and there are fouur td each one have atext and link how can i get each td and pass them to textbox on td value and linkbutton on a value i have done this simple code

C#
var theElementCollectionChildNext2 = webBrowserwelcome.Document.GetElementsByTagName("table");
         foreach (HtmlElement elChild in theElementCollectionChildNext2)
         {

             if (elChild.Id == "Table4")
             {
                 var theElementCollectionChildNext3 = webBrowserwelcome.Document.GetElementsByTagName("td");
                 foreach (HtmlElement elChild2 in theElementCollectionChildNext3)
                 {

                     // i need to pass each td value to textbox

                 }
             }


what is the correct syntax to be done

What I have tried:

var theElementCollectionChildNext2 = webBrowserwelcome.Document.GetElementsByTagName("table");
foreach (HtmlElement elChild in theElementCollectionChildNext2)
{

if (elChild.Id == "Table4")
{
var theElementCollectionChildNext3 = webBrowserwelcome.Document.GetElementsByTagName("td");
foreach (HtmlElement elChild2 in theElementCollectionChildNext3)
{

// i need to pass each td value to textbox

}
}
Posted
Updated 23-May-16 11:03am
Comments
Karthik_Mahalingam 23-May-16 13:57pm    
what is the table structure ?
post the mark up for table.

Assume that your table structure is like this
HTML
<table id="Table4">
        <tr>
            <td>1</td>
            <td>2</td>
            <td>3</td>
            <td>4</td>
        </tr>
    </table>

then you will have to code as below to assign the values to the respective text box, i would recommend you to use break point[^] and debugger [^]to play around with the object and properties getting in the loop, based on that you can customize your code which suits your need.

C#
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
       {
          HtmlElement table =  webBrowser1.Document.GetElementById("table4");
          var rows = table.GetElementsByTagName("tr");
          foreach (HtmlElement row in rows)
          {  int i = 0;
              foreach (HtmlElement cell in row.GetElementsByTagName("td"))
              {
                  i++;
                  string value = cell.InnerText;
                  if (i == 1)
                      textBox1.Text = value;
                  if (i == 2)
                      textBox2.Text = value;
                  if (i == 3)
                      textBox3.Text = value;
                  if (i == 4)
                      textBox4.Text = value;

              }
          }


       }


Since you knew the Id of the table, you can get the Table Element by using GetElementById instead of TagName
 
Share this answer
 
Comments
MR.alaa 23-May-16 16:06pm    
this is the structure
<table id="Table4">
<tr>
<td>1link1</td>
<td>2link2</td>
<td>3</td>
<td>4</td>
</tr>
</table>

not all have links
Karthik_Mahalingam 23-May-16 16:18pm    
what is link1 ?
post the complete markup of the table..
if link1 is a text, then the above code should work fine..
Karthik_Mahalingam 23-May-16 16:28pm    
put it inside code tag code
click edit and view the code...

then the above code will work fine..
what is link button ?
i use this and works fine

C#
var theElementCollectionChildNext2 = webBrowserwelcome.Document.GetElementsByTagName("a");
                      foreach (HtmlElement elChild in theElementCollectionChildNext2)
                      {

                          if (elChild.Id == "HyperLink4")
                          {
                              hyperlinkLabelControl1.Text = elChild.GetAttribute("href");
                          }

                          else if (elChild.Id == "HyperLink5")
                          {
                              hyperlinkLabelControl2.Text = elChild.GetAttribute("href");
                          }

                          else if (elChild.Id == "HyperLink6")
                          {
                              hyperlinkLabelControl3.Text = elChild.GetAttribute("href");
                          }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900