Skip to main content

Posts

Showing posts from 2014

How to post a dynamic form using json data?

// Post dynamic form using json data   function post(uri, data, method) { // when method not provided use default post method method = method || "post" ;     var form = document.createElement( "form" ); form.setAttribute( "method" , method); form.setAttribute( "action" , uri); for ( var property in data) {   if (params.hasOwnProperty(property)) {     var hiddenField = document.createElement( "input" );     hiddenField.setAttribute( "type" , "hidden" );     hiddenField.setAttribute( "name" , property);     hiddenField.setAttribute( "value" , data[property]);      form.appendChild(hiddenField);   } } document.body.appendChild(form); form.submit(); }

How to Gzip and export a csv stream in web usig c#

using System.IO.Compression; public static void ExportAsGZip(Stream stream, string fileName) {     HttpContext.Current.Response.ContentType = "application/gzip";     HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment;filename={0}.gz", fileName));     using (GZipStream compressionStream = new GZipStream(HttpContext.Current.Response.OutputStream, CompressionMode.Compress))     {       stream.CopyTo(compressionStream);     } }  

Generic method to convert enum datatype to select list item

// Generic method to convert enum datatype to select list items public static List < SelectListItem > GetEnumSelectListItems<T>() {     List < SelectListItem > list = new List < SelectListItem >();     foreach (T type in Enum .GetValues( typeof (T)))     {         list.Add ( new SelectListItem ()  {              Selected = false ,             Text = type.ToString(),             Value = type.ToString()         } );     }     return list; }

open a url in a new window in html javascript

function OpenPopUpWindow(url) { params = 'width=' + Math.round(screen.width * .90); params += ', height=' + screen.height; params += ', top=0, left=' + Math.round(screen.width * .05); params += ', fullscreen=no' ; params += ', scrollbars=yes' ; var newwin = window.open(url, 'windowname' , params); if (window.focus) { newwin.focus() } if ( typeof this .stopPropagation != 'undefined' ) { this .stopPropagation(); } return false ; }

ajax post request

$.ajax({   url: url,   type: "POST" ,   contentType: 'application/json' ,   data: JSON.stringify($scope.Model) }) .done(successCallback) .fail(failureCallback); function successCallback(){    // on success } function failureCallback(){    // on failure }

Creating angular scope base with angular bootstrap (my custom base model)

<div id="myArea" ng-controller="myController"> </div> <script type="text/javascript">     angular.bootstrap(document.getElementById("myArea"), ["myController"]); </script> var my = angular.module('my', []); var myScope = null; function myController($scope, $timeout) {     // Enable global access of this scope .     if (myScope == null)         myrScope = $scope; if (typeof _myModel != 'undefined' && _myModel && _myModel != "") {         $scope.Model = angular.extend($scope.Model, _myModel);     } }

Convert xml to c# object and c# object to xml ( serialize and deserialise xml to c# object)

    public static string Serialize<T>(T dataToSerialize)     {         using (StringWriter stringwriter = new System.IO.StringWriter())         {             var serializer = new XmlSerializer(typeof(T));             serializer.Serialize(stringwriter, dataToSerialize);             return stringwriter.ToString();         }     }     public static T Deserialize<T>(string xmlText)     {         using (StringReader stringReader = new System.IO.StringReader(xmlText))         {             var serializer = new XmlSerializer(typeof(T));             return (T)serializer.Deserialize(stringReader);         }     }

Making Sense of ASP.NET Paths

Nice article found on the following path about Asp.Net Paths. Ref: http://weblog.west-wind.com/posts/2009/Dec/21/Making-Sense-of-ASPNET-Paths Request Property Description and Value ApplicationPath Returns the web root-relative logical path to the virtual root of this app. /webstore/ PhysicalApplicationPath Returns local file system path of the virtual root for this app. c:\inetpub\wwwroot\webstore PhysicalPath Returns the local file system path to the current script or path . c:\inetpub\wwwroot\webstore\admin\paths.aspx Path FilePath CurrentExecutionFilePath All of these return the full root relative logical path to the script page including path and scriptname. CurrentExcecutionFilePath will return the ‘current’ request path after a Transfer/Execute call while FilePath will always return the original request’s p

How to get all html img tag (image) src uri from a html content?

public List<string> GetAllImageUri(string content) {     string pattern = @"<(?<Tag_Name>img)\b[^>]*?\b(?<URL_Type>(?(1)src))\s*=\s*(?:""(?<URL>(?:\\""|[^""])*)""|'(?<URL>(?:\\'|[^'])*)')";       // Getting collection of matched uri.     MatchCollection imageUriCollection = Regex.Matches(content, pattern, RegexOptions.IgnoreCase);       if (imageUriCollection.Count > 0)     {           return imageUriCollection.Cast<Match>() .Select(x => x.Groups["URL"].Value) .ToList();     }     return null; }