Showing posts with label example. Show all posts
Showing posts with label example. Show all posts

Monday, March 26, 2012

Deserializing derived objects

Hello!

Maybe my question is rather stupid, but I'm just a newbie...

I have some classes derived from class DataObject, for example Account:

public

classAccount :DataObject {...}

and a [ScriptService()] - marked .asmx service with method

[

WebMethod( EnableSession =true )]publicDataObject AddObject(DataObject dataObject )

which passes this dataObject to another .svc service which writes it to database...

I call AddObject method throughSys.Net.WebServiceProxy.invoke(...), passing JavaScript object corresponding to Account class (I mean it has the same fields). If I set the parameter type in AddObject method to Account - everything works fine, it serializes and deserializes correct and passes further as an Account object. But if I try to set parameter type to DataObject - it looks like deserializer cuts all fields that are not declared in DataObject class and doesn't recognize derived object. The dataObject object in this method is pure DataObject, not Account.

I want to handle objects this way because actually I have now something about 10 classes and 10 such methods, and their number may increase. Writing all methods like AddAccount, AddUser, AddQuote and so on looks unhandy for me.

Is it possible for default JavaScriptSerializer to recognize derived objects? Or if I have to redefine it - how could I do that?

Thank you!

I faced a similar problem. The issue at hand is that the module that does the serialization/deserialization will look to the return types and parameters as cues for what it should serialize to / deserialize to , and it doesn't look deeper than that. I ended up doing a fairly hacky work-around to get the code in on time, but at some point I'm probably going to try and figure out a good solution.

A couple thoughts I had, neither of which are tried out. First, I'm aware that you can generate javascript types for nested types of the web service class (seehttp://ajax.asp.net/docs/mref/T_System_Web_Script_Services_GenerateScriptTypeAttribute.aspx) so that's an option, I suppose. Seems like not the best solution in our case, though, as what we're working with aren't logically nested types of the web service. I also considered writing a serializer module that can take the first param as a type argument so that, at the least, you could specify which concrete type you're sending upstream.instead of letting the module map only to the types in the webmethod params.


For XML Serialization (as used in classic 2.0 asmx web services) there is an attribute called XmlInclude which allows you to specify specializations of the actual return type. omly the ones you list with XmlInclude will be taken into account by the XmlSerializer.

I know the script service infrastructure hooks into the classic asmx stuff, but I have no idea if it looks at XmlInclude-s to. You might give it a try ...

-- Henkk

Saturday, March 24, 2012

Deploying Atlas in IIS

Hi,

I've got a little problemwhen deploying my TreeView Atlas Example in IIS. I'm working on WinXP PRo SP2. Publishing the website from VS2005 is not a problem but when I want to view it in a browser i've got the following error:

XML analysis error : malformed Site: http://192.168.0.169/Test/FirstTreeView.aspxLine Number 1, Column 2: < % @dotnet.itags.org. Page Language="C # "% > - ^
Pls I need help

What is your sample doing? It looks like it's trying to parse the server aspx page itself as XML, which would not work. Does your web site run correctly before you deploy it?

David


Thanks for replying. My sample is a TreeView which DataSource is an XML File. I've got 2 update panels, on one, there is the update panel and the second show the selected nodes of the Treeview. It works fine before deploying it in IIS. I tried it on a friend's machine and the deployment works (Windows Server 2003, IIS 6.0). Do we then need to have IIS 6.0 in order to deploy coveniently atlas applications on IIS?

Here is my source code:

<%@. Page Language="C#" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
<script runat="server">
void dataBound(Object sender, TreeNodeEventArgs e)
{
// Determine the depth of a node as it is bound to data.
// If the depth is 1, show a check box.
if (e.Node.Depth == 2)
{
e.Node.ShowCheckBox = false;
}
else
{
e.Node.ShowCheckBox = false;
}
}

protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
{
//If a node is selected, display the text in the right table
//Message.Text = TreeView1.SelectedNode.Text;
if (TreeView1.SelectedNode.Selected)
{
foreach(TreeNode node in TreeView1.SelectedNode.ChildNodes)
{
Message.Text += node.Text + "<br>";
}
}
else
{
Message.Text = "No Item Selected";
}
}

protected void Button1_Click(object sender, EventArgs e)
{
Message.Text="";
}
</script
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>First TreeView with an XML File</title>
<style type="text/css">
div.global
{
width:100%;
margin:0px;
border:1px solid #99ccff;
line-height:150%;
}
div.header,div.footer
{
padding:0.5em;
color:white;
background-color:#99ccff;
clear:left;
}
div.left
{
float:left;
width:200px;
margin:0;
padding:1em;
}
div.right
{
margin-left:270px;
border-left:1px solid #99ccff;
padding:1em;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<atlas:ScriptManager EnablePartialRendering=true ID=ScriptManager runat=server/>
<div class=global>
<div class="header">Dynamic Atlas Tree View</div>
<div class=left>
<atlas:UpdatePanel ID=up1 RenderMode=Inline Mode=Conditional runat=server>
<ContentTemplate>
<asp:TreeView ID="TreeView1" runat="server" DataSourceID="XDS" OnTreeNodeDataBound="dataBound" ShowCheckBoxes=None OnSelectedNodeChanged="TreeView1_SelectedNodeChanged" ShowLines=true>
<DataBindings>
<asp:TreeNodeBinding DataMember="Kpf" ValueField="ID" TextField="Name"/>
<asp:TreeNodeBinding DataMember="Activity" ValueField="Value" TextField="Value"/>
</DataBindings>
</asp:TreeView>
<asp:XmlDataSource ID="XDS" runat="server" DataFile="~/XMLTVDataSource.xml"></asp:XmlDataSource>
</ContentTemplate>
</atlas:UpdatePanel>
</div>
<div class=right>
<atlas:UpdatePanel ID=up2 runat=server Mode=Conditional RenderMode=Inline>
<ContentTemplate>
Selected elements<hr /><br />
<asp:Label ID=Message runat=server></asp:Label>
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Clear All" />
</ContentTemplate>
<Triggers>
<atlas:ControlEventTrigger ControlID=TreeView1 EventName=SelectedNodeChanged />
</Triggers>
</atlas:UpdatePanel>
</div>
<div class=footer>KPF Nord</div>
</div
</form>
</body>
</html>


I've fixed it. I re-installed IIS and .NET Framework 2.0. Latter i followed this:

All is OK Thanks

Wednesday, March 21, 2012

Delete Image from slideshow

Hi there,

I have copied the slideshow example and modified it to suit my purposes. I am stuck however on a problem.

I want to be able to press a button and delete an image from the slideshow. I know how to create the delete statement I just don't know how to get the value from the image into the statement.

Im using XML to store the location information of the images

<?xml version="1.0" encoding="utf-8"?>
<customer>
<header>
</header>
<slides>
<slide imagePath="\AjaxShaneSite\SlideShow\images\Blue hills.jpg" name="1" description="This is blue" />
<slide imagePath="\AjaxShaneSite\SlideShow\images\sunset.jpg" name="2" description="sunset" />
<slide imagePath="\AjaxShaneSite\SlideShow\images\water lilies.jpg" name="4" description="water lillies" />
<slide imagePath="\AjaxShaneSite\SlideShow\images\winter.jpg" name="5" description="winter...." />

</slides>
</customer>

Name equals the images ID.

This is my webmethod for the slide show

public partial class _Default : System.Web.UI.Page
{
// protected System.Web.UI.WebControls.DropDownList DropDownList1;
// Get the SQL Connection string from the web.config.
public String strConnectSQL = (ConfigurationSettings.AppSettings["csMain"]);

[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod]
public static AjaxControlToolkit.Slide[] GetSlides()
{
XmlDocument myXML = new XmlDocument();
myXML.Load("H:\\AjaxShaneSite\\Slides.xml");
int i = 0;
AjaxControlToolkit.Slide[] temp = new AjaxControlToolkit.Slide[myXML.SelectSingleNode("/customer/slides").ChildNodes.Count];
foreach (XmlNode xNode in myXML.SelectSingleNode("/customer/slides").ChildNodes)
{
temp[i] = new AjaxControlToolkit.Slide(
xNode.Attributes["imagePath"].Value,
xNode.Attributes["name"].Value,
xNode.Attributes["description"].Value);
i++;
}
return temp;
}

Can anyone suggest some way for me to get the value from the image that is currently been displayed within the show?

Any help would be great!

Thanks

Shane

You can set the behavior id on the slideshow and retrieve the image element and get the source from that.

Something like this:

var slideshowbehavior = $find('slideshowbehavior1');

var image = slideshowbehavior.get_element();

var imageurl = image.src;


Sorry to be sounding daft here but where do I place this code?

Think I need a dummies guide to .NET & AJAX


In a client side function that responds to the slide changed event.

Check out this post:http://forums.asp.net/thread/1627419.aspx. It talks about getting the current slide.


Hi again,

Sorry I am really battling with this one.

I have a form that is in a master page. I have add your script to my page but nothing happens.

This is my update.

<%@. Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="ItemDetails.aspx.cs" Inherits="_Default" Title="RMH - Item Details" %><%@. Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %><asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"> <script type="text/javascript">function addSlideShowHandlers() {// hook into the slide show change eventvar slideshow = $find("slideShowBehavior1");slideshow.add_slideChanged(setCurrentSlide);}function setCurrentSlide(sender, eventargs) {var label = $get('label1');label.innerText = eventargs.ImagePath; }</script> <table style="width: 100%; height: 100%" onload="addSlideShowHandlers()"> <tr> <td style="width: 10%;"> </td> <td style="width: 35%;"> </td> <td style="width: 55%;"> </td> <td style="width: 10%;"> </td>
....
...
..
<cc1:slideshowextenderid="slideshowextend1"runat="server"autoplay="true"imagedescriptionlabelid="imageLabel1"BehaviorID="slideShowBehavior1"loop="true"nextbuttonid="nextButton"playbuttonid="playButton"playbuttontext="Play"previousbuttonid="prevButton"slideshowservicemethod="GetSlides"stopbuttontext="Stop"targetcontrolid="Image1"></cc1:slideshowextender>

</td><tdstyle="width: 55%;">

</td><tdstyle="width: 10%;"></td></tr></table>

Copyright

<br/>

</

asp:Content>

Any ideas?


I was having the same problem you are having and finally realized that since I am using Master Pages, I have to put the <bodyonload="addSlideShowHandlers()"> statement in the body tag on the Matser Page. Once I did that, it worked like a charm.

Hope that helps.


I've seen a number of threads such as this one discussing how to retrieve the current slide from the extender. I need to set the current slide so that the slide show begins from a designated index inside the slides array.

I am developing a rudimentary understanding of using behaviorID to hook into the various methods inside the javascript used to implement the extender but don't yet have enough of an understanding of what I'm seeing to figure out how to pass an index in and have the slideshow initialize at that point in the array rather than the beginning.

Any assistance is greatly appreciated!


For people using masterpages and trying the onload you might need to register the onload even in your child page. Make sure in your master page you've got the body tag set to runat="server" and give it an id="yourname". The example code below uses the body tag ID ="MainBody".

(inside the childpage's Page_Load event)

HtmlGenericControl body = (HtmlGenericControl)Page.Master.FindControl("MainBody");

body.Attributes.Add("onload","LoadBase()");

---

Doin the above means you will not have to register the code in your masterpage.

Declarative xml-script from UpdatePanel

Is there a way to insert (and execute) declarative xml-script from UpdatePanel?

For example following code works if control is visible, so xml-script is loaded with the page. If control is hidden, or created at run time xml-script is not executed.

test.aspx:

<%@dotnet.itags.org. Page Language="C#" AutoEventWireup="true" %>
<%@dotnet.itags.org. Register TagPrefix="uc" TagName="Control1" src="http://pics.10026.com/?src=~/WebUserControl.ascx" %>
<script runat="server">
void Button1_Click(object sender, EventArgs e)
{
c1.Visible = true;
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server"><title>Untitled Page</title></head>
<body>
<form id="form1" runat="server">

<atlas:ScriptManager ID="ScriptManager" runat="server" EnablePartialRendering="true" />

<atlas:UpdatePanel ID="p1" runat="server" Mode="Conditional">
<ContentTemplate>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
<uc:Control1 ID="c1" runat="server" Visible="false" />
</ContentTemplate>
</atlas:UpdatePanel>

</form>
</body>
</html>

WebUserControl.ascx:

<%@dotnet.itags.org. Control Language="C#" ClassName="WebUserControl" %>

<div id="panel">Click the button to Hide me</div>
<input id="hideButton" type="button" value="Hide" />

<script type="text/xml-script">
<page xmlns="http://schemas.microsoft.com/xml-script/2005">
<components>
<control id="panel" />
<button id="hideButton">
<click>
<setProperty target="panel" property="visible" value="false" />
</click>
</button>
</components>
</page>
</script>

hello.

well, where's your panel control? if it's inside a user control, then you've already found your problem: the user control is a naming container class. this means that controls declared inside it will have a different client side id (it will be composed by the usercontrol id and the control id).


I thought client side id only changed for webform controls. This panel is just a plain <div>.

And if I just make user control visible from the start, it works, xml-script executes.


hello.

yes, if it's an html control, then you're right. not sure about the visibility...can you show me the div declaration that you have in the user control?


The div declaration is actually in the code above:<div id="panel">Click the button to Hide me</div>

I choose to hide div just for sample sake, it can be any xml-script.

Anyway, I recall now, javascript doesn't execute either, when it's inside control template and then the control instantiated inside update panel. The only way is to use RegisterXYZScript(...). But RegisterXYZScript(...) doesn't work for xml-script.

When using ScriptManager RegisterControl it places xml-script inside of that <xmlScript> tag at the end of update panel's <delta> response. I think it executes it, but there beside RegisterControl I didn't find any more methods to generate xml-script.

I know there is a way to write an ExtenderControl, and use ScriptTextWriter to generate xml-script. I haven't tried that yet.