Difference between Page.IsPostBack and Page.IsCallBack

Page.IsCallBack

It is getting a value indicating whether the page request is the result of a call back. Its a special postback, so a round-trip always occurs; however, unlike the classic postback, the script callback doesn’t redraw the whole page. ViewState is not updated during a callback, it is for postback. IsPostBack is true when a method in the code behind posts a page

Page.IsPostBack

Checks whether the Page is accessing the server for the first time or not. Unlike the IsCallBack, the ViewState is updated. IsPostBack is true when an AJax call, calls back a the page.

How to make a callback?

In the client side JavaScript code, if GetCallbackEventReference() method is reference, then when the JavaScript code is executed, a channel to the server is opened and an HTTP request is sent to the remote ASP.NET page.[1]

references: [1] Script Callback

Advertisements

Linq To XML: Generate Types with Nested Types using C#

In this article, we will have look at how can we generate Types with subtypes using Linq To XML

Suppose we have an XML file which holds information about Cars as their Model Name, Transmission details, Engine Details and some other information, like follows

<?xml version="1.0" encoding="utf-8" ?> 
<Cars>
 <Car Name = "Golf">
    <Transmission Type="Manual" Gears="5" />
    <Engine Type="diesel" Power="2000" />
 </Car>
 <Car Name = "Mazda">
    <Transmission Type="Automatic" Gears="4" />
    <Engine Type="Patrol" Power="2000" />
 </Car>
</Cars>

Now we have class named Car as Follows

    class Car
    {
        // struct Transmission
        public Transmission Transmission { get; set; }
        // struct Engine
        public Engine Engine { get; set; }
    }

Class car has two properties which are structs, Transmission and Engine

    struct Engine
    {
        // Type of Engine
        public string Type
        { get; set; }

        // Power of Engine
        public int Power
        { get; set; }
    }
    struct Transmission
    {
        // Type of Transmission
        public string Type
        { get; set; }

        // Type of Transmission
        public int Gears
        { get; set; }

        public override string ToString()
        { return Type + " " + Gears; }
    }

This is how we can parse the above XML document using XElement and Linq To XML and generate the required classes

                 // Load Document using XElement
                 XElement elements = XElement.Load("XMLFile.xml");

                 // using Linq To Xml parse and load Objects
                 List<Car> CarsList =
                 (from e in elements.Descendants("Car")
                 // get new Car object
                 select new Car
                 {
                     // get Name for the Car
                     Name = e.Attribute("Name").Value,

                     // get Transmission details for the Car
                     Transmission = new Transmission()
                     {
                         Type = e.Element("Transmission").Attribute("Type").Value,
                         Gears = Convert.ToInt32(e.Element("Transmission").Attribute("Gears").Value)
                     },
                     // get Engine details for the Car
                     Engine = new Engine()
                     {
                         Type = e.Element("Engine").Attribute("Type").Value,
                         Power = Convert.ToInt32(e.Element("Engine").Attribute("Power").Value)
                     }
                 }).ToList<Car>();

To work over the returned List of cars we can use many methods, one of which is foreach

            foreach (Car car in CarsList)
            {                
                Console.WriteLine(car.Name);                
                Console.WriteLine(car.Engine.Type);
                Console.WriteLine(car.Transmission.ToString());
                Console.WriteLine("--------------");  
            }

here is the output:

Difference Between Debug and Release

Debug:

  • Complete symbolic debug information is emitted to help while debugging applications
  • code optimization is not taken into account
  • Release

  • In release build, symbolic debug information is not emitted.
  • Release build is considerably faster and more optimized
  • Code written in “debug” directive is not included in execution
  • Managing Configuration Details:
    To manage the configuration details for our project we can provide custom configuration by: right click Solution -> configurationManager, or use existing settings by Project –> Propeties / Build.

    Using attributes in c#

    In the .NET Framework there are many reasons to use attributes, like

  • Defining which classes are serializable
  • Choosing which methods are exposed in a Web service
  • As per definition at MSDN
    Attributes allow us to add descriptions to classes, properties, and methods at design time that can then be examined at runtime via reflection.”

    Consider this example:
    Say you have a class which has a method from older version which is still in use for any reason and now you have come up with a new version of the class which makes fantastic use of Generic List and LINQ and has a new method for similar purpose. You would like developers to prefer the new one provided in the later version of your library. How will you do that ? One way is to write in the documentation. A better way is to use attribute as follow.

    public class AccountsManager
        {
          [Obsolete("prefer GetAccountsList", true)]
          static Account[] GetAccounts( ) { }    
          static List<Account> GetAccountsList( ) { }      
        }
    

    If an obsolete method is used when the program is compiled, the developer gets this info and decides accordingly.

    AccountManager.GetAccounts() is obsolete: prefer GetAccountsList

    We may also create and add Custom Attributes as per requirements.

    References :

  • Use the advantages of custom attributes in your C# applications
  • Using Attributes in C#
  • IS F# going to replace C#

    One tool can not fit all needs.

  • Did Asp.Net MVC replace Asp.Net Forms ? No, both are used according to the project needs.
  • Did Vb.Net replace C# ? No, both provide almost same functionality and options but pull developers from both families. Vb and C.
  • Has WPF replaced, WinForms ? not yet, might in future.
  • Chris Smith’s on his Blog, says

    ” But just because you can write code in a new language doesn’t mean you should. So why use F#? Because being a functional language, F# makes writing some classes of programs much easier than its imperative cousins like C#. Parallel Programming and Language-Oriented Programming are two such domains that can be expressed easily in F#.

    If you’ve ever written a .NET application and found yourself fighting against the language to get your idea expressed, then perhaps F# is what you’ve been looking for.”

    F# is basically an functional language by its nature. The argument here could be more towards the style of programming rather than features a language may support.

    As per Eric Lipert answer to a similar topic ( my post was inpired to ) at stackoverflow, following things are to be considered before one thinks about using F# as the language of choice for a project.

  • F# cannot solve any problem C# could.
  • F# is a functional language, statically typed.
  • F# is a functional language that supports O-O-Programming
  • more information and references:

    Microsoft F# Developer Center
    F# at Microsoft Research

    And to:
    Learn F#
    download F#
    F# FEBRUARY 2010 RELEASE

    Working with Asp.Net variables in c#

    Variables don’t automatically maintain state across page calls in ASP.NET. Asp.Net Page has a life cycle and being stateless, the information is lost at the end of this cycle, after a request has been served on the client side. One way is to use View-state for the persistence of the variables, but this significantly increases the size of a page and the data sent and received, making it slow. There are several solution for this.

  • SessionState
  • hidden fields
  • querystrings
  • Here is SessionState example

    // saving aHiddenVariable 
    Session["aHiddenVariable"]="aHiddenVariable";
    
    // retreiving aHiddenVariable 
    string aHiddenVariable = (string)Session["aHiddenVariable"];
    

    Here is hidden field example.

    In HTML, we simply need to create:

    <input type="hidden" id="aHiddenVariable">
    

    To set it’s value in javascript:

    document.getElementById("aHiddenVariable").value = "myValue"
    

    In ASP.NET code-behind, you use the Request object to retrieve the value.

    string aHiddenVariable= (string)Request.Params["aHiddenVariable"];
    

    Here is a sample for QueryString

    // setting quesryString
    // set s=feed, "text" to search
    http://codingphobia.com/?s=feed
    
    // retrieve variable
    string textToSearch= Request.QueryString["s"];
    

    Passing indefinite parameters as Method arguments in C#

    One might have a situation where he or she needs to pass a number of variables as arguments at runtime. When we are sure of the parameters required, we can declare them in method signature, like

             public static void PrintMessage(string message, int times)
            {
                for (int i = 0; i < times; i++)
                    Console.WriteLine(message);
            }
    

    In the above example we know that we need a message as string and an int (times) which would be used to display the message a number of times. What if we do not know how many variables of some type or different types are required at compile time, like print a number of messages, together or message or names . Here is params keyword which will gracefully tackle this situation. If we need list of argument of particular type, like string

             public static void PringtMessages(params string[] stringsList)
            {
                foreach (string s in stringsList) { }   
                // your logic here
            }
    
            // this is how to call
            string[] messages = new string[] { "Message1", "Message2" };
            PringtMessages(messages);
    

    This is how, if we a need to pass different type of objects as indefinite number of arguments

            public static void PrintMessageWithID(params object[] list)
            {
                foreach (string s in list) { } 
                // your logic here
            }
            object[] messageAndId = new object[] { "Message1", 1,  "Message2", 2 };
            PrintMessageWithID(messageAndId);