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
 
 

Tidak ada komentar:

Posting Komentar