Saturday, March 31, 2012

Server side Event handler for DIV click?

Is it possible with ASP.NET C# to establish a click event on a DIV element so that when the user clicks on the DIV it can postback to the server and initate a click event for that DIV element? I am currently wrapping my content with a <A runat=server> to achieve the onserverclick effetc.

In fact if i wanted to create a server side event hanler for dynamic content, how would one go about establishing this. I can see .NET makes heavy use of __doPostBack on the client side but i just dont see how it can initate an event handler on the server side.

Tx

Hi quantass0,

You can handle the click event on a DIV as the code following (div1 is the id of the DIV element)

public

partialclass_Default : System.Web.UI.Page,IPostBackEventHandler

{

protectedvoid Page_Load(object sender,EventArgs e)

{

div1.Attributes[

"onclick"] = ClientScript.GetPostBackEventReference(this,"ClickDiv");

}

protectedvoid Div1_Click()

{

// Do something

}

#region

IPostBackEventHandler Memberspublicvoid RaisePostBackEvent(string eventArgument)

{

if (!string.IsNullOrEmpty(eventArgument))

{

if (eventArgument =="ClickDiv")

{

Div1_Click();

}

}

}

#endregion

}

Hope this helps


Excellent info. Thank you!

Hi, i wonder if you could help me, i'm trying to get this work inside a user control. It doesnt implementSystem.Web.UI.Page,

i have tried the followng code but i cant pick up the event

Sorry, this is converted to vb as well

Code behind is

Imports System.ReflectionPartialClass editControllerBarInherits System.Web.UI.UserControlImplements IPostBackEventHandlerProtected Sub Page_Load(ByVal senderAs Object,ByVal eAs EventArgs) cancelDiv.Attributes("onclick") =Me.Page.ClientScript.GetPostBackEventReference(Me,"ClickDiv")End Sub Protected Sub Div1_Click()' Do somethingEnd Sub Private MembersAs IPostBackEventHandlerPublic Sub RaisePostBackEvent1(ByVal eventArgumentAs String)Implements System.Web.UI.IPostBackEventHandler.RaisePostBackEventIf Not String.IsNullOrEmpty(eventArgument)Then If eventArgument ="ClickDiv"Then Div1_Click()End If End If End Sub

the div on the user control looks like this

 <div class="btnCancel" runat="server" id="cancelDiv"> </div>
 
could you help please? 

0 comments:

Post a Comment