Monday, March 26, 2012

Call server side function on javascript mouseover event?

Hi, I'm very new to ajax so please forgive me if this is a stupid question:

I have a hyperlink hooked to a mouseover event and an asp server side function called Test(). I like to know if there's any way in ASP.NET AJAX to call Test() when that mouseover event fires, and how to go about hooking this up. Code samples on how to do this would be helpful.

Thanks in advance.

Hi Totenkopf,

You can do what you want to do using the new client callback feature in asp.net 2.0 :

check these articles for more info -

http://www.devx.com/webdev/Article/28451/0/page/2

http://dotnetjunkies.com/Article/E80EC96F-1C32-4855-85AE-9E30EECF13D7.dcik

For other options check this post -

http://forums.asp.net/thread/1515994.aspx


Hmm, so can this be done at all using the existing AJAX extension? What I wish to accomplish is when a user mouseover a hyperlink, have a server-side method executes which populates a repeater control (these two are within an updatepanel)

Here's an ASP.NET AJAX solution. In this example, hovering over the link does the same thing as clicking on it. (Either action triggers an async postback.) For your example, put your repeater where I have the label. I hope this helps!

<%@. Page Language="C#" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><script runat="server"> public void click(object sender, EventArgs e) { lbl.Text = "Moused at " + DateTime.Now.ToString(); }</script><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title>Untitled Page</title></head><body> <form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server" /> <asp:UpdatePanel ID="up1" runat="server"> <ContentTemplate> <asp:LinkButton ID="hoverButton" runat="server" OnClick="click" onmouseover="__doPostBack(this.id, '');" Text="Mouse Over Me" /> <asp:Label ID="lbl" runat="server" /> </ContentTemplate> </asp:UpdatePanel> </form></body></html>

Using the client callback doesn't give you the triggering you need. Unfortunately this can only be forked by using a private JS method that is included when you add an UpdatePanel.

On the client side, call the following method on the hover event:
Sys.WebForms.PageRequestManager.getInstance()._doPostBack(hyperlinkClientID,"Some String Argument");

Your custom hyperlink control should implementIPostBackEventHandler.

Create anOnHover event on you custom hyperlink control that can be used for triggering an update panel to refresh.

In theRaisePostBackEvent method that you have to implemented on your control, fire the OnHover event.


or yeah you can replace Sys.WebForms.PageRequestManager.getInstance()._doPostBack(hyperlinkClientID,"Some String Argument"); with __doPostBack(hyperlinkClientID,"Some String Argument");as shown be Steve.

Furthermore I forgot to mention you would have to register your control as an asyncronous control in OnPreRender():

ScriptManager

sm =ScriptManager.GetCurrent(this.Page);
if (sm !=null) sm.RegisterAsyncPostBackControl(this);

I've tried passing in an argument to the second parameter of __doPostBack, but I keep getting a "Invalid postback or callback argument..." popup. I then tried registering the control as suggested:

protected override void OnPreRender(System.EventArgs e)
{

ScriptManager sm = ScriptManager.GetCurrent(this.Page);
if (sm != null)
sm.RegisterAsyncPostBackControl(this);
}

... but then I get "The page cannot be registered through RegisterAsyncPostBackControl or RegisterPostBackControl"

I can get the argument with Request.Params.Get("__EVENTARGUMENT")if I disable EnableEventValidation, but is there any way to get this workingwithout setting EnableEventValidation to false?

Thanks for the help again.

No comments:

Post a Comment