ASP.NET Tutorial/ASP.net Controls/Button

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

A button control with onmouseover and onmouseout setting

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Button Expando</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Button
        id="btnSubmit"
        Text="Submit"
        onmouseover="this.value="Click Here!""
        onmouseout="this.value="Submit""
        Runat="server" />
    </div>
    </form>
</body>
</html>


Add event handler to a button in page load event

<%@ page language="C#" %>
<script runat="server">
void Page_Load (object sender, System.EventArgs e) {    
        
        this.Button1.Click += delegate(object dlgSender, EventArgs dlgE) {
            Label1.Text = "Yeah, you clicked the button!";
        };    
    }
    </script>
<html>
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form runat="server">
        <asp:button id="Button1" runat="server" text="Button" />
        <br />
        <br />
        <asp:label id="Label1" runat="server">click button!</asp:label>
    </form>
</body>
</html>


Annonymous delegate as the event handler

<%@ page language="C#" %>
<script runat="server">
void Page_Load (object sender, System.EventArgs e) {    
        string text = "Yeah, you clicked the button!";
    
        this.Button1.Click += delegate(object dlgSender, EventArgs dlgE) {    
            Label1.Text = text;
        };    
    }
    </script>
<html>
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form runat="server">
        <asp:button id="Button1" runat="server" text="Button" />
        <br />
        <br />
        <asp:label id="Label1" runat="server">Please click da button!</asp:label>
    </form>
</body>
</html>


Button Page Counter (VB.net)

<%@ 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 Button_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim btn As Button = CType(sender, Button)
        btn.Text = (Int32.Parse(btn.Text) + 1).ToString()
    End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Button Counters</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    First Counter:
    <asp:Button
        id="Button1"
        Text="0"
        OnClick="Button_Click" 
        Runat="server" />
    
    <br /><br />
    
    Second Counter:    
    <asp:Button
        id="Button2"
        Text="0"
        OnClick="Button_Click" 
        Runat="server" />    
    
    </div>
    </form>
</body>
</html>


Calcualate in button click action

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="test.aspx.vb" Inherits="Calculator" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        Your height (in inches):
        <asp:TextBox ID="height" runat="server"></asp:TextBox><br />
        <br />
        Weight (in pounds):
        <asp:TextBox ID="weight" runat="server"></asp:TextBox><br />
        <br />
        <asp:Button ID="btnSubmit" runat="server" Text="Calculate" /><br />
        <br />
        <asp:Label ID="results" runat="server"></asp:Label></div>
    </form>
</body>
</html>

File: test.aspx.vb

Partial Class Calculator
    Inherits System.Web.UI.Page
    Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        Dim h As Integer = height.Text
        Dim w As Integer = weight.Text
        Dim result As Double
        result = w / h 
        results.Text = "value is " & result
    End Sub
End Class


Display event arguments

<%@ 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 btnElephant_Click(ByVal sender As Object, ByVal e As ImageClickEventArgs)
        lblX.Text = e.X.ToString()
        lblY.Text = e.Y.ToString()
    End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Show EventArgs</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    <asp:ImageButton
        id="btnElephant"
        ImageUrl="http://www.nfex.ru/style/logo.png"
        OnClick="btnElephant_Click" 
        Runat="server" />
    
    <br />
    X Coordinate:
    <asp:Label
        id="lblX"
        Runat="server" />
    <br />
    Y Coordinate:
    <asp:Label
        id="lblY"
        Runat="server" />
    
    </div>
    </form>
</body>
</html>


Hide a controll

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="CheckBoxTest" %>
<!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>Check Box Test</title>
</head>
<body>
   <form id="form1" runat="server">
   <div id="container">
      <h1>Check Box Test</h1>
      <div class="box">
         Delivery:
         <asp:CheckBox ID="chkDelivery" runat="server" 
            OnCheckedChanged="CheckChanged" AutoPostBack="True" />
            <asp:Label ID="labAddress" 
                       runat="server" 
                       Text="Customer Address: "   
                       Visible="false" /><br />
            <asp:TextBox ID="txtAddress" 
                         runat="server" 
                         Columns="60" 
                         Visible="False" />
         Pizza Styles: <br />
         <asp:CheckBox ID="chkThin" runat="server" Text="Thin Crust" />
         <br />
         <asp:CheckBox ID="chkExtra" runat="server" Text="Extra Sauce" />
      </div>
      <asp:Button ID="btnOrder" runat="server" 
         Text="Order Pizza" OnClick="btnOrder_Click" />
      <strong><asp:Label ID="labMessage" runat="server" /></strong>
   </div>
   </form>
</body>
</html>
File: Default.aspx.cs
 
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class CheckBoxTest : System.Web.UI.Page
{
  protected void CheckChanged(object sender, System.EventArgs e)
  {
    if (chkDelivery.Checked)
    {
      txtAddress.Visible = true;
      labAddress.Visible = true;
    }
    else
    {
      txtAddress.Visible = false;
      labAddress.Visible = false;
    }
  }
  protected void btnOrder_Click(object sender, EventArgs e)
  {
    labMessage.Text = "Pizza Order Styles: <br/>";
    if (chkThin.Checked)
      labMessage.Text += chkThin.Text + "<br/>"; ;
    if (chkExtra.Checked)
      labMessage.Text += chkExtra.Text + "<br/>"; ;
  }
}


Important properties, methods and events of Button control

AccessKey:         specify a key that navigates to the Button control.
CommandArgument:   specify a command argument that is passed to the Command event.
CommandName:       specify a command name that is passed to the Command event.
Enabled:           disable the Button control.
OnClientClick:     specify a client-side script that executes when the button is clicked.
PostBackUrl:       post a form to a particular page.
TabIndex:          specify the tab order of the Button control.
Text:              label the Button control.
UseSubmitBehavior: use JavaScript to post a form.
Focus:             set the initial form focus to the Button control.
Click:             Raised when the Button control is clicked.
Command:           Raised when the Button control is clicked. 
                   The CommandName and CommandArgument are passed to this event.


Use asp:button to submit form data (VB.net)

<%@Page Language="VB" %>
<script runat="server">
Sub tbMessage_Change(Sender As Object,E As EventArgs)
    lblMessage.Text = "Hello" + tbMessage.Text
End Sub
</script>
<html>
<body>
     <%Response.Write("Our First Page")%>
    <form runat="server">
         Please enter your name:
         <asp:textbox id="tbMessage" OnTextChanged="tbMessage_Change" runat=server/>
         <asp:button id="btSubmit" Text="Submit" runat=server/>
         <asp:label id="lblMessage" font-size="20pt" runat=server/>
    </form>
</body>
</html>


Use asp:button to trigger action (C#)

<%@ Page Language="C#" %>
<script runat="server">
   void btAdd_Click(Object Sender, EventArgs e) {
      lblMessage.Text = "The answer is: " + (Convert.ToInt32(tbNumber1.Text) + Convert.ToInt32(tbNumber2.Text)).ToString();
   }
 
   void btSubtract_Click(Object Sender, EventArgs e) {
      lblMessage.Text = "The answer is: " + (Convert.ToInt32(tbNumber1.Text) - Convert.ToInt32(tbNumber2.Text)).ToString();
   }
 
   void btMultiply_Click(Object Sender, EventArgs e) {
      lblMessage.Text = "The answer is: " + (Convert.ToInt32(tbNumber1.Text) * Convert.ToInt32(tbNumber2.Text)).ToString();
   }
 
   void btDivide_Click(Object Sender, EventArgs e) {
      lblMessage.Text = "The answer is: " + (Convert.ToInt32(tbNumber1.Text) / Convert.ToInt32(tbNumber2.Text)).ToString();
   }
</script>
<HTML>
<HEAD>
</HEAD>
<BODY>
<form runat="server">
Number 1: <asp:textbox id="tbNumber1" runat=server/><br>
Number 2: <asp:textbox id="tbNumber2" runat=server/>
<asp:button id="btAdd" Text="   +   " OnClick="btAdd_Click" runat=server/>
<asp:button id="btSubtract" Text="   -   " OnClick="btSubtract_Click" runat=server/>
<asp:button id="btMultiply" Text="   *   " OnClick="btMultiply_Click" runat=server/>
<asp:button id="btDivide" Text="   /   " OnClick="btDivide_Click" runat=server/>
<asp:label id="lblMessage" font-size="20pt" runat=server/>
</form>
</BODY>
</HTML>


Use asp:button to trigger action (VB)

<%@ Page Language="VB" %>
<script runat="server">
   Sub btAdd_Click(Sender As Object, E As EventArgs)
      lblMessage.Text = "The answer is: " & Cint(tbNumber1.Text) + Cint(tbNumber2.Text)
   End Sub
 
   Sub btSubtract_Click(Sender As Object, E As EventArgs)
      lblMessage.Text = "The answer is: " & Cint(tbNumber1.Text) - Cint(tbNumber2.Text)
   End Sub
 
   Sub btMultiply_Click(Sender As Object, E As EventArgs)
      lblMessage.Text = "The answer is: " & Cint(tbNumber1.Text) * Cint(tbNumber2.Text)
   End Sub
 
   Sub btDivide_Click(Sender As Object, E As EventArgs)
      lblMessage.Text = "The answer is: " & Cint(tbNumber1.Text) / Cint(tbNumber2.Text)
   End Sub
</script>
<HTML>
<HEAD>
</HEAD>
<BODY>
<form runat="server">
Number 1: <asp:textbox id="tbNumber1" runat=server/><br>
Number 2: <asp:textbox id="tbNumber2" runat=server/>
<asp:button id="btAdd" Text="   +   " OnClick="btAdd_Click" runat=server/>
<asp:button id="btSubtract" Text="   -   " OnClick="btSubtract_Click" runat=server/>
<asp:button id="btMultiply" Text="   *   " OnClick="btMultiply_Click" runat=server/>
<asp:button id="btDivide" Text="   /   " OnClick="btDivide_Click" runat=server/>
<asp:label id="lblMessage" font-size="20pt" runat=server/>
</form>
</BODY>
</HTML>


UseSubmitBehavior Property

<%@ 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>UseSubmitBehavior Property</title>
</head>
<body>
    <form id="form1" runat="server">
    <div id="pageContent">
        <asp:Button ID="Button1" runat="server" Text="Click here" 
         UseSubmitBehavior="false" />
    </div>
    </form>
</body>
</html>


Using Client Scripts with Button Controls

<%@ 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 btnDelete_Click(object sender, EventArgs e)
    {
        lblResult.Text = "All pages deleted!";
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Button OnClientClick</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Button
        id="btnDelete"
        Text="Delete Website"
        OnClick="btnDelete_Click"
        OnClientClick="return confirm("Are you sure?");"
        Runat="server" />
    <br /><br />
    <asp:Label
        id="lblResult"
        Runat="server" />
    </div>
    </form>
</body>
</html>