Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

ASP.NET Behaviour of dynamically added controls

Options
  • 28-08-2006 1: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 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 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