Wednesday, September 17, 2014

Gridview Function

<asp:GridView ID="TrainingGrid" runat="server" ItemType="TrainingTutorial.Entities.Training"
             DataKeyNames="TrainingID" SelectMethod="trainingGrid_GetData"
             UpdateMethod="trainingGrid_UpdateItem"
             EmptyDataText="No training records available."
             OnRowCommand="trainingGrid_RowCommand" PageSize="5"
             AutoGenerateColumns="False" Width="700px" AllowPaging="True" AllowSorting="True"
             <Columns>
                 <asp:TemplateField HeaderText="Action" ItemStyle-HorizontalAlign="Center">
                     <ItemTemplate>
                         <asp:LinkButton ID="approveButton" runat="server" CommandName="Update" CommandArgument="Approved" Text="Approve" Visible="<%# Item.Status == TrainingTutorial.Entities.TrainingStatus.Pending.ToString()  %>"></asp:LinkButton>
                         <asp:Label ID="Label1" runat="server" Text=" | " Visible="<%# Item.Status == TrainingTutorial.Entities.TrainingStatus.Pending.ToString()  %>"></asp:Label>
                         <asp:LinkButton ID="cancelButton" runat="server" CommandName="Update" CommandArgument="Cancelled" Text="Cancel" Visible="<%# Item.Status == TrainingTutorial.Entities.TrainingStatus.Pending.ToString()  %>"></asp:LinkButton>
                     </ItemTemplate>
                 </asp:TemplateField>
                 <asp:BoundField DataField="TrainingID" HeaderText="ID" Visible="false" ItemStyle-HorizontalAlign="Center">
                 </asp:BoundField>
                 <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-HorizontalAlign="Center" SortExpression="Name">
                 </asp:BoundField>
                <asp:BoundField DataField="EffectiveDate" DataFormatString="{0:dd-MM-yyyy}" HeaderText="Effective Date" />

                <asp:BoundField DataField="EffectiveDate"  DataFormatString="{0:HH:mm}"  HeaderText="Effective Time" />
             </Columns>
         </asp:GridView>

OnRowCommand is to click the button of row
e.CommandArgument is get command arguement , e.g. CommandArgument="Cancelled"

DataKeyNames as an agurment for method (Update)
public void trainingGrid_UpdateItem(int trainingID)

Tuesday, September 16, 2014

Check IE by Javascript

    function getInternetExplorerVersion()
    {
        var rv = -1; // Return value assumes failure.
        if (navigator.appName == 'Microsoft Internet Explorer')
        {
            var ua = navigator.userAgent;
            var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
            if (re.exec(ua) != null)
                rv = parseFloat(RegExp.$1);
        }
        return rv;
}


    function checkVersion()
    {
var msg = "The site is best viewed with Internet Explorer Version 6 and above.\n\nThe page might not be properly displayed if viewed on your current browser.";
      
var ver = getInternetExplorerVersion();

        if (ver > -1)
        {
            if (ver >= 6.0)
                msg = ""
        }

        if(msg != "")
        {
            alert(msg);
        }

}

Saturday, September 13, 2014

Class Constructor

  1. Class Constructor is automatically initialize class field when that instance is created. 
  2. Class Constructor no return values and same name as the class.
  3. If not provide a Class Constructor, default constructor will be used.
  4. Class Constructor can be overloaded by number and type of param.
    class Program
    {
        static void Main()
        {
            Customer c = new Customer();
            c.Printname();

            Customer c1 = new Customer("Water");
            c1.Printname();

            Customer c2 = new Customer("Water","Lily");
            c2.Printname();

            Console.ReadLine();
        }
    }

    class Customer
    {
        string _firstname;
        string _lastname;

        //Default Constructor
        public Customer()
        {
            this._firstname = "Default First Name";
            this._lastname = "Default LastName";
        }

        //Calling another Constructor
        public Customer(string firstname)
            : this(firstname, "No LastName")
        {
        }

        //Parameter Constructor
        public Customer(string firstname, string lastname)
        {
            this._firstname = firstname;
            this._lastname = lastname;
        }

        public void Printname()
        {
            Console.WriteLine(_firstname + " + " + _lastname);
        }

    }

Friday, September 12, 2014

Static vs Instance Method & Static Variables

Instance Method

    class Program
    {
        static void Main()
        {
            Program p = new Program();
            p.ShowMessage();
        }

        public void ShowMessage()
        {
            Console.WriteLine("Message Showed");
            Console.ReadLine();
        }
}

Static Method

    class Program
    {
        static void Main()
        {
            Program.ShowMessage();
        }

        public static void ShowMessage()
        {
            Console.WriteLine("Message Showed");
            Console.ReadLine();
        }
    }


Different
  1. Instance method is for object instance. However Static Method is for class.
  2. Instance cannot call static method.
  3. Static Method take value is use Class Name, this is not work
  4. Static Method - Input given and return output (no state), regular class - input, store/maintain current state.
  5. Static Method - No Abstract, no inherit , no implementing as interface.

Static Variables
no matter how many objects of the class are created, there is only one copy of the static member.


class StaticVar
{
    public static int num;
    public void count()
    {
      num++;
    }
   public int getNum()
   {
      return num;
   }
}

static void Main(string[] args)
{
       StaticVar s1 = new StaticVar();
       StaticVar s2 = new StaticVar();
       s1.count();
       s2.count();
       Console.WriteLine("Variable num for s1: {0}", s1.getNum()); //Variable num for s1: 2
       Console.WriteLine("Variable num for s2: {0}", s2.getNum()); //Variable num for s2 :2
}




Static Function
Such functions can access only static variables. The static functions exist even before the object is created.

class StaticVarMethod
{
    public static int num;
   
    public void count()
    {
      num++;   //calling normal method, but static value change
    }
   public static int getNum()
   {
      return num;
   }
}

static void Main(string[] args)
{
    StaticVarMethod s = new StaticVarMethod();
    s.count();
    s.count();
    Console.WriteLine("Variable num: {0}", StaticVarMethod.getNum());  //Variable num: 2
}



Tuesday, September 9, 2014

Query Strings


  • name/value collection pairs.
  • Send Data from 1 page to another with appended to the URL
  • is beginning of a query string and it's value.
  • & is to append subsequent query string.
Advantage
  • Easy to use
Disadvantage
  • Have max length, cannot send too much data
  • is visible in the URL, not suitable for sensitive informtaion.
  • Cannot send [&] [and] space character. Must Encode.

User Control

  • Custom, reusable controls.
  • Contain HTML and Server that allow other pages to use it. The extension is (.ascx)
Advantages of User Control:
  • Code Reuse.
  • Reduce Development and Testing Time.
  • Cache the output of the control using fragment caching
  • Can program against any properties declared in the control, just like ASP.NET web controls.
Disadvantages of User Control:
  • User Controls can use within application only. 
  • Can't hide code of user controls like server controls by compiling into an assembly.
When to use User Control ?
  1. Seperate in different section. (Divide Page)
  2. Mutiple Page is using the samething.
Compare  between Web Form and User Control 


Web Form
User Control
Extension
.aspx
.ascx
Beginning
@Page
@Control
<html>,<head>,<body>
No
Yes
Code Behind
Yes
Yes

Header

TagPrefix is the infront and TagName is behind for body 

<%@ Register Src="ucPackageAddEdit.ascx" TagName="ucPackageAddEdit" 
TagPrefix="ucPackageAddEdit" %>



Can Accept more than 1 method or properties, 1 and 2 is run the code across from child page to parent page.

1) Accept Param from Parent Page 

Parent Page (asp.net)

<ucPackageAddEdit:ucPackageAddEdit runat="server" ID="ucPackageAddEdit"
OnAddPackageClicked="AddPackageClicked"></ucPackageAddEdit:ucPackageAddEdit>

protected void AddPackageClicked(ServicesProfile serviceProfile)
{
   run code;
}

Child Page (ascx)

public delegate void AddPackageClickedDelegate(ServicesProfile serviceProfile);
public event AddPackageClickedDelegate AddPackageClicked;

protected void btnAdd_Click(object sender, EventArgs e)
{
    AddPackageClicked(serviceProfile);
}



2) No Accept Param  from Parent Page 

Parent Page (asp.net)

<td>

   <ucClientPackage:ucClientPackage ID="ucClientPackage" runat="server"                           OnMESProfileLoad="MESProfileSection_Load">
   </ucClientPackage:ucClientPackage>
</td>

protected void MESProfileLoad(object sender, EventArgs e)
{
   run code;
}

Child Page (ascx)

public event EventHandler MESProfileLoad;

protected void btnAdd_Click(object sender, EventArgs e)
{
   MESProfileLoad(new object(), new EventArgs());

}



3) Get Value  from Parent Page 

Parent Page (asp.net)

<td>

  <uc:action ID="ucAction" runat="server" NewPageURL="client.aspx" VisibleNewButton="true" />
</td>

Child Page (ascx)

public bool VisibleNewButton
{
    get { return pnlNew.Visible;  }
    set { pnlNew.Visible = value}
}

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

string newPageURL;

public string NewPageURL
{
  get { return newPageURL;  }
  set { newPageURL = value}
}

protected void btnAdd_Click(object sender, EventArgs e)
{
  string url = newPageURL;
}