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

I have a bunch of divs on my page that are dynamically added at runtime. When you click any of the dynamically added divs - everyone needs to call the same function in my code. Each of the divs must pass its own function identifier. I can’t use web methods because the function needs to identify which div was clicked and then show / hide / fill other controls on the page.

Greetings guys

          Header controls and more        
    <div id="div_Footer" class="HoverEdit" title="Click To Edit" runat="server" onclick="EditDiv(div_Footer)">
        Footer Controls and stuff go here
    </div>

and then in the code behind:

Sub EditDiv(ID_ofDiv As String)

    'Do some stuff to the controls on the page
    'Swapping tabs, showing /hiding controls etc.

End Sub
+3
source share
3 answers

VB, #, , , . , , :

HTML

 <div id="div_Footer" class="HoverEdit" title="Click To Edit" runat="server" onclick="EditDiv(this)">
        Footer Controls and stuff go here
    </div>

Client

<script type="text/javascript">
function EditDiv(s,e){
var id = $(s).attr("id");
__doPostBack(id,id);
}
</script>

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.
   }
}

:

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

+3

, @Tim Schmelter, . :

:

<div id="div1" runat="server" style="height:100px;width:100px;">click me</div>

:

public class MyPage  
      Inherits System.Web.UI.Page
      Implements IPostBackEventHandler

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    div1.Attributes("onclick") = 
         Me.Page.ClientScript.GetPostBackEventReference(Me, "div1_clicki")
End Sub

Protected Sub Div1_Click()
    'do your stuff
    div1.InnerHtml="div1 clicked"
End Sub


'Private Members As IPostBackEventHandler
Public Sub RaisePostBackEvent1(ByVal eventArgument As String) Implements 
                    IPostBackEventHandler.RaisePostBackEvent
    If eventArgument = "div1_click" Then
            div1_click()
        End If
    End If
End Sub
0

You can create a postback from javascript. One way is to create a button or link and add a click event to it. Add Style = "Display: none;" and force the Div to the reverse gear at the click of a button.

<div id="div_Footer" class="HoverEdit" title="Click To Edit" runat="server" onclick="EditDiv(this)">
    Footer Controls and stuff go here
</div>
//Javascript
function EditDiv()
{
   // do your code
   __doPostBack('ButtonAID','')
}

A good explanation can be found in the next article. ASP.NET postback with JavaScript

0
source

All Articles