Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

ASP.NET Behaviour of dynamically added controls

  • 28-08-2006 12:24PM
    #1
    Closed Accounts Posts: 80 ✭✭


    I have code which loops through and IDataReader adding a checkbox to a tablecell for each record. I need to have the tablecells change color when the checkboxes are checked and unchecked. Preferrable without a roundtrip to the server.

    C# code
    	while (data.Read())
    	{
    		.......
    
    		for (cellCtr = 1; cellCtr <= cellCnt; cellCtr++) 
    		{
    			TableCell tCell = new TableCell();
    			tCell.RowSpan = 1;
    			tRow.Cells.Add(tCell);
    
    			CheckBox cb = new CheckBox ();
    			cb.Checked = false;
    			cb.Text = Field1.ToString();
    			cb.ID = Field1.ToString();
    			cb.ToolTip = Field2.ToString();
    	
    			//Add the description of the value
    			tCell.Controls.Add (cb);
    		}
    	}
    

    Thanks in advance. I am relatively inexperienced in C# and have not been able to figure this one out.


Comments

  • Registered Users, Registered Users 2 Posts: 7,468 ✭✭✭Evil Phil


    IDataReader - Is it a custom object? If its a DataReader object (or something that implements IEnumerable) then you could bind it to a datagrid and use column templates for the checkboxes and include some javascript in the html to change the background of the cell.


  • Registered Users, Registered Users 2 Posts: 640 ✭✭✭Kernel32


    Why not use a datalist or something. Thats what the controls are for.


  • Closed Accounts Posts: 80 ✭✭jimbozo


    Thanks for the suggestions guys....I got this from a friend:

    Add a JavaScript client-side event handler to each cell?
    // Server-side:
    cb.Attribute.add("onclick","changeColor(this.parent)")

    // Then put this in the <head> of your page:
    <script type="text/javascript">
    function changeColor(cell)
    {
    oldBgColour = '#000000'; // default colour
    newBgColour = '#FFFFFF'; // highlight colour
    if (cell.style.backgroundColor == oldBgColour)
    {
    cell.style.backgroundColor = newBgColour;
    }
    else
    {
    cell.style.backgroundColor = oldBgColour;
    }
    </script>

    This assumes the parent of the checkbox is the cell.

    </end>

    Anyway it works apart from the Javascript which I am tweaking and I will post it when I have finished. Cheers Jim.


Advertisement