Can I call a code behind function from a div onclick event?

前端 未结 3 948
余生分开走
余生分开走 2020-12-21 07:42

I have a bunch of divs on my page that are added at run time dynamically. When any of the dynamically added divs are clicked - All need to call the same function in my code

3条回答
  •  遥遥无期
    2020-12-21 08:07

    I'm not used to write VB code so my example is in C# but maybe it can help you get started. It's probably not the cleanest way to implement this, but i'll give it a try:

    HTML

     
    

    Client

    
    

    Server

    private void Page_Load(object sender, EventArgs e)
    {
       var arg = Request.Form["__EVENTTARGET"]; 'this will be empty on your first page request, but if the user click a div it will cause a postback to server, so this event will be fired again and will contain the div ID.
    
       if(arg != null)
       {
          string divID = (string)arg;
          'call your method with the argument.
       }
    }
    

    More information about this can be found here:

    http://wiki.asp.net/page.aspx/1082/dopostback-function/

提交回复
热议问题