Showing posts with label AJAX. Show all posts
Showing posts with label AJAX. Show all posts

Wednesday, May 6, 2009

Implement Ajax AutoComplete Extender inside Gridview

Hi All,
     This Post implements enabling AJAX Autocomplete Extender while Editing Gridview Control in ASP.NET.Do change Connection String as of yours...

So...here it is.....



   <asp:ScriptManager ID="ScriptManager1" runat="server">

        <Services>

            <asp:ServiceReference Path="~/AutoComplete.asmx" />

        Services>

    asp:ScriptManager>

    <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None"

        AutoGenerateColumns="False" OnRowEditing="GridView1_RowEditing">

        <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />

        <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />

        <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />

        <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />

        <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />

        <AlternatingRowStyle BackColor="White" />

        <Columns>

            <asp:CommandField ShowEditButton="True" />

            <asp:TemplateField HeaderText="Age" SortExpression="ID">

                <ItemTemplate>

                    <asp:Label ID="lblID" runat="server" Text=''>asp:Label>

                ItemTemplate>

                <EditItemTemplate>

                    <asp:Label ID="lblID" runat="server" Text=''>asp:Label>

                EditItemTemplate>

            asp:TemplateField>

            <asp:TemplateField HeaderText="Name" SortExpression="Name">

                <ItemTemplate>

                    <asp:Label ID="lblName" runat="server" Text=''>asp:Label>

                ItemTemplate>

                <EditItemTemplate>

                    <asp:TextBox ID="txtName" runat="server" Text=''>asp:TextBox>

                    <ajaxToolkit:AutoCompleteExtender runat="server" ID="autoComplete1" TargetControlID="txtName"

                        ServicePath="AutoComplete.asmx" ServiceMethod="GetCompletionList" MinimumPrefixLength="1" CompletionInterval="10" EnableCaching="true" CompletionSetCount="12" />

                EditItemTemplate>

            asp:TemplateField>

            <asp:TemplateField HeaderText="Location" SortExpression="Location">

                <ItemTemplate>

                    <asp:Label ID="lblLocation" runat="server" Text=''>asp:Label>

                ItemTemplate>

                <EditItemTemplate>

                    <asp:Label ID="lblLocation" runat="server" Text=''>asp:Label>

                EditItemTemplate>

            asp:TemplateField>

        Columns>

    asp:GridView>

 WebService Code

 [WebMethod]

  public DataTable GetRecords(string strName)

    {

        string strConn = ConfigurationManager.ConnectionStrings["TestConnectionString"].ConnectionString;

        SqlConnection con = new SqlConnection(strConn);

        SqlCommand cmd = new SqlCommand();

        cmd.Connection = con;

        cmd.CommandType = System.Data.CommandType.Text;

        cmd.Parameters.AddWithValue("@Name", strName);

        cmd.CommandText = "Select Name from Emp where Name like '%'+@Name+'%'";

        DataSet objDs = new DataSet();

        SqlDataAdapter dAdapter = new SqlDataAdapter();

        dAdapter.SelectCommand = cmd;

        con.Open();

        dAdapter.Fill(objDs);

        con.Close();

        return objDs.Tables[0];

    }

 

    [WebMethod]

    public string[] GetCompletionList(string prefixText, int count)

    {

        if (count == 0)

        {

            count = 10;

        }

        DataTable dt = GetRecords(prefixText);

        List<string> items = new List<string>(count);

        for (int i = 0; i <>

        {

            string strName = dt.Rows[i][0].ToString();

            items.Add(strName);

        }

        return items.ToArray();

    }

    


ASPX.CS Code


protected void Page_Load(object sender, EventArgs e)

    {

        Bind();

     

    }

 

    public void Bind()

    {

        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["TestConnectionString"].ToString());

        conn.Open();

        SqlCommand cmd = new SqlCommand("Select * from EMP", conn);

        DataTable dt = new DataTable();

        SqlDataAdapter da = new SqlDataAdapter();

        da.SelectCommand = cmd;

        da.Fill(dt);

        GridView1.DataSource = dt;

        GridView1.DataBind();

       

    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)

    {

        GridView1.EditIndex = e.NewEditIndex;

        GridView1.DataBind();

    }


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