Click here to Skip to main content
15,894,896 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
HI, Sorry for my English

I have created a dynamic table using c#, Now I want to select the table cells based on id and that table cells to change color when they are clicked using javascript. How can i achieve this??

Thanks.

What I have tried:

Here is my code behind:
private void CreateDynamicTable()
{

    int rowCnt;
    int cellCnt;
    int rowCtr;
    int cellCtr;

    Table Table1 = new Table();
    Table1.BorderWidth = 3;
    Table1.BorderStyle = BorderStyle.Solid;
    Table1.ID = "myTable";

    rowCnt = int.Parse(txtrow.Text);
    cellCnt = int.Parse(txtcol.Text);

    for (rowCtr = 1; rowCtr <= rowCnt; rowCtr++)
    {
        TableRow tRow = new TableRow();
        Table1.Rows.Add(tRow);
        for (cellCtr = 1; cellCtr <= cellCnt; cellCtr++)
        {
            TableCell tCell = new TableCell();
            tRow.Cells.Add(tCell);
            string prodID = rowCtr + "_" + cellCtr;

            TextBox txtbox = new TextBox();
            txtbox.Text = "Row:" + rowCtr + "  Column:" + " " + cellCtr;
            tCell.Controls.Add(txtbox);

            tRow.Controls.Add(tCell);
        }
        Table1.Rows.Add(tRow);
    }
    form1.Controls.Add(Table1);
}

HTML:
HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <link href="StyleSheet1.css" rel="stylesheet" />
    <title></title>
</head>
<body>
    
    <form id="form1" runat="server">
       <div>
            <table>
                Creating a dyanamic Table
                <tr>
                    <td>Row:     
                        <asp:TextBox ID="txtrow" placeholder="No of Rows Here" runat="server" AutoCompleteType="Disabled"></asp:TextBox>

                        <br />
                        <br />
                    </td>
                </tr>
                <tr>
                    <td>Coloum:
                        <asp:TextBox ID="txtcol" placeholder="No of Coloums Here" runat="server" AutoCompleteType="Disabled"></asp:TextBox>

                        <br />
                        <br />
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Button ID="Button1" Text="Create Table" runat="server" CssClass="button1" OnClick="OnClickOfCreateTable" />
                    </td>
                </tr>
            </table>
         </div>
       </body>
    </html>
Posted
Updated 7-Sep-18 19:46pm
v2

You should be able to easily do that with jQuery. First, define a css:

CSS
.highlight { background-color: green; }


then, you can do something like this:

JavaScript
$("#myTable tr").click(function() {
    var selected = $(this).hasClass("highlight");
    $("#myTable tr").removeClass("highlight");
    if(!selected)
            $(this).addClass("highlight");
});
 
Share this answer
 
Comments
Syf AK 7-Sep-18 6:27am    
Many thanks. @Vincent
try the below code:
HTML & javascript code:
<!DOCTYPE html>
<html>
<head id="Head1" runat="server">

    <title></title>

    <style>
        .highlight {
            background-color: red;
            color: white;
        }
    </style>
    <script>
        function abc(index) {
            var rr = document.getElementsByClassName("highlight");
            for (var i = 0; i < rr.length; i++) {
                rr[i].className = rr[0].className.replace("highlight", "");
            }
            var ss = document.getElementById(index);
            ss.className = "highlight";
        }

    </script>
</head>
<body>

    <form id="form1" runat="server">
        <div>
            <table>
                Creating a dyanamic Table
                <tr>
                    <td>Row:     
                        <asp:TextBox ID="txtrow" placeholder="No of Rows Here" runat="server" AutoCompleteType="Disabled"></asp:TextBox>

                        <br />
                        <br />
                    </td>
                </tr>
                <tr>
                    <td>Coloum:
                        <asp:TextBox ID="txtcol" placeholder="No of Coloums Here" runat="server" AutoCompleteType="Disabled"></asp:TextBox>

                        <br />
                        <br />
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Button ID="Button1" Text="Create Table" runat="server" CssClass="button1" OnClick="Button1_Click" />
                    </td>
                </tr>
            </table>
        </div>
    </form>
</body>
</html>


code behind code:
protected void Button1_Click(object sender, EventArgs e)
       {
           CreateDynamicTable();
       }

       private void CreateDynamicTable()
       {

           int rowCnt;
           int cellCnt;
           int rowCtr;
           int cellCtr;

           Table Table1 = new Table();
           Table1.BorderWidth = 3;
           Table1.BorderStyle = BorderStyle.Solid;
           Table1.ID = "myTable";

           rowCnt = int.Parse(txtrow.Text);
           cellCnt = int.Parse(txtcol.Text);

           for (rowCtr = 1; rowCtr <= rowCnt; rowCtr++)
           {
               TableRow tRow = new TableRow();
               Table1.Rows.Add(tRow);
               for (cellCtr = 1; cellCtr <= cellCnt; cellCtr++)
               {
                   TableCell tCell = new TableCell();

                   tRow.Cells.Add(tCell);
                   string prodID = rowCtr + "_" + cellCtr;



                   TextBox txtbox = new TextBox();
                   txtbox.Text = "Row:" + rowCtr + "  Column:" + " " + cellCtr;


                   txtbox.Attributes.Add("id", prodID);

                   txtbox.Attributes.Add("onfocus", "abc('" + prodID + "')");
                   tCell.Controls.Add(txtbox);



                   tRow.Controls.Add(tCell);
               }
               Table1.Rows.Add(tRow);
           }
           form1.Controls.Add(Table1);
       }
 
Share this answer
 
Comments
Syf AK 9-Sep-18 5:31am    
Than you very much.
while creating the dynamic table, just add the id to each td(cell) and add a javascript function and remove the highlight class and add the highlight class to current cell where you click. try the below code:


<html>
<head>
<style>
.highlight{
background-color:red;
color:white;
}
</style>
<script>
function abc(index){
var rr=document.getElementsByTagName("td");
for(var i=0; i<rr.length;i++){
rr[i].className=rr[0].className.replace("highlight", "");
}
var ss=document.getElementById(index);
ss.className="highlight";
}
</script>
</head>
<body>
<table >
<tr>
<td onclick="abc(1)" id="1" class="highlight">1</td>
<td onclick="abc(2)" id="2" class="highlight">vivek</td>
</tr>
<tr>
<td onclick="abc(3)" id="3" class="highlight">2</td>
<td onclick="abc(4)" id="4" class="highlight">Chaudhary</td>
</tr>
<tr>
<td onclick="abc(5)" id="5" class="highlight">3</td>
<td onclick="abc(6)" id="6" class="highlight">dk</td>
</tr>
</table>

</body>
</html> 
 
Share this answer
 
Comments
Syf AK 7-Sep-18 6:30am    
Thanks for the replay, i am generating table based on user inputs how can i add tds.
Syf AK 9-Sep-18 6:40am    
Can u please tell me how to retain a state of a dynamically created table across post back, my ui consists of two buttons. On clicking the second button the table is not retains its state. Same above example. @Member 10371658

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