Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
509 views
in Technique[技术] by (71.8m points)

asp.net - execute serverside code on div click

ASPX CODE

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

ServerSide Code

Public Sub loadContentFamily(ByVal PageName As String)
        MsgBox("")
        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

        Try
            _con.Open()
            _da.Fill(_table)
            _con.Close()
            _con.Dispose()
            With _table.Rows(0)
                h4header.InnerText = .Item(0)
                divUpdatePanel.InnerHtml = .Item(1)
                Me.Title = .Item(0)
            End With

        Catch ex As Exception
            MsgBox(ex.Message)
            divUpdatePanel.InnerText = "No Data Found"
        Finally
            _con.Close()
            _con.Dispose()
        End Try
    End Sub

PROBLEM:

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...