user1150440 user1150440 - 1 year ago 84 Question

execute serverside code on div click


<div id="c" runat="server" onclick="loadContentFamily" onmousedown="loadContentFamily" rel="divUpdatePanel" >Family</div>

ServerSide Code

Public Sub loadContentFamily(ByVal PageName As String)
PageName = "Family"
Dim _con As New SqlConnection(ConfigurationManager.ConnectionStrings("LeaveDBConnectionString").ConnectionString)
Dim _da As New SqlDataAdapter("SELECT PageHeader,PageContent FROM PageKeeper WHERE PageName='" & PageName & "'", _con)
Dim _table As New DataTable

With _table.Rows(0)
h4header.InnerText = .Item(0)
divUpdatePanel.InnerHtml = .Item(1)
Me.Title = .Item(0)
End With

Catch ex As Exception
divUpdatePanel.InnerText = "No Data Found"
End Try
End Sub


When i click on the div it does not execute the ServerSide Code ...why??Any help appreciated.

Answer Source

You need to raise a server event, not client event (and it looks like you want to do that asynchronously).

This article explains how to attach a snippet of code to a client element (like a DIV) and cause an asynchronous postback.

Another quick and dirty way of accomplishing this is to use a hidden server button. The button can be located wherever desired (such as inside an UpdatePanel) and it allows proper usage of the ASP page lifecycle.

<asp:Button runat="server" id="btnPostback" style="display:none" onclick="serverEventHandler" />

<div onclick="document.getElementById('<%= btnPostback.ClientID %>').click()">Clickable DIV</div>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download