Minggu, 30 Juni 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
 
 

Sabtu, 29 Juni 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


Your project will submit after 5 hour 
mail to me : narenkumar851@gmail.com

Project Demo