1

I have an aspx page including three HTML tables. One of the tables in HTML is shown below:

<table id='example' >
   <thead>
      <tr>
         <th><INPUT type="checkbox" onchange="checkAll(this)" name="chk[]"/></th>
         <th>SchoolID</th>
         ...
      </tr>
   </thead>
   <tr>
      <td>
         <input type='checkbox' name='name0' />
      </td>
      <td>200116</td>
   </tr>
   <tr>
      <td>
         <input type='checkbox' name='name1' />
      </td>
      <td>200116</td>
   </tr>
   ...
</table>

This table looks like:

enter image description here

What I need is to get the SchoolID for the rows that are checked by the end-user in C#. What is the best way to do that?

I am currently trying to use HTMLAgilityPack to get my HTML table as Datatable, but I am not successful. It is getting the first table, I need the third one. And yet, I am not sure if it is the best way. Please advise.

var doc = new HtmlDocument();
//doc.Load(Request.Url.ToString()); //gives error.
doc.Load(filepath); //this works

//this is returning me the first table of my page, i need the third table.
var nodes = doc.DocumentNode.SelectNodes("//table/tr");

var table = new DataTable("exampleDataTable");

Any help would be so appreciated!

EDIT: Turned out I cannot use HTMLAgilityPack since I am actually creating the table in C# then sending to front-end. Using filepath is not working because there is no table in the .aspx file by default.

EDIT2: Sec, there is web.load function I have found, maybe I can use Request.Url.

Eray Balkanli
  • 7,752
  • 11
  • 48
  • 82
  • Please [edit] your question to remove the text (and tag) about HTMLAgilityPack if you can't use it. Are you in control of the code that renders those tables? – Heretic Monkey Feb 08 '19 at 18:08
  • @HereticMonkey I am not sure if I can use it or not yet. Yes I am in control of code renders all those tables. 2 tables are in aspx already, they are static. Third of them is generated by C# code. – Eray Balkanli Feb 08 '19 at 18:09
  • I would just add a `value` attribute to the checkboxes whose value is the same as the School ID. Then, when they are submitted with the form, you'll have the values of the checked items. You may want to name them all something like `chosenSchoolId[]` so that you get an array in your action. – Heretic Monkey Feb 08 '19 at 18:11
  • The main problem is I could not manage to get any values of the HTML table in C# – Eray Balkanli Feb 08 '19 at 18:12
  • I'm not sure I understand that statement. ASP.NET works by posting form values back to the server. If those tables are contained in a form, and that form is posted back to the server, then the values of the checked boxes will be transmitted to the C# in the code-behind of the page. – Heretic Monkey Feb 08 '19 at 18:17
  • I could not find the syntax to get an HTML table in C#.
    myHTMLtable
    . How to get its rows in C#, that's the main thing I need.
    – Eray Balkanli Feb 08 '19 at 18:23
  • You question makes it sound like you need the checked values, not the table. – Heretic Monkey Feb 08 '19 at 18:23
  • After I manage to reach the table I will need to understand which values are checked, I can struct a for O(n) for loop and see the first column of each row if Checked or not I believe – Eray Balkanli Feb 08 '19 at 18:25
  • What are you going to do with the values? Because what I was saying would get you something like `chosenSchoolId=200116,200517`. Sounds like you're making this a lot harder than it needs to be. See [the XY Problem](http://xyproblem.info). – Heretic Monkey Feb 08 '19 at 18:27

1 Answers1

1

You can get SchoolIDs of selected rows via jQuery in your submit button's click() listener.

var tableControl = document.getElementById('mytable');
$('#myBtn').click(function() {

var arrayOfValues = []
    $('input:checkbox:checked', tableControl).each(function() {
            arrayOfValues.push($(this).closest('tr').find('td:last').text());
        }).get();
    alert(arrayOfValues);
});


$('#chkAll').change(function() {
  $('input:checkbox').not(this).prop('checked', this.checked);
});

Working Demo: JSFiddle

Onur A.
  • 3,007
  • 3
  • 22
  • 37