Results 1 to 8 of 8

Thread: Add event handler to control

  1. #1
    Hyperactive Member jasonwucinski's Avatar
    Join Date
    Mar 10
    Location
    Pittsburgh
    Posts
    443

    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&#37;'  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
    if i was able to help, rate my post!

  2. #2

  3. #3
    Hyperactive Member jasonwucinski's Avatar
    Join Date
    Mar 10
    Location
    Pittsburgh
    Posts
    443

    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
    if i was able to help, rate my post!

  4. #4
    Fanatic Member
    Join Date
    Jun 04
    Location
    All useless places
    Posts
    916

    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

  5. #5
    Fanatic Member
    Join Date
    Jun 04
    Location
    All useless places
    Posts
    916

    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.

  6. #6
    Frenzied Member brin351's Avatar
    Join Date
    Mar 07
    Location
    Land Down Under
    Posts
    1,256

    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.
    The problem with computers is their nature is pure logic. Just once I'd like my computer to do something deluded.

  7. #7
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 04
    Location
    The Granite City
    Posts
    21,744

    Re: Add event handler to control

    Quote Originally Posted by brin351 View Post
    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

  8. #8
    Frenzied Member brin351's Avatar
    Join Date
    Mar 07
    Location
    Land Down Under
    Posts
    1,256

    Re: Add event handler to control

    Quote Originally Posted by gep13 View Post
    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.
    The problem with computers is their nature is pure logic. Just once I'd like my computer to do something deluded.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •