Showing posts with label custom. Show all posts
Showing posts with label custom. Show all posts

Wednesday, March 28, 2012

Updating a ContentPlaceHolder with Ajax

I am working on a project which uses a MasterPage with one ContentPlaceHolder. I have a custom menu control on the MasterPage. When an item in the menu is clicked I need the ContentPlaceHolder to be updated with the requested content. I know this can easily be done using an iframe, but I'd rather stay away from iframes and like to do this with Ajax.

Basically my question is if this is possible at all. I've been trying a couple of things but without success and I think it may have to do by the way a MasterPage works.
If not possible, is there some kind of workaround for this? (without residing to iframes)

Thanks!

Hi Rino,

I tried also the same thing as you. I had a menu on my master page and an ContentPlaceHolder in my update panel. That didn't work. I think it isn't possible what you want to do.

Regards,


Unfortunately, no. It's just not how master pages and AJAX work. I just finished a post on the topic here:http://odetocode.com/Blogs/scott/archive/2007/01/03/9682.aspx


Thanks guys for clearing this up!
Would be great as a new .Net feature though :)


You can do it right now, actually, it's not that big of a deal. You don't really need a contentplaceholder for it, just a Div that you can hook into w/ javascript (giving it an ID, for example). You have your menu item click handler fire a javascript function that calls a web service and then have the onrequest complete handler fill the div with the stuff it gets back.

paul.vencill:

You don't really need a contentplaceholder for it, just a Div that you can hook into w/ javascript (giving it an ID, for example). You have your menu item click handler fire a javascript function that calls a web service and then have the onrequest complete handler fill the div with the stuff it gets back.

Paul: Yes, but this is an entirely different approach.


Understood. I was trying to offer something that might work for his application. is there a good reason you can think of to limit his app to using contentplaceholders?

No, my goal was just to point out that updating content place holders with content from a different page isn't how the technologies work together. It seems to be a common question these days, so I'm trying to clear that up.

The right way to achieve this would be to use a solution like you proposed!


ah, ok, cool. :D

It is very easy.

See my last post about "Handling events...".

Wednesday, March 21, 2012

Use Ajax in Control

Hello,

I am creating a class library where I have a custom web control.

This control has Labels and a GridView.

I want to use an UpdatePanel but there is a problem:

VS 2005 is not recognizing the Update Panel that I created. How should I do this?

Should I just create my web control and then place it inside an Update Panel on my page?

Thanks,

Miguel

How are you adding the UpdatePanel? Is this a user control? What do you mean when you say that VS 2005 is not "recognizing" the UpdatePanel that you created; what errors are you getting, ie, what happens?

Use Ajax controls within web control

Is there any example that show how to write a custom web control (one that doesn't have aspx) and use Ajax controls ?

I have a user control consisting of a panel the modalpopupextender. I have a copy of the aspx and .cs file in all the sites. I want to turn it into a control so I can re-use it. How can I achive it?

Hi Thanhhuynh,

Basically, you can drag it into your customer control without doing any other special settings. Ajax Control Toolkits work depend on ScriptManager or ToolkitScriptManager. Please make sure that the ScriptManager should be located before the extender and inside a page there should be only one ScriptManager. To create customer control , here are two samples(sample1 andsample2).

I hope this help.

Best regards,

Jonathan


Thanks for answering.

Basically there are two type of controls I can create: web control and web user control. The former contains only code. There's no UI designer support where you can drag and drop different controls into it. You can to override the RenderXX() methods to create the look. The web user control has the UI part and the code behind and you can design right on the screen. But web user can only be used within the same web site. You have to copy the aspx and code-hind files to another web site source tree in order to re-use it.

So my question was let's say I create a new control, without VS UI designer how can I use the AJAX extender ?

Thanks


I believe the answer you are looking for is in this blog post by David Ebbo:http://blogs.msdn.com/davidebb/archive/2005/10/30/487160.aspx

Good luck,

Hanan Schwartzberg
---------------
http://www.lionsden.co.il

Use Custom Objects as Extender Control Properties - Part II

I thought this post -http://pietschsoft.com/Blog/Post.aspx?PostID=1377 - was very useful in further developing a custom ajax control extender. Thanks to Chris Pietschmann for posting his work.

I expanded his work to override the ConvertFrom method to JSON deserialize a string back to the custom object. In this example, I am using a Person object. Please refer to Chris's article for code and adapt the code with the changes I made below in bold italics:

GenericTypeConverter class:

namespace SandboxClass{public class GenericTypeConverter<T> : TypeConverter {public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture,object value, Type destinationType) { JavaScriptSerializer jss =new JavaScriptSerializer();string s = jss.Serialize(value);return s; }public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture,object value) {if (value is string) { JavaScriptSerializer jss =new JavaScriptSerializer(); T t = jss.Deserialize((string)value);return t; }else return base.ConvertFrom(context, culture,value); } }}
Person class:
namespace SandboxClass{ [TypeConverter(typeof(GenericTypeConverter<Person>))]public class Person {private string _FirstName;public string FirstName {get {return _FirstName; }set { _FirstName =value; } }private string _LastName;public string LastName {get {return _LastName; }set { _LastName =value; } } }}
 
Default.aspx
 
public partialclass _Default : System.Web.UI.Page{protected void Page_Load(object sender, EventArgs e) {if (!Page.IsPostBack) { // serialize object to string to pass to client SandboxClass.Person person =new SandboxClass.Person(); person.FirstName ="John"; person.LastName ="Doe"; ajaxControlExtender.MyPerson = person; }else { // deserialize object back from client to your custom object, in this example, the Person object SandboxClass.GenericTypeConverter gc = System.ComponentModel.TypeDescriptor.GetConverter(typeof(SandboxClass.Person))as SandboxClass.GenericTypeConverter; SandboxClass.Person person = (SandboxClass.Person)gc.ConvertFromString(ajaxControlExtender.ClientState); Label2.Text = person.FirstName +" " + person.LastName; } }}
 
Also, in addition to step 5 of Chris's post and after manipulating the object on client-side, I am assigning the object to the ajax control extender ClientState property
to maintain state back to the server so the deserialization process can perform. Below is an example to include in the ajax control extender js file:
 
this._MyPerson.FirstName ='Jane';this._MyPerson.LastName ='Smith';this._MyPerson = Sys.Serialization.JavaScriptSerializer.serialize(this._MyPerson);this.set_ClientState(this._MyPerson);
Enjoy!

Thanks for your sharing.


Continuing to expand on this... It looks like this requires more work when one wants to serialize and deserialize an object with nested objects. Ugh... Any input or ideas to quickly move this along would be appreciated. I am thinking this will involve some recursive logic.


As an additional reference -http://www.manuelabadia.com/blog/PermaLink,guid,22a125d3-1ed3-422b-ba2b-89ed63febce3.aspx

It looks like I need to implement a JavaScriptResolver...


Well well... Sure enough, the JavaScriptResolver solves the problem with nested objects. The link in my previous post above explains it all.

Modified the code below in bold italics -

namespace SandboxClass{public class GenericTypeConverter : TypeConverter {public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture,object value, Type destinationType) { JavaScriptSerializer jss =new JavaScriptSerializer(new SimpleTypeResolver());string s = jss.Serialize(value);return s; }public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture,object value) {if (value is string) { JavaScriptSerializer jss =new JavaScriptSerializer(new SimpleTypeResolver()); T t = jss.Deserialize((string)value);return t; }else return base.ConvertFrom(context, culture,value); } }}

I am looking to move this JSON serialization of a custom object out of an AJAX control extender into a custom AJAX client control.

I have this part in my custom AJAX client control -

 IEnumerable IScriptControl.GetScriptDescriptors() { ScriptControlDescriptor descriptor =new ScriptControlDescriptor("CGAjaxClientControls.CGRadioButtonList",this.ClientID); descriptor.AddProperty("result",this.Result); descriptor.AddProperty("targetAction",this.TargetAction);return new ScriptDescriptor[] { descriptor }; }
The 'result' is just a string. That's the easy part but 'targetAction' is a custom object with nested objects as well.
The problem is I get this error message trying to deserialize on the client.

Sys.ArgumentTypeException: Object of type 'Object' cannot be converted to type 'String'. Parameter name: data

Here is the $create -

Sys.Application.add_init(function() { $create(CGAjaxClientControls.CGRadioButtonList, {"result":"Test","targetAction":{"ID":0,"TargetRadioButtonListId":"CGRadioButtonList1","RadioButton":[{"ID":0,"Text":"Yes","Value":"True","Selected":false,"Container":"panel","TargetControlId":"divCGRadioButtonList1Y"},{"ID":1,"Text":"No","Value":"False","Selected":false,"Container":"panel","TargetControlId":"divCGRadioButtonList1N"}]}},null,null, $get("CGRadioButtonList1"));});

Can anyone help me crack this? I do notice the type resolver tags are not getting added.

Thanks!


I think I've got this figured out. It is not required to deserialize the object when you create a custom AJAX client control. It's already done for you through IScriptControl.