hi All,
I m new to c#.I m using a HTML table in web browser control and wants to get the rowid of the clicked row in HTML dont have any idea how to get.
IS there any method for getting the row id of the clicked cell.
Please help me in solving the problem thanx in advance.

Dani AI

Generated

Quick summary and options for : if the HTML is under your control the easiest, most robust approach is to add an id (or a data-* attribute) to each <tr> and read that when the user clicks. If you cannot change the HTML, either (A) attach DOM click handlers from C# to each <tr> and read the row element on click, or (B) inject a small JavaScript click handler that calls back into C# via window.external. If your scenario is data-driven, is correct that server-side controls (GridView, etc.) are usually a better fit; ’s Document iteration shows the right idea for reading cells, but a click handler gives a cleaner UX.

Attach-from-C# (safe, no JS required): wire this after the page finishes loading (DocumentCompleted), then attach a click handler to every tr and climb the DOM to the TR/TABLE to read id or compute an index.

private void webBrowser1_DocumentCompleted(object s, WebBrowserDocumentCompletedEventArgs e)
{
    var doc = webBrowser1.Document;
    if (doc == null) return;
    foreach (HtmlElement tr in doc.GetElementsByTagName("tr"))
    {
        HtmlElement row = tr;               // capture per-iteration
        row.Click += new HtmlElementEventHandler(Row_Click);
    }
}

private void Row_Click(object sender, HtmlElementEventArgs e)
{
    HtmlElement tr = sender as HtmlElement;
    while (tr != null && !tr.TagName.Equals("TR", StringComparison.OrdinalIgnoreCase))
        tr = tr.Parent;
    if (tr == null) return;
    string id = tr.GetAttribute("id");    // use if present
    // else find parent TABLE and enumerate TRs to get index
}

Do this after DocumentCompleted so the DOM is ready. (learn.microsoft.com)

Inject-from-JS (simple if you can edit the page): give each tr an onclick that calls window.external.RowClicked(idOrIndex), and in C# expose a COM-visible object via webBrowser.ObjectForScripting to receive the call (your bridge class must be [ComVisible(true)]). Example JS and a tiny C# bridge are straightforward to add. (learn.microsoft.com)

If you prefer a single document-level handler, you can use the document click and GetElementFromPoint to resolve the clicked element and then climb to the TR. (learn.microsoft.com)

Tips: reattach handlers if the page mutates, suppress or centrally handle script errors when injecting JS, and prefer row id or data-* attributes when you can (they make the C# side trivial).

Recommended Answers

All 7 Replies

if you used GridView, DataList, .... controls you can bind the data to them and get the value of something if you clicked (or take a specific action) on specific row.
but html table used for alignment and design requirements...

oooh, that's ASP.NET not HTML, if you need to learn asp.net go to www.asp.net you'll find all resources to be professional web developer ;)

No Sir,
I have only Html files and i have to access it in c# sothst i can use that module in my application.
isn't there any method in c# by which i can get the clicked row's Id.
Please help.

how can u got the data inside the tables (access db) by html, if you could by Java Script you can't identity the rows let me give you example
1
2
5
6
7
can u give a way if you pressed on number 6 tell you that u clicked on the 4th row??!!

u've raw data without any specific structure so you can't make use else read it.

Sorry Sir,
i m not accessing Access tables ,in fact i m using tables created in Html using<table>,<tr>,<td > tags.
I think u misuderstood me.
Am i right????????

yes, I am; but my answer for what I understood and what you mean.
else you in each row have a value you assign. HTML == static

Hi manjusaharan,
I think I understand exactly what you are going to do
I write this code for you and checked. I am sure it is going to help you.

wb is a web browser component

   foreach (HtmlElement  htmlobj in  wb.Document.All)
        {
            if (htmlobj.TagName =="TABLE")
                foreach (HtmlElement htmlTableTD in htmlobj.Document.All)
                {
                    if (htmlTableTD.TagName == "TD")
                        MessageBox.Show(htmlTableTD.OuterText);
                    break;
                }
            break;
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.