Showing posts with label codebehind. Show all posts
Showing posts with label codebehind. Show all posts

Wednesday, March 28, 2012

Calling function on Codebehind from client side scripting using ASP.NET AJAX

Hi,

I'm relatively new to the new avtar of AJAX. I've used the Ajax.dll in my previous application to make an AJAX call.

Now my issue is. How do i call a function on my code behind page , from my client side javascript. To elaborate this consider the following.

I'm creating a new ASP.NET AJAX Web Site.

I've Defaul.aspx with following controls.

1. a button.

2.a dropdown.

In my Default.vb page i've a function FillComboValues() that returns a string.

No on clicking Button i wanna run the FillComboValues() function on my code behind.( I DONT WANT TO USE UPDATEPANEL.)

I've read many articles on net to solvemy problem but every example describes how to access a webservice method. none of them have described how to access a pagemethod.

Pls help me out with this ...

Thanks in advance.

----

In previous version of ajax , we needed to register the server side function using <Ajax.AjaxMethod()> _ . isn't there an equivalent way of registering the function.?

Hi,

Same problem for me, plz let me know whether you have found a solution for this or not?

Thanks


Hi, you can only call static functions of codebehind from javascript normally. Create a static function in codebehind and in javascript just call it in the following way:

say your static method is returning some string value, then in javascript you have to write,

var returnedValue = <% ="'" + ReturnHello() + "'" %>


In order to call a function on codebehind you can use either a WebService or an static method. I had the same problem and i used static methods because it′s more like Ajax.AjaxMethod, The first thing to do is to add to the scripmanager the following line EnablePageMethods="true" and then in code behing put [WebMethod] in front of every method you want to use. To call them from your client script, just use PageMethods.[your method name] and you should be good to go....


i am speaking about the code behind file of aspx page (not asmx-webservice). You can not use [WebMethod] in codebehind file.

In previous version of ajax , we needed to register the server side function using[Ajax.AjaxMethod()] {in codebehind} . isn't there an equivalent way of registering the function.?

Pleaes let me know if you have any thing equivalent to this?


SaiTej:

i am speaking about the code behind file of aspx page (not asmx-webservice). You can not use [WebMethod] in codebehind file.

In previous version of ajax , we needed to register the server side function using[Ajax.AjaxMethod()] {in codebehind} . isn't there an equivalent way of registering the function.?

Please let me know if you have any thing equivalent to this?

Alternate method inAjax Extensions is putting the control (for ex:button) in Update panel. All the events that u write for this control are async.

So no need to declare explicitly[Ajax.AjaxMethod()]


asj:

Equivalent way inAjax Extensions is putting the control (for ex:button) in Update panel. All the events that u write for this control are async.

So no need to declare explicitly[Ajax.AjaxMethod()]

Saturday, March 24, 2012

call codebehind onclick function from modal popup

My modal popup extender pops up an asp:panel that has a couple asp:textbox'es and an OK asp:button that I designate as the OKControlID. I've implemented a javascript function called OnOK() that's designated as the OnOkScript in my control extender, and also implemented a Button_Click function for the OK Button in my page codehind. How do I get the codebehind function to get called. My code calls the onOK script and stops.

Thanks!

Short answer:

You can't. The behavior adds a handler for the click event of the button which executes the OnOkScript(). This prevents a postback from occurring, and a postback is what you need to occur in order to call your code behind function.

Long answer: If you use the property "EnablePageMethods='true'" in your script manager, and assign [WebMethod] attribute to your codebehind, you can call that codebehind method from your JS OnOK function.


LSU:

Tried leaving a message for you on your listed website, but it

doesn't allow anonymous comments, and I didn't want to have to

sign up..I have need for this threads concepts, but I tried doing

an MPE.show in my code behind trying to get the MPE to

display as a result of running code, (not a button click) and

get a runtime error.."cmdDelete already has a data item registered."

VS2005 Pro has no problem with the code..am I using it out of

context?..what am I doing wrong?..

Thanks..


LSU.Net:

Short answer:

You can't. The behavior adds a handler for the click event of the button which executes the OnOkScript(). This prevents a postback from occurring, and a postback is what you need to occur in order to call your code behind function.

Long answer: If you use the property "EnablePageMethods='true'" in your script manager, and assign [WebMethod] attribute to your codebehind, you can call that codebehind method from your JS OnOK function.

Haha! Both answers looked "short" to me.Big Smile
It may not work for the poster's case since the CodeBehind is a WebMethod hence static method which is independent from the page object concept and so if there're reference to the page's objects in the code-behind it'll fail. For the original poster, you may have to "cheat" your way to get it to work. Please see this threadhttp://forums.asp.net/thread/1669503.aspx . I hope there'll be support from AJAX client side or we may have to extend the ModalPopup extender.Big Smile


G20:

Thanks for your input ! I haven't had time to read /absorb the details of the

link yet, but will today. George Bernard Shaw said:

"Science is always wrong. It never answers one question without asking

10 more."

Themodal.hide works fine from the code behind.

Themodal.Show is syntactically acceptable in the code behind,

but doesn't. Why? Where is it used? What is it supposed to do?

.Show works fine in my VB / Access apps (ha ha!)

Seriously, I've been looking for a comprehensive source for

the props & methods of AJAX controls..with examples, like

MSDN would be awesome. Then I could be self -reliant.

Any help there?


yeah i have the same problem as you.

I need to fire/call the modalpopup when there is a textchanged event in a textbox.

I have tried everything i can think of jscript wise.

I also dont' get why the modalpopupextender1.show() isn't working...

If you get this figured out could you please post it ? Ill do the same if i can figure it out.

Thanks

call codebehind function in AJAX

I need to call codebehind function in AJAX like this:

My dropdown.attributes.Add("SelectedIndexChanged","updatetable(this.selectedValue)");

where updatetable() will call a codebehind function that run a query to update the table with the passed parameter of this.selectedValue.

Please advise how to make the above happen using ASP.NET AJAX.

Thanks .

look for how to call web service from ajax Client, then in you javascript call your webservice method like

function updatetable(value)

{

callyorMethod(parameters, OKReturn,onError);
}

function OKReturn(result)

{
//do whatever with data return from web service
}


I beg you to please kindly provide complete sample code.

Check this outhttp://www.asp.net/ajax/documentation/live/tutorials/ExposingWebServicesToAJAXTutorial.aspx


So I need to a web service for whatever codebehind function for AJAX to call?

You can also call a static method the same way. i think is better way to make a webservice in your project to separete call from Ajax to the page especially if you decide to develop with another framework than asp.net.


Thanks.

BTW, is that icon of yours Alain Delon?


yes is Delon in Borsalino when he was young and handsome like me.Big Smile


Et Alain est vieux et laid maintenant?