Showing posts with label clicks. Show all posts
Showing posts with label clicks. Show all posts

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? 

Monday, March 26, 2012

Server Time

I have an intranet set up and when the user clicks a
button I want the time on the server to appear in the
text box.

By reading through the post I found many that said
DateTime.Now.Hour.ToString should bring back the server's
time. But it brings back the time on my local machine.
From my research it sounded like to get local time on the
client you needed to use JaveScript.

Can someone clarify and help me get the time on the
servers station?

ThanksHi Brian,

Make sure that the call to DateTime.Now.Hour.ToString is being run on the
server side. If it is embedded on the page in a script tag without the
runat="server" attribute, then it might be running client-side.

To make sure the code is running server side, try putting it in code behind.

I notice that you're also only grabing the hour. Unless you are in a
differnet time zone or your system clocks are drastically off, the hour
should match on both machines. To test, try setting the machine clocks to
somethign drastically different and grab the DateTime.Ticks property and
compare.

HTH,
Mark Jen [MSFT]
--
Please reply in newsgroup.
This posting is provided "AS IS" with no warranties, and confers no rights.

"brian" <bshannon@.lbrspec.com> wrote in message
news:13ee001c3f7da$adf128a0$a401280a@.phx.gbl...
> I have an intranet set up and when the user clicks a
> button I want the time on the server to appear in the
> text box.
> By reading through the post I found many that said
> DateTime.Now.Hour.ToString should bring back the server's
> time. But it brings back the time on my local machine.
> From my research it sounded like to get local time on the
> client you needed to use JaveScript.
> Can someone clarify and help me get the time on the
> servers station?
> Thanks