jeudi 19 mai 2016

Identifying cell value which occurs more than once in a GridView using Javascript

I have a quite challenging task that I am trying to achieve and have spent a long time on trying to do this. I have an asp.net web form which contains an asp.net GridView with the follwing columns:

Profile ID - this column is currently populated with id's queried from sql server database.

User Id - This column has checkboxes which allow the user to select the checkbox corresponding to the ID column.
Example of my gridview is on the link below.

What I am trying to do is, the profile ID's which occur more than once in the table (If there are duplicate ID's in the table) the checkbox from the user Id column can only be checked once. For instance if the first 2 rows consist of the same Id's, only the first checkbox on User Id can ticked out of the 2 checkboxes with the same profile Id's. I want to only tick one of out the remaining Id's if there are duplicates as the profile Id table may consist of same Id's occuring more than once.

I am trying to do this on client-side using javascript. At the moment I am only able to loop through the rows in Profile Id column and get the cell values of that column using javascript. Now i will need to find out a way of identifying the profile ID's which occur in that column more than once and then only allow one checkbox from the userId column to be checked corresponding to profile Id if there is a duplicate Id, other wise the user can select a checkbox.

Below is some of my code that I have started which can only loop through and get profile Id cell values.

<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1"
            AutoGenerateColumns="False" AlternatingRowStyle-CssClass="alt"
            OnSelectedIndexChanged="GridView1_SelectedIndexChanged" CellPadding="4">

 <asp:BoundField DataField="Profile_ID" HeaderText="Profile_ID" SortExpression="Profile_ID" />

 <asp:templatefield HeaderText="User Id">           
    <itemtemplate>
    <asp:checkbox ID="userid_select" runat="server"></asp:checkbox>
    </itemtemplate>
    </asp:templatefield>

Javascript code

function display() {
var table, tbody, i, rowLen, row, j, colLen, cell;

table = document.getElementById("GridView1");
tbody = table.tBodies[0];

for (i = 0, rowLen = tbody.rows.length; i < rowLen; i++) {
    row = tbody.rows[i];

    for (j = 0, colLen = row.cells.length; j < colLen; j++) {
        cell = row.cells[j];
        if ( j == 1 ) {
            alert("profile ID    " + cell.innerHTML);  
      }
  } 
  } 
}

Apologies if I have made it sound difficult. Im finding this quite difficult to achieve and would appreciate any feedback. Thank you

my gridview




Aucun commentaire:

Enregistrer un commentaire