-
server control events
I have a table in place on a page, and I am adding controls
to it with code in the Page_Load sub, similar to this:
Dim ttRow As New TableRow()
Dim ttCell As New TableCell()
Dim ttButton As New Button()
tbltt.Rows.Add(ttRow)
tbltt.Rows(0).Cells.Add(ttCell)
tbltt.Rows(0).Cells(0).Controls.Add(ttButton)
this works fine, but the problem is that I don't know how to
get access to the click event of the Button control.
Is it do-able, and if so, how?
-
Hmm,
Interesting situation... I get the feeling you're adding dynamic controls because you're displaying a list of records or something from a database or some other datasource...
If this is the case, perhaps the ASP Table may not be the right control you are looking at for the job... You might want to take a look at the datalist control... It allows you to define a template for each item , and a header and footer template, and more.... In the item template, you can put controls, like buttons, etc, and get their click event...
I'd really need to see some more information of what exactly your objective is... Maybe we can suggest from there... but to do it the way you're trying right now, i'm not sure how you would go about finding it's click event, or if it is even possible... so post back with some more information on your situation, and we'll go from there! :)
-
Thanks for your response. I have considered using a table for displaying my data. However I was hoping to be able to use a different color background based on the content of the data. The application that I'm writing is a sports equipment rental site.
I want to have a display for every hour of the day, and indicate with that display what the status of a given item is at that time using a color coding system. Unfortunatly, I don't believe that it's possible to set the color of a data row dynamically, based
on the data that is in that row. If I can't accomplish my objective by creating controls on the fly, then I'll have to reevaluate and go with another approach.
-
To set the background color of cell 3 of the current row in the sending datagrid using the ItemDataBound event handler:
e.item.cells(2).Attributes.Add("style", "background-color: red;")
-
You can add an event handler for the click event of the button you describe, but it will never be called.
This is because the button goes out of scope after being written to the page. Then, when clicked, the server does not know what element was just clicked and cannot properly call your event handler.
Adding event handlers to runtime created components is for VB Applications programming or adding event handlers to controls that were created at design time, not run time.