I have a help button with a special image I use throughout my project. Most of the time, I can just add it in the design view with:
<img alt="" src="Images/help.jpg" onclick="click_help('foo', 'bar')" class="myHelpClass" style="height: 20px; width: 33px" />
and I'll have a javascript script at the bottom of the page to handle click_help
. Everything to this part is working as intended.
Now, however, I have a dynamically created table with dynamic cells, and I need to add a help button to one of those cells. So far, I have this:
Dim help As New HtmlControls.HtmlImage
help.Src = "Images/help.jpg"
help.class = "myHelpClass"
help.Height = 20
help.Width = 33
AddHandler help.onClick, AddressOf helpClicked
cell.Controls.Add(help)
This throws 2 errors though:
'class' is not a member of 'System.Web.UI.HtmlControls.HtmlImage'.
'onClick' is not an event of 'System.Web.UI.HtmlControls.HtmlImage'.
I looked through the hierarchy on MSDN, and it looks like neither class
nor onClick
appear anywhere.
So why does the line from the design view above not throw any errors (in fact, they are fully functional, as I mentioned above - the onClick code is definitely working)?
How do I get the desired functionality with a dynamically created control?
It works in the design view because adding classes and onclick events are basic HTML markup, however these aren't properties of the corresponding HtmlControls.HtmlImage
object type in VB.Net.
For any attributes you want to add that aren't already members of the object, you can simply use myElement.Attributes.Add("attribute-name", "attribute value")
Dim help As New HtmlControls.HtmlImage
help.Src = "Images/help.jpg"
help.Height = 20
help.Width = 33
help.Attributes.Add("class","myHelpClass")
help.Attributes.Add("onclick","click_help('foo', 'bar');")
cell.Controls.Add(help)