ASP.NET Tutorial/ASP.net Controls/ImageButton

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Important properties, methods and events of ImageButton control

AccessKey:       a key that navigates to the ImageButton control.
AlternateText:   alternate text for the image (required for accessibility).
DescriptionUrl:  a link to a page that contains a detailed description of the image (required to make a complex image accessible).
CommandArgument: a command argument that is passed to the Command event.
CommandName:     command name that is passed to the Command event.
Enabled:         disable the ImageButton control.
GenerateEmptyAlternateText: set the AlternateText property to an empty string.
ImageAlign:      align the image relative to other HTML elements in the page. 
                 Possible values are AbsBottom, AbsMiddle, Baseline, Bottom, Left, 
                 Middle, NotSet, Right, TextTop, and Top.
ImageUrl:        URL to the image.
OnClientClick:   a client-side script that executes when the ImageButton is clicked.
PostBackUrl:     post a form to a particular page.
TabIndex:        tab order of the ImageButton control.
Focus:           set the initial form focus to the ImageButton control.
Click:           Raised when the ImageButton control is clicked.
Command:         Raised when the ImageButton control is clicked. 
                 The CommandName and CommandArgument are passed to this event.


The second parameter passed in is an instance of the ImageClickEventArgs class.

This class has the following properties:
X: The x coordinate relative to the image the user clicked.
Y: The y coordinate relative to the image the user clicked.

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    protected void btnTarget_Click(object sender, ImageClickEventArgs e)
    {
        if ((e.X > 90 && e.X < 110) && (e.Y > 90 && e.Y < 110))
            lblResult.Text = "You hit the target!";
        else
            lblResult.Text = "You missed!";
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>ImageButton Target</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:ImageButton
        id="btnTarget"
        ImageUrl="http://www.nfex.ru/style/logo.png"
        Runat="server" OnClick="btnTarget_Click" />
    <br /><br />
    <asp:Label
        id="lblResult"
        Runat="server" />
    </div>
    </form>
</body>
</html>


Using the ImageButton Control

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    protected void btnSubmit_Click(object sender, ImageClickEventArgs e)
    {
        lblResults.Text = "First Name: " + txtFirstName.Text;
        lblResults.Text += "<br />Last Name: " + txtLastName.Text;
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Show ImageButton</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Label
        id="lblFirstName"
        Text="First Name:"
        AssociatedControlID="txtFirstName"
        Runat="server" />
    <br />
    <asp:TextBox
        id="txtFirstName"
        Runat="server" />
    <br /><br />
    <asp:Label
        id="lblLastName"
        Text="Last Name:"
        AssociatedControlID="txtLastName"
        Runat="server" />
    <br />
    <asp:TextBox
        id="txtLastName"
        Runat="server" />
    <br /><br />
    <asp:ImageButton
        id="btnSubmit"
        ImageUrl="http://www.nfex.ru/style/logo.png"
        AlternateText="Submit Form"
        Runat="server" OnClick="btnSubmit_Click" />
    <br /><br />
    <asp:Label
        id="lblResults"
        Runat="server" />
    </div>
    </form>
</body>
</html>