How to run a server Side function on a Dynamic HTML Control

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • yonatan1234
    New Member
    • Jul 2010
    • 5

    How to run a server Side function on a Dynamic HTML Control

    Hello Everyone.
    I am trying to create an HTML button that can use a C# server side delete function. since I don't know how many results I will have at the page, the control is created dynamically when the end user searches for current Messages:

    Code:
      CmsContactUsContent += string.Format("
                "<input type='image' value='delete' id='del{8}' runat='server' onClick='DeleteItem' img src='/MaromyDotNet/img/AdminIcons/note.png' style='width:16px;'/>" +
                "</div>" +
                "<div style='clear:both'></div></div>", message.ContactMessageId);
    There is a Class called ContactMessage and I'd like at each iteraion to allow the user to delete the current message through this function:
    Code:
     public void DeleteItem(object sender, EventArgs e)
        {
            int idToDelete = 41; 
            BLLContactMessage.deleteMessageById(idToDelete);
        }
    However, the event does not fire. any ideas?
  • Alex Papadimoulis
    Recognized Expert New Member
    • Jul 2010
    • 26

    #2
    The event handler "DeleteItem " will not fire because you are not wiring it up to anything.

    What you are doing is appending a string to CmsContactUsCon tent (which I'm assuming is a string that gets assigned to something else), which will then be written out to the response stream in some manner.

    ASP.NET cannot or will not parse these strings for the same reason that typing "print 'Hello World';" into a textbox will not instruct the computer to do that. it's simply text.

    What you need to do is create a control (either dynamically or at design-time) that has this event wired up.

    Comment

    • yonatan1234
      New Member
      • Jul 2010
      • 5

      #3
      Thanks a lot.

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        yonatan1234,

        You should not be using regular HTML controls in an ASP.NET application because regular HTML controls do not have server side events that you can easily detect.

        Do not generate your HTML like this.
        Instead use a Repetaer control or a GridView control to specify a template (that uses .NET controls like the ImageButton control) so that you can handle Events generated by these controls in your server-side code.

        -Frinny

        Comment

        • yonatan1234
          New Member
          • Jul 2010
          • 5

          #5
          All right. Thanks a lot.

          Comment

          Working...