2

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.

9
  • 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? Commented Feb 8, 2019 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. Commented Feb 8, 2019 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. Commented Feb 8, 2019 at 18:11
  • The main problem is I could not manage to get any values of the HTML table in C# Commented Feb 8, 2019 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. Commented Feb 8, 2019 at 18:17

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.