-->

Saturday, August 17, 2013

Margin and Padding of Elements: WPF

In our previous post we have set Height and Width properties of an element, now we will set the next size related properties i.e. Margin and Padding. Margin controls how much extra space gets placed around the outside edges of the element, whereas padding controls around the inside edges of the element.

Margin and Padding can be assign in following simple ways:
  • Margin = "10", same margin of all four sides.
  • Margin = "10,5", 10 for left & right, 5 for top & bottom.
  • Margin = "10,5,10,5", for left, top, right and bottom respectively.
Same can be used for padding. Here is xaml code with three buttons having some margins.
<StackPanel Orientation="Horizontal">
<Button Margin="10">Button 1</Button>
<Button Margin="20,5">Button 2</Button>
<Button Margin="20,15,20,15">Button 3</Button>
</StackPanel>
As padding controls around the inside edges of the element, I have used a border across each button:
<StackPanel Orientation="Horizontal" Height="60">
<Border Background="Aqua" BorderThickness="2">
<Button Padding="10">Button 1</Button>
</Border>
<Border Background="Aqua">
<Button Padding="10,15">Button 2</Button>
</Border>
<Border Background="Aqua">
<Button Padding="20,15,20,15">Button 3</Button>
</Border>
</StackPanel>
The effect of padding will look like in the following screenshot:

Margin and Padding of Elements in WPF

Varying the values of these margins and paddings, we can measure the changes. If one want to use all these margins through code behind then we have to use Thickness class like the below code:
Button button1 = new Button();
button1.Margin = new Thickness(10);
button1.Margin = new Thickness(20, 15, 20, 15);

As we can see that either we can use a single value or all the four values to use margin property of an element. There is no option to use the second type i.e. the one for left & right and one for top & bottom.

Alignments and Height & Width

ASP.NET: How to use Generic Handlers

Example of Generic Handlers
Step-1 : Add new "GenericHandlers.ashx" file in your project
 

<%@ WebHandler Language="C#" Class="Handler" %>

using System;

using System.Web;

public class Handler : IHttpHandler {



public void ProcessRequest (HttpContext context) {

context.Response.ContentType = "html";



context.Response.Write("Hello World");


manaeform(context);





}
public void manaeform(HttpContext context)


{
context.Response.Write("<html><body><form>");

context.Response.Write("<h2>Select your feature</h2>");

if (context .Request .Params ["Feature"]==null)


{
context.Response.Write("<select name='Feature'>");

context.Response.Write("<option>asp.net </option>");

context.Response.Write("<option>java </option>");
context.Response.Write("</select></form></body></html>");


}

}


public bool IsReusable {

get {

return false;


}

}

Output
ASP.NET: How to use Generic Handlers

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.
© Copyright 2013 Computer Programming | All Right Reserved