ASP.Net/Asp Control/Image Button — различия между версиями

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

Текущая версия на 11:52, 26 мая 2010

asp:ImageButton AlternateText (VB.net)

<%@ Page Language=VB Debug=true %>
<script runat=server>
Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs)
    lblMessage.Text = "Press a button!"
End Sub
Sub SubmitBtn1_Click(Sender As Object, E As ImageClickEventArgs)
    lblMessage.Text = "You clicked at location (" _
        & E.X.ToString() & ", " & E.Y.ToString() _
        & ")"
End Sub
Sub SubmitBtn2_Click(Sender As Object, E As CommandEventArgs)
    lblMessage.Text = "The button you clicked: " _
        & e.rumandName _
        & ", has the following arguement: " _
        & e.rumandArgument & "."
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>Image Button Controls Sample Page</TITLE>
</HEAD>
<BODY>
<form runat="server">
<Font Face="Tahoma" Size="+1">
<asp:Label 
    id="lblMessage" 
    Text="Press a button!"
    runat="server"
/>
<BR><BR>
<asp:ImageButton 
    id="butAlone"
    AlternateText="Alone Button"
    ImageURL="./clickme.gif"
    ImageAlign="Middle"
    OnClick="SubmitBtn1_Click" 
    runat="server"
/>
<BR><BR>
<asp:ImageButton 
    id="butAdd"
    AlternateText="Add Record"
    ImageURL="./add.gif"
    CommandName="Add"
    CommandArgument="None"
    OnCommand="SubmitBtn2_Click" 
    runat="server"
/>
<asp:ImageButton 
    id="butEdit"
    AlternateText="Edit Record"
    ImageURL="./edit.gif"
    CommandName="Edit"
    CommandArgument="None"
    OnCommand="SubmitBtn2_Click" 
    runat="server"
/>
<asp:ImageButton 
    id="butSortA"
    AlternateText="Sort A-Z"
    ImageURL="./sorta.gif"
    CommandName="Sort"
    CommandArgument="A"
    OnCommand="SubmitBtn2_Click" 
    runat="server"
/>
<asp:ImageButton 
    id="butSortD"
    AlternateText="Sort Z-A"
    ImageURL="./sortd.gif"
    CommandName="Sort"
    CommandArgument="D"
    OnCommand="SubmitBtn2_Click" 
    runat="server"
/>
</Font>
</Form>
</BODY>
</HTML>



Deal with button action event arguments: x, y coordinates (C#)

<%@ Page Language="C#" %>
<script runat="server">
    protected void btnElephant_Click(object sender, ImageClickEventArgs e)
    {
        lblX.Text = e.X.ToString();
        lblY.Text = e.Y.ToString(); 
    }
</script>
<html>
<head>
    <title>Show EventArgs</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    <asp:ImageButton
        id="btnElephant"
        ImageUrl="http://www.nfex.ru/style/logo.png"
        Runat="server" OnClick="btnElephant_Click" />
    
    <br />
    X Coordinate:
    <asp:Label
        id="lblX"
        Runat="server" />
    <br />
    Y Coordinate:
    <asp:Label
        id="lblY"
        Runat="server" />
    
    </div>
    </form>
</body>
</html>



Determining the Coordinates Clicked on the Image of an ImageButton Control (VB.net)

<%@ Page Language=VB Debug=true %>
<script runat=server>
Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs)
End Sub
Sub SubmitBtn_Click(Sender As Object, E As ImageClickEventArgs)
    Dim String1 as String
    Dim String2 as String
    If E.X < 18 then
        String1 = "Left"
    Else
        String1 = "Right"
    End If
    If E.Y < 18 then
        String2 = "Top"
    Else
        String2 = "Bottom"
    End If
    lblLocationClicked.Text = "You clicked in the " & String2 _
        & "-" & String1 & " portion of the image."
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>Determining the Coordinates Clicked on the Image of an ImageButton Control</TITLE>
</HEAD>
<form runat="server">
<asp:label 
    id="lblLocationClicked" 
    runat="server"
/>
<BR><BR>
<asp:imagebutton 
    id="imagebutOK"
    alternatetext="View Coordinates"
    imageurl="http://www.nfex.ru/style/logo.png"
    onclick="SubmitBtn_Click" 
    runat="server"
/>
</form>
</BODY>
</HTML>



For loop inside asp:ImageButton Click event (VB.net)

<%@ Page Language="VB" %>
<script runat="server">
    
    Sub ImageButton1_Click(sender As Object, e As ImageClickEventArgs)
      Dim Counter as Integer
      For Counter = 1 To 100  " Start looping 100 times
        Label1.Text &=  Counter.ToString() & ": I  Love You! " & "<BR />"
      Next
    End Sub
</script>
<html>
<head>
</head>
<body>
    <form runat="server">
        <p>
            <asp:ImageButton id="ImageButton1" onclick="ImageButton1_Click" runat="server" ImageUrl="http://www.nfex.ru/style/logo.png"></asp:ImageButton>
        </p>
        <p>
            <asp:Label id="Label1" runat="server"></asp:Label>
        </p>
    </form>
</body>
</html>



Get Image Button mouse cursor coordinates (C#)

<%@ Page Language="C#" %>
<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>
<head>
    <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>



Get position from ImageClickEventArgs

<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    Sub btnTarget_Click(ByVal sender As Object, ByVal e As ImageClickEventArgs)
        If (e.X > 90 And e.X < 110) And (e.Y > 90 And e.Y < 110) Then
            lblResult.Text = "You hit the target!"
        Else
            lblResult.Text = "You missed!"
        End If
    End Sub
</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">
    <asp:ImageButton
        id="btnTarget"
        ImageUrl="http://www.nfex.ru/style/logo.png"
        Runat="server" 
        OnClick="btnTarget_Click"
        Width="200"
        Height="300" />
    
    <asp:Label
        id="lblResult"
        Runat="server" />
    </form>
</body>
</html>



Link Javascript function to OnClientClick event

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Popup Date Selector</title>
    <script language="javascript" type="text/javascript">
    function PickDate(controlName)
    {
        var wnd = null;
        var settings = "width=300,height=200,location=no,menubar=no,toolbar=no,scrollbars=no,resizable=yes,status=yes";
        var url = "Popup.aspx?control=" + controlName;
        wnd = window.open(url,"DatePopup",settings);
        if (wnd.focus){ wnd.focus(); }
    }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    Start Date:
    <asp:TextBox ID="txtDateStart" runat="server" Columns="10"></asp:TextBox>
    <asp:ImageButton ID="btnDateStart" runat="server" 
       ImageUrl="~/images/cal_allday.gif" 
       OnClientClick="PickDate("txtDateStart")" />
    <br />
    End Date:
    <asp:TextBox ID="txtDateEnd" runat="server" Columns="10"></asp:TextBox>
    <asp:ImageButton ID="btnDateEnd" runat="server" 
       ImageUrl="~/images/cal_allday.gif" 
       OnClientClick="PickDate("txtDateEnd")" />
    </div>
    Press enter key to continue.
    </form>
</body>
</html>



Set ImageUrl for asp:Image (VB.net)

<%@ Page Language="VB" %>
<script runat="server">
    Sub Button1_Click(sender As Object, e As EventArgs)
      Label3.Visible = False
      Label1.Text = Int(Rnd() * 6) + 1
      Label2.Text = Int(Rnd() * 6) + 1
    
      Image2.ImageUrl = "http://www.nfex.ru/style/logo.png"
      Image3.ImageUrl = "http://www.nfex.ru/style/logo.png"
    
      If Label1.Text = "1" And Label2.Text = "1" Then
        Label5.Text = Label4.Text
        If CInt(Label5.Text) > CInt(Label6.Text) Then
          Label6.Text = label5.Text
          Panel1.Visible = True
        End If
        Label4.Text = 0
        Label3.Visible = True
      Else
        Label4.Text = CInt(Label4.Text) + CInt(Label1.Text) + CInt(Label2.Text)
      End If
    End Sub
    
    Sub Button2_Click(sender As Object, e As EventArgs)
      Label6.Text = 0
    End Sub
    
    Sub Button3_Click(sender As Object, e As EventArgs)
      Panel1.Visible = False
    End Sub
</script>
<html>
<head>
</head>
<body>
    <form runat="server">
        <p>
            <asp:Label id="Label1" runat="server" Height="44px" Font-Size="X-Large" Font-Names="Verdana" BorderStyle="Solid">0</asp:Label>
            <asp:Label id="Label2" runat="server" Height="44px" Font-Size="X-Large" Font-Names="Verdana" BorderStyle="Solid">0</asp:Label>
        </p>
        <p>
            <asp:Image id="Image2" runat="server"></asp:Image>
            <asp:Image id="Image3" runat="server"></asp:Image>
        </p>
        <p>
            <asp:Label id="Label3" runat="server" Visible="False">Ooops, you did it again :(</asp:Label>
        </p>
        <p>
            <asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="Hit me baby one more time..."></asp:Button>
        </p>
        <p>
            <asp:Label id="Label4" runat="server" Font-Size="X-Large" Font-Names="Verdana" ForeColor="Red">0</asp:Label>
        </p>
        <p>
            Last Score: 
            <asp:Label id="Label5" runat="server" Font-Size="X-Large" Font-Names="Verdana" ForeColor="Red">0</asp:Label>
        </p>
        <p>
            High Score: 
            <asp:Label id="Label6" runat="server" Font-Size="X-Large" Font-Names="Verdana" ForeColor="Red">0</asp:Label>
        </p>
        <p>
            <asp:Button id="Button2" onclick="Button2_Click" runat="server" Text="Reset High Scores"></asp:Button>
        </p>
        <p>
            <asp:Panel id="Panel1" style="Z-INDEX: 100; LEFT: 192px; POSITION: absolute; TOP: 421px" runat="server" Visible="False">
                <p>
                    Well done, you can now submit your high score.
                </p>
                <p>
                    Name: 
                    <asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
                </p>
                <p>
                    E-mail: 
                    <asp:TextBox id="TextBox2" runat="server"></asp:TextBox>
                </p>
                <p>
                    &nbsp;<asp:Button id="Button3" onclick="Button3_Click" runat="server" Text="Submit High Score"></asp:Button>
                </p>
            </asp:Panel>
        </p>
    </form>
</body>
</html>



Simple ImageButton control (VB.net)

<%@ Page Language=VB Debug=true %>
<script runat=server>
Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs)
End Sub
Sub SubmitBtn_Click(Sender As Object, E As ImageClickEventArgs)
  lblDataEntered.Text = "You entered:<BR>" _
      & txtName.Text & "<BR>" _
      & txtEmail.Text & "<BR>" _
      & txtPhone.Text
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>Creating a Basic ImageButton Control</TITLE>
</HEAD>
<form runat="server">
<asp:label 
    id="lblDataEntered" 
    runat="server"
/>
<BR><BR>
Enter your Name:<BR>
<asp:textbox 
    id="txtName" 
    columns="25"
    maxlength="30"
    runat=server 
/>
<BR><BR>
Enter your Email Address:<BR>
<asp:textbox 
    id="txtEmail" 
    columns="35"
    maxlength="50"
    runat=server 
/>
<BR><BR>
Enter your Phone Number:<BR>
<asp:textbox 
    id="txtPhone" 
    columns="15"
    maxlength="15"
    runat=server 
/>
<BR><BR>
<asp:imagebutton 
    id="imagebutOK"
    alternatetext="Click to Submit"
    imageurl="http://www.nfex.ru/style/logo.png"
    onclick="SubmitBtn_Click" 
    runat="server"
/>
</form>
</BODY>
</HTML>



Use Image Button event (VB.net)

<%@ Page Language="VB" %>
<script runat="server">
    Sub ImageButton1_Click(sender As Object, e As ImageClickEventArgs)
      Dim Message As String
    
      Message = "Hello " & txtName.Text & "."
    
      If chkTour.Checked Then
        Message &= cboCountry.SelectedItem.Text
      End If
    
      If radMail.SelectedItem.Value = "HTML" Then
        Label1.Text = "Using HTML format for mail. Sending:<p />" & Message
      Else
        Label1.Text = "Using plain format for mail. Sending:<p />" & Message
      End If
    End Sub
</script>
<html>
<head>
</head>
<body>
    <form runat="server">
        <p>
            Subscribe to our mailing list: 
        </p>
        <p>
            <table style="WIDTH: 300px; HEIGHT: 150px">
                <tbody>
                    <tr>
                        <td>
                            Name:</td>
                        <td>
                            <asp:TextBox id="txtName" runat="server"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            E-mail Address:</td>
                        <td>
                            <asp:TextBox id="txtEmail" runat="server"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            Country:</td>
                        <td>
                            <asp:DropDownList id="cboCountry" runat="server">
                                <asp:ListItem Value="US" Selected="True">United States</asp:ListItem>
                                <asp:ListItem Value="UK">United Kingdom</asp:ListItem>
                            </asp:DropDownList>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            Mail me tour details:</td>
                        <td>
                            <asp:CheckBox id="chkTour" runat="server"></asp:CheckBox>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            Mail format:</td>
                        <td>
                            <asp:RadioButtonList id="radMail" runat="server">
                                <asp:ListItem Value="HTML">HTML</asp:ListItem>
                                <asp:ListItem Value="Text">Text</asp:ListItem>
                            </asp:RadioButtonList>
                        </td>
                    </tr>
                    <tr>
                        <td>
                        </td>
                        <td>
                            <asp:ImageButton id="ImageButton1" onclick="ImageButton1_Click" runat="server" ImageUrl="http://www.nfex.ru/style/logo.png"></asp:ImageButton>
                        </td>
                    </tr>
                </tbody>
            </table>
        </p>
    </form>
    <p>
        <asp:Label id="Label1" runat="server"></asp:Label>
    </p>
</body>
</html>