Showing posts with label Ajax. Show all posts
Showing posts with label Ajax. Show all posts

Monday, November 25, 2013

Positioning an AutoCompleteExtender list in relation to its target control

To make the Auto Complete Extender display always below the Target textbox using JavaScript.

Step 1: Create a function in JavaScript as mentioned below

function resetPosition(object, args) {
    var tb = object._element;
    var tbposition = findPositionWithScrolling(tb);
    var xposition = tbposition[0];
    var yposition = tbposition[1] + 20; // 22 textbox height
    var ex = object._completionListElement;
    if (ex)
        $common.setLocation(ex, new Sys.UI.Point(xposition, yposition));
}
function findPositionWithScrolling(oElement) {
    if (typeof (oElement.offsetParent) != 'undefined') {
        var originalElement = oElement;
        for (var posX = 0, posY = 0; oElement; oElement = oElement.offsetParent) {
            posX += oElement.offsetLeft;
            posY += oElement.offsetTop;
            if (oElement != originalElement && oElement != document.body && oElement != document.documentElement) {
                posX -= oElement.scrollLeft;
                posY -= oElement.scrollTop;
            }
        }
        return [posX, posY];
    } else {
        return [oElement.x, oElement.y];
    }
}

Step 2: Add the following code in the AjaxControlToolkit: AutoCompleteExtender

 OnClientShown="resetPosition" 

 
Save your code and refresh the page issue has been resolved..!!

Sunday, April 28, 2013

Download / Upload any file and Display Message using C# in ASP.NET

* To display information using message box in ASP.NET page from server side code.

static public void DisplayAJAXMessage(Control page, string msg)
    {
            string myScript = String.Format("alert('{0}');", msg);
            ScriptManager.RegisterStartupScript(page, page.GetType(), "MyScript", myScript, true);      
    }


e.g. : DisplayAJAXMessage(this, "Hello World");
OUTPUT: 
Fig 1: Output of DisplayAjaxMessage


* Download a file from server using link button:

protected void lnkDownloadFile_Click(object sender, EventArgs e)
    {
        try
        {
            string FileName = string.Empty;
            FileName = "demo.xls"; // Any File Name with extension
            string link = Server.MapPath("<~/Folder Name/>" + FileName); //Optional Folder Name
            FileInfo myfile = new FileInfo(link);
            Response.AddHeader("Content-Disposition", "attachment; filename=" + myfile.Name);
            Response.AddHeader("Content-Length", myfile.Length.ToString());
            Response.ContentType = ReturnExtension(myfile.Extension.ToLower());
            Response.TransmitFile(myfile.FullName);
            Response.Flush();
            Response.End();
        }
        catch (Exception ex)
        {
            DisplayAJAXMessage(this, "File Not Found");
        }
    }


private string ReturnExtension(string fileExtension)
    {
        switch (fileExtension)
        {
            case ".xls":
                // case ".csv":
                return "application/vnd.ms-excel";
            default:
                return "application/octet-stream";
        }
    }


* Upload any file to server using C# in ASP.NET 
(Note: This example shows to upload excel file (.xls) , Just change the extension of file as per requirement to upload any type file.)

  protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            if (UploadFile.HasFile) //FileUpload tool from ToolBox
            {
                string Extension = Path.GetExtension(UploadFile.PostedFile.FileName);
                if (Convert.ToString(Extension).ToLower() != ".xls")
                {
                    lblError.Text = "Please select a file with extension .xls before validation";
                    return;
                }
            }
            else
            {
                lblError.Text = "Please select a file with extension .xls before validation";
                return;
            }
            string uploadFileName = Path.GetFileName(UploadFile.PostedFile.FileName);
            int index = uploadFileName.LastIndexOf('.');
            string SheetName = uploadFileName.Substring(0, index);
            string FileName = Path.GetFileName(UploadFile.PostedFile.FileName);
            string FilePath = Server.MapPath("~//" + FileName); //Optional FolderName
            UploadFile.SaveAs(FilePath);
         }
        catch (Exception ex)
        {
           DisplayAJAXMessage(this,"Error::"+ex);

        }
    }


Happy Coding 



View Amit Lal's profile on LinkedIn

Software Developer, Academic Writing Writer
Showing posts with label Ajax. Show all posts
Showing posts with label Ajax. Show all posts

Monday, November 25, 2013

Positioning an AutoCompleteExtender list in relation to its target control

To make the Auto Complete Extender display always below the Target textbox using JavaScript.

Step 1: Create a function in JavaScript as mentioned below

function resetPosition(object, args) {
    var tb = object._element;
    var tbposition = findPositionWithScrolling(tb);
    var xposition = tbposition[0];
    var yposition = tbposition[1] + 20; // 22 textbox height
    var ex = object._completionListElement;
    if (ex)
        $common.setLocation(ex, new Sys.UI.Point(xposition, yposition));
}
function findPositionWithScrolling(oElement) {
    if (typeof (oElement.offsetParent) != 'undefined') {
        var originalElement = oElement;
        for (var posX = 0, posY = 0; oElement; oElement = oElement.offsetParent) {
            posX += oElement.offsetLeft;
            posY += oElement.offsetTop;
            if (oElement != originalElement && oElement != document.body && oElement != document.documentElement) {
                posX -= oElement.scrollLeft;
                posY -= oElement.scrollTop;
            }
        }
        return [posX, posY];
    } else {
        return [oElement.x, oElement.y];
    }
}

Step 2: Add the following code in the AjaxControlToolkit: AutoCompleteExtender

 OnClientShown="resetPosition" 

 
Save your code and refresh the page issue has been resolved..!!

Sunday, April 28, 2013

Download / Upload any file and Display Message using C# in ASP.NET

* To display information using message box in ASP.NET page from server side code.

static public void DisplayAJAXMessage(Control page, string msg)
    {
            string myScript = String.Format("alert('{0}');", msg);
            ScriptManager.RegisterStartupScript(page, page.GetType(), "MyScript", myScript, true);      
    }


e.g. : DisplayAJAXMessage(this, "Hello World");
OUTPUT: 
Fig 1: Output of DisplayAjaxMessage


* Download a file from server using link button:

protected void lnkDownloadFile_Click(object sender, EventArgs e)
    {
        try
        {
            string FileName = string.Empty;
            FileName = "demo.xls"; // Any File Name with extension
            string link = Server.MapPath("<~/Folder Name/>" + FileName); //Optional Folder Name
            FileInfo myfile = new FileInfo(link);
            Response.AddHeader("Content-Disposition", "attachment; filename=" + myfile.Name);
            Response.AddHeader("Content-Length", myfile.Length.ToString());
            Response.ContentType = ReturnExtension(myfile.Extension.ToLower());
            Response.TransmitFile(myfile.FullName);
            Response.Flush();
            Response.End();
        }
        catch (Exception ex)
        {
            DisplayAJAXMessage(this, "File Not Found");
        }
    }


private string ReturnExtension(string fileExtension)
    {
        switch (fileExtension)
        {
            case ".xls":
                // case ".csv":
                return "application/vnd.ms-excel";
            default:
                return "application/octet-stream";
        }
    }


* Upload any file to server using C# in ASP.NET 
(Note: This example shows to upload excel file (.xls) , Just change the extension of file as per requirement to upload any type file.)

  protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            if (UploadFile.HasFile) //FileUpload tool from ToolBox
            {
                string Extension = Path.GetExtension(UploadFile.PostedFile.FileName);
                if (Convert.ToString(Extension).ToLower() != ".xls")
                {
                    lblError.Text = "Please select a file with extension .xls before validation";
                    return;
                }
            }
            else
            {
                lblError.Text = "Please select a file with extension .xls before validation";
                return;
            }
            string uploadFileName = Path.GetFileName(UploadFile.PostedFile.FileName);
            int index = uploadFileName.LastIndexOf('.');
            string SheetName = uploadFileName.Substring(0, index);
            string FileName = Path.GetFileName(UploadFile.PostedFile.FileName);
            string FilePath = Server.MapPath("~//" + FileName); //Optional FolderName
            UploadFile.SaveAs(FilePath);
         }
        catch (Exception ex)
        {
           DisplayAJAXMessage(this,"Error::"+ex);

        }
    }


Happy Coding 



View Amit Lal's profile on LinkedIn

Software Developer, Academic Writing Writer