I am creating a web control by addin the html in the code behind, like this:
Dim sb As New StringBuilder
Dim myDiv As HtmlGenericControl = Me.theContainer
sb.Append("<table width='100%' cellpadding='0' cellspacing='0' style='border:0px;'>" & vbCrLf)
sb.Append(" <tr class='mainRow1'>" & vbCrLf)
sb.Append("<td align='left' style='border:0px solid white;'>Main Menu</td>" & vbCrLf)
sb.Append("<td>" & vbCrLf)
sb.Append("<asp:Label ID='Label1' runat='server' Text='Label'></asp:Label>" & vbCrLf)
sb.Append("</td>" & vbCrLf)
sb.Append("<td align='right' style='border:0px solid white;'>Sort By: Id | Name</td>" & vbCrLf)
sb.Append("</tr>" & vbCrLf)
sb.Append("</table>" & vbCrLf)
myDiv.InnerHtml = sb.ToString
This will generate the html for the table in the codebehind then inject it into the specified DIV element. My question is, if I create a control in this manner, how do I add an event that will fire a method in my codebehind? For example, if I want to create a image button I would do it this way in the code:
<asp:ImageButton CommandArgument='<%# (Container.DataItem.objectValue) %>'
ID='ImageButton1' runat='server' OnCommand='Button1_Click' ImageUrl='~/images/folder.gif' />
but in the code behind I would do this:
sb.Append("<input type='image' name='ctl00$ContentPlaceHolder1$importWizard1$folderControl1$folderRepeater$ctl04$ImageButton" & myCounter & "'")
sb.Append(" id='ctl00_ContentPlaceHolder1_importWizard1_folderControl1_folderRepeater_ctl04_ImageButton" & myCounter & "'")
sb.Append(" src='images/folder.gif' style='border-width:0px;' />" & vbCrLf)
My question is, considering that I am generating the control on the server, how do I add an “onCommand” event that would fire a specific method on the server? I’m not looking to just cause the page to reload. This functionality is in a user control (ascx file) that is in a update pannel. I just want the update pannel to post back, not the whole page. When I view the source of a image button, or any button, with an ONCommand event, it is acutally not even in teh code behind.
Any help would be great. Oh, I am developing with Visual studio 2005
Thanks
jason
if I try to add a server control like this:
Dim img As New ImageButton
img.Command = New CommandEventHandler(AddressOf Button1_Click)
img.ID = "button-" & item.directoryName & "-" & item.objectValue
Dim newSb As StringBuilder = New StringBuilder()
Dim tw As StringWriter = New StringWriter(newSb)
Dim hw As HtmlTextWriter = New HtmlTextWriter(tw)
img.RenderControl(hw)
the “img.Command ” gives me an error (blue squigley line) saying “Public Event Command(sender as object, e as webcontrols.commandEventArgs) is an event and cannot be called directly. use a raise event statement to raise an event”
Dim img As New ImageButton()
img.ID = "button-" & item.directoryName & "-" & item.objectValue
img.ImageUrl = "images/folder.gif"
AddHandler img.Click, AddressOf img_Click
Dim newSb As StringBuilder = New StringBuilder()
Dim tw As StringWriter = New StringWriter(newSb)
Dim hw As HtmlTextWriter = New HtmlTextWriter(tw)
img.RenderControl(hw)
sb.Append(tw.ToString)
Protected Sub img_Click(ByVal sender As Object, ByVal e As ImageClickEventArgs)
...
End Sub
Although this seem to render fine (when I view the source) the method is never actually called.
So, i figured it out and wanted to post in case anyone else is trying to do this:
on the client side, add this javascript function:
on the client side, add these controls:
On the client page, add this updatedate panel with triggers:
in the codebehind, add this handler:
last, in the codebehind, add an onclick even to call the js function.