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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

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