-->

Sunday, June 30, 2013

How to use ImageButton Control in ASP.NET

Introduction
The ImageButton control is a Button control that displays an image instead of text . The ImageButton control exists within the System.Web.UI.WebControls namespace . This control is specifically useful when you want to create image maps . Image maps are clickable regions on images and can initiate various actions depending on part of the image that you click.

Public Properties of the ImageButton Class:

CauseValidation : Obtains or set a value showing whether or not validation is performed when the ImageButton control is clicked.

CommandArgument : Obtains or sets a argument that provides information about the CommandName property

CommandName : Obtains or sets the command name related to the ImageButton control

Enabled : Obtains or sets a value indicating whether the ImageButton control can be clicked to perform a PostBack operation to the server.
GenerateEmptyAlternateText : Obtains or sets a value showing whether the control generates an alternate-text attribute for an empty string value.

OnClientClick : Obtains or sets the client-side script , which is executed when an ImageButton control's Click event fired.

PostBackUrl : Obtains or sets the url of the web page to post from the current web page when the ImageButton control is clicked.

ValidationGroup : Obtains or sets the group of controls for which the ImageButton control cause validation when they are post back to the server.


Public Events of the ImageButton Class
Click : occurs when the ImageButton is clicked . The event cause the page to be posted back to the server.

Command : occurs when the ImageButton is clicked . The Command event is raised through this control hierarchy in the form of the BubbleEvent.

Example

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="imageButton.aspx.cs" Inherits="imageButton" %>

<!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="Image Button Control Example"></asp:Label>
        <br />
        <asp:Label ID="Label2" runat="server" Text="Click any where on the  ImageButton Control Bellow"></asp:Label>
 
    </div>
        <asp:ImageButton ID="ImageButton1" runat="server" Height="81px" ImageUrl="~/samsung.jpg" OnClick="ImageButton1_Click" Width="189px" />
        <br />
        <br />
        <asp:Label ID="Label3" runat="server" Text="You Clicked on the image button at following co-ordinate"></asp:Label>
        <p>
            <asp:Label ID="result" runat="server" BackColor="#FF6600"></asp:Label>
        </p>
    </form>
</body>
</html>

Code Behind Code


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class imageButton : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        result.Text = e.X.ToString() + "," + e.Y.ToString();
    }
}
OutPut


imagebutton
 
 

Saturday, June 29, 2013

Find a control in windows forms

When we drag-n-drop controls in our designer file then we can easily access those controls in our code file directly by its name. But when we dynamically add some controls either in constructor or in load event of form then we can't access them directly in our code.

Let's have an example

  • Add a textbox and a button using drag-n-drop operation on our form i.e. Form1.
  • Leave the name of controls i.e. textBox1 and button1 as they are.
  • Now in constructor add a new textbox dynamically having name "txtBox" with some properties like:

    TextBox txtBox = new TextBox();
    txtBox.Name = "txtBox";
    txtBox.Text = "dynamically added control";
    txtBox.Width = 250;
    txtBox.Location = new Point(10, 10);

    this.Controls.Add(txtBox);
In above code the Name property will be used to find this control. All the remaining properties are familiar to you as in our designer file. At the last we have added this textbox to the form, here the keyword this is pointing Form1.
This form will look like the following screenshot: 

find control in windows form


In click event of button when we will try to access above controls then we notice that "textBox1" will be accessed by its name and "txtBox" will not accessed here.

To access these dynamically added controls we have to use a pre-defined method Find(). This method searches the controls by their name and builds an array as in below code.

Control[] controls = this.Controls.Find("txtBox", false);
foreach (var item in controls)
{
     TextBox txtBox = item as TextBox;
     if (txtBox != null)
        MessageBox.Show("control accessed");
}

In above set of code "controls" will hold the array of all the controls having name "txtBox". One by one we will access items of array and check the type of item. If type of item is TextBox then we found our textbox having name "txtBox".

As we know about naming standards of programming that two controls of the same name cannot exists. Keep in mind the above line, and we are sure that, there will only one control named “txtBox” of type TextBox.

Now we will access the above textbox in one line of code i.e.

TextBox txtBox1 = this.Controls.Find("txtBox", false).First() as TextBox;
if (txtBox1 != null)
 MessageBox.Show("control accessed");

When we run our project and click on button then our form look like this.
find control


Online Voting System Project in ASP.NET

Introduction:
Online voting system is a web based solution.It removes traditional voting system problems such as :
  • Take more time and human resources
  • Does not give instant poll result
  • False voter
  •  Inefficient

Online voting system project



Those problem are major problem in traditional system . If you want to remove such types of problem in traditional system then you will use web-based solution.

Features of Online voting system:
  • Detect false voter
  • Instant poll result
  • Easy method for vote count
  • keep global information
System Requirements : 
  • Visual studio 2010 
  • SqlServer 2008
  • Internet connection
  • Mail_id
 How to run this project :

  • Open website folder in Visual studio 2010
  • Run your project by selecting green triangle button
  • Open voter register page 
  • Fill some required filled and click on submit button
  • Login in Admin panel by username and password
  • Approve Voter by changing flag bit (0 to 1)
  • After approval login in your mail id and copy your secure code
  • Login in voter login panel and fill some required filled(voter-id , securecode , email )
  • After login you can vote now of your desired candidate .
  • Count total vote by admin
  • Show result by admin
If you want to purchase this please contact me on :  narenkumar851@gmail.com

Project Demo

How to use ValidationSummary Control in ASP.NET

Introduction
The ValidationSummary control collects all the validation control error message (known as summary) and displays it collectively on the screen . The display of the summary , which can be a list , a bulleted list , or a single paragraph , can be set by using the DisplayMode property . You can also specify whether the summary should be displayed in the web page or in a message box by setting the ShowSummary and ShowMessageBox properties respectively . The validationSummary control exists within the System.Web.UI.WebControls namespace.
Public Properties of  ValidationSummary Class

DisplayMode : Obtains or set the display mode of the ValidationSummary Control.

EnableClientScript : Obtains or set a value indicating whether the ValidationSummary  control updates itself using the client-side script.

ForeColor : Obtains or sets the foreground color of the control.

HeaderText : Obtains or sets the header text displayed at the top of the summary.

ShowMessageBox : Obtains or sets a value showing whether the validation summary is displayed in a message box.

ShowSummary : Obtains or sets a value showing whether the validation summary is displayed in line.

ValidationGroup : Obtains or set the group of controls for which the ValidationSummary object displays validation message.

Example :

<p>
<h2>Login for security</h2></p>
<asp:ValidationSummary ID="ValidationSummary1" runat="server" BackColor="#CCCC00" BorderColor="Black" BorderStyle="Solid" BorderWidth="4px" />
<p>
Enter member username :&nbsp;
<asp:TextBox ID="usrtxt" runat="server" Width="182px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="usrtxt" ErrorMessage="Enter Username" ForeColor="Maroon">*</asp:RequiredFieldValidator>
</p>
<p>
Enter&nbsp; member password : <asp:TextBox ID="pwd" runat="server" TextMode="Password" Width="182px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="usrtxt" ErrorMessage="Enter password" ForeColor="Maroon">*</asp:RequiredFieldValidator>
</p>
<p>
<asp:Button ID="Button1" runat="server" Text="Member Login" OnClick="Button1_Click" />
</p>
<p>
&nbsp;</p>


Output
validationSummary




Friday, June 28, 2013

How to use RegularExpressionValidator Control in ASP.NET

Introduction
Regular expression are used to check whether or not the text matches a certain pattern . The RegularExpressionValidator control validates whether the value in the input control (text box) matches the format specified by the regular expression , such as a US telephone number or a date format . The RegularExpressionValidator control exists within the System.Web.UI.WebControls namespace.
In general , regular expression are made up of text within an embedded code listing that starts with a backslash (\) as well as other control code listing . The following is an example:
\b[A-Za-z]+\b
The code for a word boundary (where a word ends or starts ) is \b and a character class is a set of characters surrounded by [ and ] . This allows you to specify what character you want to accept . So , this regular expression matches a word consisting of uppercase or lowercase letters only. The + sign stands for one or more of , so we matching one or more uppercase and / or lowercase letters here.

for example , the regular expression to determine if the text matches a valid e-mail address is :
\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
Here , \w stands for a word character (such as letters , underscores, and the rest) and * means zero or more of .
Public Property of the RegularExpressionValidator Class

ValidationExpression : Obtains or sets the regular expressions that you want to match data against for validation.

Example


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>



Enter your Email Address:
 
 
<asp:TextBox ID="Emailtxt" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="Emailtxt" Display="Dynamic" ForeColor="Maroon">Email format incorrect</asp:RegularExpressionValidator>
&nbsp;<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="Emailtxt" Display="Dynamic" ErrorMessage="Fill must" ForeColor="Maroon"></asp:RequiredFieldValidator>
<br />
<asp:Button ID="Button1" runat="server" Text="Submit" />
<br />
</div>
</form>
</body>
</html>
OutPut
emialvalidator

Thursday, June 27, 2013

How to use RequiredFieldValidator Control in ASP.NET

Introduction

The Required FieldValidator control is the simplest control , and is used to ensure that the user has entered the data into the input control, such as TextBox to which it is bound. For example , if you are entering data, for buying a T-shirt , in an input control , it is necessary to enter the size of the T-Shirt that you want to buy. If you do not enter a value , the validation control displays an error message .

Public property of the RequiredFieldValidator Class

initialValue : Handles the initial value of the associated input control.

Example



<%@ Page Language="C#" AutoEventWireup="true" CodeFile="requiredfield.aspx.cs" Inherits="requiredfield" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

<title></title>

</head>

<body>

<form id="form1" runat="server">

<div>






UserName :
 
 
<asp:TextBox ID="usrtxt" runat="server"></asp:TextBox>

<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="usrtxt" ForeColor="Maroon">Field is required</asp:RequiredFieldValidator>

<br />

<asp:Button ID="Button1" runat="server" Text="Submit" />





 
 
</div>

</form>

</body>

</html>

 
Output
validator
 
© Copyright 2013 Computer Programming | All Right Reserved