Add event handler to control
Hello everyone,
I am creating a web control by addin the html in the code behind, like this:
Code:
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:
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:
Code:
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)
But I cant add the OnCommand because it wont be rendered properly.
Thanks for your help
jason
Re: Add event handler to control
Re: Add event handler to control
Hi,
Thanks for the reply. I'm not sure I understand how this will help. The article seems to be for how to create a control and add it to your client side code. I want to create the html code in the codebehind and add the appropriate event.
So, if in my codeBehind i create the control like this:
Code:
Dim sb As New StringBuilder
Dim myDiv As HtmlGenericControl = Me.theContainer
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)
myDiv.InnerHtml = sb.ToString
how do I add the "onClick" event that will call the method. I use a loop to create these controls, so all controls will call the same methdod but pass different parameters.
Thanks for your help
jason
Re: Add event handler to control
You may want to look at __doPostBack using javascript.
This article may help: http://www.codedigest.com/Articles/A...in_AspNet.aspx
Re: Add event handler to control
Looked a bit more at your code, and seems like you may want to look at this too: http://geekswithblogs.net/mnf/archiv.../04/59081.aspx
This is for the $ sign that ASP.Net puts in control names and you have that in the string you are building.
Re: Add event handler to control
Is there a reason why you can NOT create the controls dynamically in code?
Rendering them in a html string seems like bad practice to me.
Re: Add event handler to control
Quote:
Originally Posted by
brin351
Rendering them in a html string seems like bad practice to me.
This is pretty much what all the ASP.NET Server Controls do behind the scenes though.
Gary
Re: Add event handler to control
Quote:
Originally Posted by
gep13
This is pretty much what all the ASP.NET Server Controls do behind the scenes though.
Gary
True but this is before the render event, you can't make a refrence to the controls in code and seems counter intuative. I asked why do this because there must be a better way to achive the same result.