-->

Friday, August 16, 2013

How to set Height and Width of Elements: WPF

Panels, also called parent elements, supports multiple child elements to be arranged in it. Parents decide about where to render and how much space the children get. WPF elements tend to size to their content that is to be done through its SIzeToContent property.

Height & Width:

There are several properties which are used for layouts of an element, some are size related and some are position related. Size related properties includes height, width, margin and padding. Position related properties includes horizontal & vertical alignment.

All the elements have height and width properties and also they have MinHeight, MinWidth, MaxHeight, MaxWidth to specify the values. Height/Width property is used to get or set the suggested height/width of the respective element.

We can create a textblock with height(100) and width(200) using the below xaml code.
<TextBlock Text="Hello World" Height="100" Width="200" Background="Black"/>
The above code may be written like the below code as we have discussed in our “earlier post”.
<TextBlock>
<TextBlock.Text>Hello World</TextBlock.Text>
<TextBlock.Height>100</TextBlock.Height>
<TextBlock.Width>200</TextBlock.Width>
<TextBlock.Background>Black</TextBlock.Background>
</TextBlock>
The textblock looks like as the following screenshot:

Height and Width property of an element in WPF

Element also contains some more size-related properties like DesiredSize, RenderSize, ActualHeight and ActualWidth. RenderSize represents the final size of an element, and ActualWidth and ActualHeight are exactly same as the final width and final height of an element. DesiredSize is used when IsMeasureValid property is true for an element.

Alignments and Margin & Padding

Thursday, August 15, 2013

ASP.NET : Change label color dynamically

Introduction 
The Label control is used to display the text that the user cannot edit. The label control exists within the System.Web.UI.WebControls namespace.

Public properties of the Label class
AssociatedControlID : Obtains or sets the identifier for a server control the Label control is associated with.

Text : Obtains or sets the text content of the Label control.
BackColor : Obtains or sets the back color of the Label.

Related Top Article
 How to bind label control in asp.net

Example of Change label color dynamically




<%@ Page Language="C#" %>
<!DOCTYPE html>
<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = " Welcome to dotprogramming.blogspot.com";
        Label1.BackColor = System.Drawing.Color.Green;
        Label1.ForeColor = System.Drawing.Color.Orange;
     
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
 
        <asp:Label ID="Label1" runat="server"></asp:Label>
 
    </div>
    </form>
</body>
</html>

Output


ASP.NET : Change label color dynamically

Tuesday, August 13, 2013

How to use Cross-Page Posting in ASP.NET

Postback 
Postback is the process of sending the data back to the server for processing. This is done to authenticate the login and password of a client or other such tasks that a client can not perform on its own. ASP.NET provides a rich framework for handling postbacks from ASP.NET Web pages .Postback includes cross-page posting, which is the name given to the process of receiving the response of a request by the server on another page.

Cross-Page Posting
When we post back data to the server, the server sends the response back to the same page. If we want to receive the response on another page, we use the concept of cross-page posting

Example of Cross-page posting


Step-1 : Take a web form in your project name as "Default3.aspx"

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>  
        Enter Name :
        <asp:TextBox ID="TextBox1" runat="server" Height="19px" Width="165px"></asp:TextBox>
        <br />
        <asp:Calendar ID="Calendar1" runat="server"></asp:Calendar>
        <br />
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Same page poat back" />
        <br />
        <br />
        <asp:Button ID="Button2" runat="server" PostBackUrl="~/Default4.aspx" Text="Cross Page Post back" />
        <br />
        <br />
        <asp:Label ID="Label1" runat="server"></asp:Label>
 
    </div>
    </form>
</body>
</html>

CodeBehind page of same page postback "Default3.aspx.cs"


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Default3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Label1.Text = "hello" + " here is the output of the same page post back button" + Calendar1.SelectedDate.ToString();

    }
}
Step-2 : Take another Webform in your project name as "Default4.aspx"


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
    </div>
    </form>
</body>
</html>

CodeBehind page of Default4.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default4 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Calendar cal = new Calendar();
        TextBox txt = new TextBox();
        cal = (Calendar)PreviousPage.FindControl("Calendar1");
        txt = (TextBox)PreviousPage.FindControl("TextBox1");
        Label1.Text = txt.Text + "here is the output of the cross page post back" + cal.SelectedDate.ToString();

    }
}



How to use Cross-Page Posting in ASP.NET
Different page post backing output




Monday, August 12, 2013

PHP versus ASP : Difference between PHP and ASP

PHP and ASP both are languages specify in web development. These are used in  server side programming language to develop dynamic web applications. We have a compare chat which shows the advantage and disadvantage of both the languages on different parameter.


PHP
ASP
PHP stands for Hypertext Preprocessor.
ASP stands for Active Server Pages.
This is a language which is used to write server side code.
This is also a language which provides server side programming interface to the website.
PHP may be run on most of servers like Apache, IIS,PWS etc.
ASP  program execute only windows server i.e. IIS
This is open source package where user can see the source code, and optimize it.
This is approach from Microsoft technology which is not show the original source code of product.
PHP   programming style familiar with c++.
ASP syntax familiar with BASIC language.
PHP has object oriented features like inheritance, polymorphism, encapsulation etc.
ASP also include object orientation concept.
PHP runs on major platforms like Unix, MAC, Windows etc.
It runs only on windows.
PHP is open source package which is freely available for developers.
This is Microsoft product and we need to purchase license before use it.
PHP sites are no overhead problems so it’s code execute in less time.
ASP has overhead problems which takes time to execute the code.
PHP is user friendly because it is similar to c/c++ and provides better libraries and reference.
In asp we have to write lots of code for developing application.
PHP executed with well managed code.
This is run with unmanaged code.
Cost of development is less because it is freeware.
Cost of development is high because it is license ware.

Use Canvas Panel Control in WPF

Canvas is the basic panel. One can simply place children in a canvas with its attached properties left, top, right and bottom. One should have some knowledge of graph paper to use a canvas panel in WPF. This works as margin property of an element.

I have placed some buttons in a canvas as in following image:


The buttons in above image can be simply placed by using following xaml code:
<Canvas>
<Button Content="First" Canvas.Left="10" Canvas.Top="10"></Button>
<Button Content="Second" Canvas.Top="10" Canvas.Right="10"></Button>
<Button Content="Third" Canvas.Bottom="50" Canvas.Right="50"></Button>
</Canvas>

It is the most lightweight panel for creating flexible user interfaces. One should keep it in mind for maximum performance when one need precise control over the placement of elements. A child can use only two of the canvas attached properties at a time, remaining will be ignored. It means one cant dock an element to more than one corner of a canvas.

Grid Panel Overview in WPF

When a window is added to WPF project then grid is added by default having zero children. It is most often used panel in compare to dock panel, stack panel and other panels. Grid provides no of rows and columns which enables you to arrange the elements without wrapping, stacking. It is like table in programming where we can add no of rows and columns as required.
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
</Grid>
We have to specify the position of each child using either Grid.Row or Grid.Column property of each child in this grid. If we not then it will place all the children in first cell by default.


Grid cells can be either left empty or multiple elements can also be placed in a single cell. Other panels like stack panel or wrap panel can also be used in the grid. Resizing the rows or columns can also be done using Grid Splitter. All this can be also performed by using code behind file.

Grid provides many more in-built functions like sharing width of a column, resizing cells, adding controls to grid and many more.
Canvas panel

Sunday, August 11, 2013

DockPanel Overview in WPF

It is somewhat different with wrap panel. The name Dock panel is created by the word Dock. Dock element provides the user with a way of launching, switching between running applications. Dock panel provides easy docking of elements to an entire side of the panel, stretching it to fill the entire side. It also enables a single element to fill all the remaining space unused by the docked elements.

DockPanel with four children

There are four possible dock in a dock panel i.e. left, top, right and bottom. A children can itself control its dock with these four values. The dock property of any child is left by default. In the above image only four buttons are used, if we add a fifth button in this then the fourth one will shift to bottom and the fifth one will be placed in center like in following image.

DockPanel with five children

Dock panel supports number of children as per requirements. Elements are stacked in the appropriate direction when multiple elements are docked in the same direction. If we will place a sixth element then it will stacked with the First element. The xaml code for the above dock panel image is:
<DockPanel>
<Button Content="First" DockPanel.Dock="Left"></Button>
<Button Content="Second" DockPanel.Dock="Top"></Button>
<Button Content="Third" DockPanel.Dock="Right"></Button>
<Button Content="Four" DockPanel.Dock="Bottom"></Button>
</DockPanel>
Grid panel
© Copyright 2013 Computer Programming | All Right Reserved