Thursday, December 4, 2008

Yield Keyword in C#

One interesting new feature of the C# 2।0 is the "yield" keyword. Basically it is used to iterate through objects returned by a method. It creates a state engine in IL so you can create methods that retain their state and dont have to go through the pain of maintaining state in your code.



public static IEnumerable GetInt()
{
for (int i = 0; i <>
yield return i;
}


Here's the implementation.


class प्रोग्राम
{ static void Main(string[] args)
{
foreach (int i in GetInt())
Console।WriteLine("Got " + i।ToString());
}


public static IEnumerable GetInt()
{
for (int i = 0; i <>
yield return i;
}
}

Thanks,
Nitin Sharma

Tuesday, December 2, 2008

Consuming Web Service by AJAX SlideShow Extender Control

Hi All,
     Here is a way to Consume Web Service in  AJAX SlideShowExtender Control using Context Key..

in .aspx page::

 if (Request.QueryString["Id"] != null)
        {
            uint Id = Convert.ToUInt16(Request.QueryString["Id"].ToString());
                       
          SlideShowExtender1.ContextKey = Id.ToString(); //Passing the ID attribute Dynamically
            
        }
 ------------------------------------------------------------------------------------
   div align="center"
    asp:Label runat="Server" ID="imageTitle" CssClass="slideTitle" 

    asp:Image ID="ImagePlayer" runat="server" Height="300" Style="border: 1px 
solid black; width: auto" 
    asp:Label runat="server" ID="imageDescription" CssClass="slideDescription"
    asp:Label
    
    asp:Button runat="Server" ID="btnBack" Text="Prev" Font-Size="Larger" 
    asp:Button runat="Server" ID="btnPlay" Text="Play" Font-Size="Larger" 
    asp:Button runat="Server" ID="btnNext" Text="Next" Font-Size="Larger" 
    cc1:SlideShowExtender ID="SlideShowExtender1" runat="server" TargetControlID="ImagePlayer"
        AutoPlay="True" Loop="True" NextButtonID="btnNext" PlayButtonID="btnPlay" PlayButtonText="Play"
        PreviousButtonID="btnBack" SlideShowServiceMethod="GetSlides" SlideShowServicePath="~/Test.asmx"
        StopButtonText="Pause" ImageDescriptionLabelID="imageDescription" ImageTitleLabelID="ImgTitle"
    cc1:SlideShowExtender>
    div

-------------------------------------------------------------------------------------

In .asmx(web service).cs page::
 [WebMethod]
    public AjaxControlToolkit.Slide[] GetSlides(string contextKey)
    {
        System.Web.UI.Page ObjUIPage = new System.Web.UI.Page();
        int i = 0;
        int j = 0;

        //'The below line specifies the Directory to be used.Here the images are in images folder of my application
        System.IO.DirectoryInfo DirInfo = new System.IO.DirectoryInfo(ObjUIPage.Server.MapPath("") + "\\PropertyUnitImages\\" + contextKey);
        System.IO.DirectoryInfo[] subDirs = DirInfo.GetDirectories();

        //'The below line gets the list of files in the directory specified above
        System.IO.FileInfo[] Files = DirInfo.GetFiles();

        //The below line gives the file count, which is useful to specify the size of array
        j = Files.Length;

        Slide[] mySlide = new AjaxControlToolkit.Slide[j + 1];
        //System.IO.FileInfo di = default(System.IO.FileInfo);

        //' This loop continues upto the last file in the directory
        foreach (var d in Files)
        {
            mySlide[i] = new AjaxControlToolkit.Slide(d.FullName, d.Name, d.Name.Substring(0, d.Name.IndexOf(".")));
            i = i + 1;
        }
        return mySlide;
        //' this line sends the dynamically added details as AjaxControlToolkit.Slide
    }
      





A word onContext Key  property of SlideShowExtender:



The parameter is contextKey of type String. You can use contextKey in anyway you want, it is meant to pass information from the calling AJAX control to the web service. In this application, the parameter will be used to supply the directory for the slide show to be fed from. The return parameter is an array of Slides. The AJAX SlideShow control looks for this signature and the signature is case sensitive and exact. Don't try to change any of this syntax or your service not be called.

Thanks,
Nitin Sharma