ASP.Net/Asp Control/Image Button

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

asp:ImageButton AlternateText (VB.net)

   <source lang="csharp">

<%@ 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"> <asp:Label

   id="lblMessage" 
   Text="Press a button!"
   runat="server"

/>

<asp:ImageButton

   id="butAlone"
   AlternateText="Alone Button"
   ImageURL="./clickme.gif"
   ImageAlign="Middle"
   OnClick="SubmitBtn1_Click" 
   runat="server"

/>

<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"

/> </Form> </BODY> </HTML>

      </source>
   
  


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

   <source lang="csharp">

<%@ 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">
   <asp:ImageButton
       id="btnElephant"
       ImageUrl="http://www.nfex.ru/style/logo.png"
       Runat="server" OnClick="btnElephant_Click" />
   
   
X Coordinate: <asp:Label id="lblX" Runat="server" />
Y Coordinate: <asp:Label id="lblY" Runat="server" />
   </form>

</body> </html>

      </source>
   
  


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

   <source lang="csharp">

<%@ 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"

/>

<asp:imagebutton

   id="imagebutOK"
   alternatetext="View Coordinates"
   imageurl="http://www.nfex.ru/style/logo.png"
   onclick="SubmitBtn_Click" 
   runat="server"

/> </form> </BODY> </HTML>

      </source>
   
  


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

   <source lang="csharp">

<%@ 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! " & "
" Next End Sub

</script> <html> <head> </head> <body>

   <form runat="server">

<asp:ImageButton id="ImageButton1" onclick="ImageButton1_Click" runat="server" ImageUrl="http://www.nfex.ru/style/logo.png"></asp:ImageButton>

<asp:Label id="Label1" runat="server"></asp:Label>

   </form>

</body> </html>

      </source>
   
  


Get Image Button mouse cursor coordinates (C#)

   <source lang="csharp">

<%@ 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">
   <asp:ImageButton
       id="btnTarget"
       ImageUrl="http://www.nfex.ru/style/logo.png"
       Runat="server" OnClick="btnTarget_Click" />
   
   

<asp:Label id="lblResult" Runat="server" />
   </form>

</body> </html>

      </source>
   
  


Get position from ImageClickEventArgs

   <source lang="csharp">

<%@ 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>

</source>
   
  


Link Javascript function to OnClientClick event

   <source lang="csharp">

<%@ 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">
   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")" />
   
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")" />
   Press enter key to continue.
   </form>

</body> </html>

</source>
   
  


Set ImageUrl for asp:Image (VB.net)

   <source lang="csharp">

<%@ 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">

<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>

<asp:Image id="Image2" runat="server"></asp:Image> <asp:Image id="Image3" runat="server"></asp:Image>

<asp:Label id="Label3" runat="server" Visible="False">Ooops, you did it again :(</asp:Label>

<asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="Hit me baby one more time..."></asp:Button>

<asp:Label id="Label4" runat="server" Font-Size="X-Large" Font-Names="Verdana" ForeColor="Red">0</asp:Label>

Last Score: <asp:Label id="Label5" runat="server" Font-Size="X-Large" Font-Names="Verdana" ForeColor="Red">0</asp:Label>

High Score: <asp:Label id="Label6" runat="server" Font-Size="X-Large" Font-Names="Verdana" ForeColor="Red">0</asp:Label>

<asp:Button id="Button2" onclick="Button2_Click" runat="server" Text="Reset High Scores"></asp:Button>

<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.

Name: <asp:TextBox id="TextBox1" runat="server"></asp:TextBox>

E-mail: <asp:TextBox id="TextBox2" runat="server"></asp:TextBox>

 <asp:Button id="Button3" onclick="Button3_Click" runat="server" Text="Submit High Score"></asp:Button>

           </asp:Panel>
       </p>
   </form>

</body> </html>

      </source>
   
  


Simple ImageButton control (VB.net)

   <source lang="csharp">

<%@ 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:
" _ & txtName.Text & "
" _ & txtEmail.Text & "
" _ & txtPhone.Text

End Sub </SCRIPT> <HTML> <HEAD> <TITLE>Creating a Basic ImageButton Control</TITLE> </HEAD> <form runat="server"> <asp:label

   id="lblDataEntered" 
   runat="server"

/>

Enter your Name:
<asp:textbox

   id="txtName" 
   columns="25"
   maxlength="30"
   runat=server 

/>

Enter your Email Address:
<asp:textbox

   id="txtEmail" 
   columns="35"
   maxlength="50"
   runat=server 

/>

Enter your Phone Number:
<asp:textbox

   id="txtPhone" 
   columns="15"
   maxlength="15"
   runat=server 

/>

<asp:imagebutton

   id="imagebutOK"
   alternatetext="Click to Submit"
   imageurl="http://www.nfex.ru/style/logo.png"
   onclick="SubmitBtn_Click" 
   runat="server"

/> </form> </BODY> </HTML>

      </source>
   
  


Use Image Button event (VB.net)

   <source lang="csharp">

<%@ 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">

Subscribe to our mailing list:

<tbody> </tbody>
Name:
                           <asp:TextBox id="txtName" runat="server"></asp:TextBox>
E-mail Address:
                           <asp:TextBox id="txtEmail" runat="server"></asp:TextBox>
Country:
                           <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>
Mail me tour details:
                           <asp:CheckBox id="chkTour" runat="server"></asp:CheckBox>
Mail format:
                           <asp:RadioButtonList id="radMail" runat="server">
                               <asp:ListItem Value="HTML">HTML</asp:ListItem>
                               <asp:ListItem Value="Text">Text</asp:ListItem>
                           </asp:RadioButtonList>
                           <asp:ImageButton id="ImageButton1" onclick="ImageButton1_Click" runat="server" ImageUrl="http://www.nfex.ru/style/logo.png"></asp:ImageButton>

   </form>

<asp:Label id="Label1" runat="server"></asp:Label>

</body> </html>

      </source>