Wednesday, March 21, 2012

AJAX SlideShow Extender error

Hey, I have a SlideShow Extender into a Webform with Visual Basic and this doesn't show the pictures via WebMethod, only the first picture witch I inicializated. I let you know my code:

<asp:Image ID="Image1" runat="server"
Height="200"
Style="border: 1px solid black;width:auto"
ImageUrl="~/images/DSC02489.JPG"
AlternateText="Desfile Sambil Valencia" /> <br />
<asp:Label runat="Server" ID="imageLabel1"/><br />
<asp:ImageButton ID="prevButton" runat="server" ImageUrl="~/images/previous_20_1.gif" />
<asp:ImageButton ID="playButton" runat="server" ImageUrl="~/images/play_30_1.gif" />
<asp:ImageButton ID="nextButton" runat="server" ImageUrl="~/images/next_20_1.gif" />
<ajaxToolkit:SlideShowExtender ID="SlideShowExtender1" runat="server"
TargetControlID="Image1"
SlideShowServiceMethod=GetSlides()
AutoPlay="true"
ImageDescriptionLabelID="imageLabel1"
NextButtonID="nextButton"
PlayButtonText="Play"
StopButtonText="Stop"
PreviousButtonID="prevButton"
PlayButtonID="playButton"
Loop="true" />

and the script...

<script runat="server" type="text/VB">
<System.Web.Services.WebMethod()> _
<System.Web.Script.Services.ScriptMethod()> _
Public Shared Function GetSlides() As AjaxControlToolkit.Slide()
Return New AjaxControlToolkit.Slide() {New AjaxControlToolkit.Slide("~Images/DSC02489.JPG", "Desfile Febrero Sambil", "Desfile Febrero Sambil"), New AjaxControlToolkit.Slide("~Images/DSC02489.JPG", "Desfile Febrero Sambil", "Desfile Febrero Sambil")}
End Function
</script>

and the Internet Explorar error is as follow>

Line: 79

Char: 1

Error: 'this._slides[...].ImagePath' is null or not an object

Please... Can Someone tell me how I doing it a best-use...

Please set the method name as a string

SlideShowServiceMethod="GetSlides"

*not* SlideShowServiceMethod=GetSlides() .

Check out the sample athttp://ajax.asp.net/ajaxtoolkit/SlideShow/SlideShow.aspx.


Ok, it's working like sample now, but I really need to use a webmethod that allow obtain photos from a diferent source. I mean, internally I can to set the webmethod to read a database and locate photo source but my problem is when it's required that WebMethod "knows" what is the data source to query because I can't to pass parameters? or Can I?

Thank you in advance


After hours figthing with this code I found a solution to my need...

I was needing pass a parameter to the WebService to retrieve values (Pictures Names) from a database. At the moment, I couldn't do this (Anyone can to make me a correction). So, I tried to use a Application Variable (Applicationstate) named Id (my Parameter) and my Webservice could read it and this worked to retrieve the specific Pictures Collection taht I wished...

This is the code:

WebService:

1<%@. WebService Language="VB"Class="SlideShowPictures" %>23Imports System.Web4Imports System.Web.Services5Imports System.Web.Services.Protocols6Imports System.Data7Imports System.Data.SqlClient8Imports System.Web.Profile9Imports System.Web.HttpRequest1011<WebService(Namespace:="http://tempuri.org/")> _12<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _13<Script.Services.ScriptService()> _14Public Class SlideShowPictures15Inherits System.Web.Services.WebService16Public img1As String17 Public img2As String18 Public img3As String19 Public img4As String2021 <System.Web.Script.Services.ScriptMethod()> _22 <System.Web.Services.WebMethod()> _23Public Function GetSlides()As AjaxControlToolkit.Slide()24 ReadPhotoNews()2526Return New AjaxControlToolkit.Slide() {New AjaxControlToolkit.Slide(img1,"News01","Photo01"),New AjaxControlToolkit.Slide(img2,"News2","Photo02"),New AjaxControlToolkit.Slide(img3,"News03","Photo03"),New AjaxControlToolkit.Slide(img4,"News4","Photo04")}2728End Function2930 Public Sub ReadPhotoNews()31Dim idAs Integer =CInt(Me.Context.Application("id"))32Dim rstQueryAs SqlDataReader = MyNameSpace.News.ReadNews(id)33If rstQuery.Read()Then34 img1 = IIf(IsDBNull(rstQuery.GetValue(4)),"", rstQuery.GetValue(4))35 img2 = IIf(IsDBNull(rstQuery.GetValue(5)),"", rstQuery.GetValue(5))36 img3 = IIf(IsDBNull(rstQuery.GetValue(6)),"", rstQuery.GetValue(6))37 img4 = IIf(IsDBNull(rstQuery.GetValue(7)),"", rstQuery.GetValue(7))38End If39 End Sub

ASP.aspx file:

<asp:Image ID="Image1" runat="server"
Height="200"
Style="border: 1px solid black;width:auto" />
<br />
<asp:ImageButton ID="prevButton" runat="server" ImageUrl="images/previous_20_1.gif" />
<asp:ImageButton ID="playButton" runat="server" ImageUrl="images/play_30_1.gif" />
<asp:ImageButton ID="nextButton" runat="server" ImageUrl="images/next_20_1.gif" />
<ajaxToolkit:SlideShowExtender ID="SlideShowExtender1" runat="server"
TargetControlID="Image1"
SlideShowServiceMethod="GetSlides"
SlideShowServicePath="SlideShowPictures.asmx"
AutoPlay="true"
NextButtonID="nextButton"
PlayButtonText="Play"
StopButtonText="Stop"
PreviousButtonID="prevButton"
PlayButtonID="playButton"
Loop="true" />

ASP.aspx.vb File:


Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Application("id") = Request.QueryString("Id")
End Sub



Yes. This is a workaround expected, that you wrap your call inside a method whose signature and param names match up what is passed to the slideshow extender. Although, we would like to improve it. Seebug. All extenders in the toolkit that take in webservice and webmethod as parameters need to be more flexible and we plan to fix that.

Using c#, i am beginning to realize that it is not possible to persist an application variable (or even a session variable) and be able to access it from a static context (from the webmethod GetSlides() that is).

I am hitting my head on the keyboard right now.

Do you perhaps know a workaround in order to be able to pass a variable (originally obtained from "Request" in the same Page's onLoad() ) in the webmethod?


This worked for me

in page load I tack on AlbumID to the SlideShowServiceMethod

protectedvoid Page_Load(object sender,EventArgs e) {

SlideShowExt1.SlideShowServiceMethod ="GetSlides?AlbumID=" + Request.QueryString["AlbumID"];

}

Then in GetSlides() get the string out of the request

[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod]
public static AjaxControlToolkit.Slide[] GetSlides()
{
int AlbumID = int.Parse(HttpContext.Current.Request.QueryString["AlbumID"]);

//gel a list of photos from the album
System.Collections.Generic.List<Photo> photos = PhotoManager.GetPhotos(AlbumID);

// copy relevant data to slides array
AjaxControlToolkit.Slide[] slides = new AjaxControlToolkit.Slide[photos.Count];

int i = 0;

foreach (Photo p in photos)
{
AjaxControlToolkit.Slide slide = new AjaxControlToolkit.Slide();

// must be relative path
slide.ImagePath = "images\\" + p.Location;
slide.Name = p.Caption;
slide.Description = p.Caption;

slides[i] = slide;
i++;
}
return slides;
}


We (Ted one of the Toolkit developers made this change yesterday) have added support to now pass additional data to webservices used by AutoComplete, CascadingDropDown, NumericUpDown and SlideShow so you will no longer need this workaround. You can download the latest source code and build the Toolkit dll and try it out. The demo pages have been updated in the Sample Website as well. Take a look at the manual tests added by Ted that demonstrate the usage. This should be part of the next official Toolkit release around end of this month.

Can somebody post the sample code how this additional parameter could be used in this xample with AlbumID? I looked at the source, but it is confusing. The sample is very limited, jsut describes what context key is for. It would be great to have full sample like that..


The AutoComplete example shows how to pass context pretty well (AutoCompleteWithContext.aspx).

But I think you may have discovered an inherent bug in slideshow.

The function_slideShowInitshould be "public" i.e. it should not start with an underscore so that after the contextkey parameter is changed the slideshow reinvokes the xmlhttprequest to get new data. This could also and actually should also be done by a handler when contextkey is updated or other information about the slideshow is updated (I will need to look into this further). Currently the only time slideshow queries the webservice is onload so even setting the contextkey in pageLoad is too late because it is already completely initialized by then.


The error that some people report about the 'webmethod not found' can be avoided by being sure to set the WebMethod used to a static function. The ajax docs example in the Sample Website leaves that out, and it won't work without it.


I have openedWorkItemId=11887 to track this issue.

No comments:

Post a Comment