Showing posts with label stupid. Show all posts
Showing posts with label stupid. Show all posts

Wednesday, March 28, 2012

Calling a server side function from client side using the toolkit

Ok, I'm sure this is a really stupid question, but I'm willing to ask it anyway.

Let's say that I created a new extender called "My", so I now have three files files, MyExtender.cs, MyBehaviour.js, and MyDesigner.cs.

I want my control to be able to take some data on the client side and use it to retrieve some data on the server side, which AJAX is all about right? :) So for the sake of argument, let's say I want to enter an email address in a textbox, and then consult a database to return the user's password.

So in my MyExtender.cs file, I create a function something like this:

public string getPassword(string email) {

... Make some database calls to get the info

return password;

}

What code do I put into the MyBehaviour.js file in to to call the getPassword function? And do I need any special decorations on the getPassword() function to make it happen?

You can use ICallbackEventHandler interface for this purpose. This is the exact purpose of this interface in ASP.Net 2.0. Check out the MSDN for this interface.


You really should look at how to create an extender to do it the best way. There is a great tutorial here,http://www.asp.net/ajax/ajaxcontroltoolkit/samples/Walkthrough/CreatingNewExtender.aspx that is a simple example of how to create a simple extender. Also if you just want to use the code behind to retrieve info from the database, you may just want to wrap an update panel around your form and get the data from the code behind. Either way you will be able to accomplish what you want.

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.