ASP.Net/Asp Control/Button

Материал из .Net Framework эксперт
Версия от 11:52, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Add action listener to a server Button (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 EventArgs)
    lblMessage.Text = "You pressed the alone button."
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>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:Button 
    id="butAlone"
    text="Alone Button"
    OnClick="SubmitBtn1_Click" 
    runat="server"
/>
<BR><BR>
</Font>
</Form>
</BODY>
</HTML>



Add action listener to asp Button by delegate (C#)

<%@ page language="C#" %>
<script runat="server">
void Page_Load (object sender, System.EventArgs e) {    
        
        this.Button1.Click += delegate(object dlgSender, EventArgs dlgE) {
            Label1.Text = "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">Please click da button!</asp:label>
    </form>
</body>
</html>



asp:button: command name and arguments (VB.net)

<%@ Page Language=VB Debug=true %>
<script runat=server>
Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs)
End Sub
Sub CommandButton_Click(Sender As Object, E As CommandEventArgs)
  lblButtonPressed.Text = "You pressed: " _
      & E.rumandName & "<BR>With this argument: " _
      & E.rumandArgument & "<BR>"
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>Using a Button Control as a Command Button</TITLE>
</HEAD>
<form runat="server">
<asp:label 
    id="lblButtonPressed" 
    runat="server"
/>
<BR><BR>
<asp:button 
    id="butSortName"
    text="Sort By Name"
    oncommand="CommandButton_Click"
    commandname="Sort"
    commandargument="By Name"
    runat="server"
/>
<BR><BR>
<asp:button 
    id="butSortPrice"
    text="Sort By Price"
    oncommand="CommandButton_Click"
    commandname="Sort"
    commandargument="By Price"
    runat="server"
/>
<BR><BR>
<asp:button 
    id="lnkbutNewSearch"
    text="New Search"
    oncommand="CommandButton_Click"
    commandname="New Search"
    commandargument="None"
    runat="server"
/>
</form>
</BODY>
</HTML>



asp:button event to change the label text (VB.net)

<%@ Page Language="VB" %>
<script runat="server">
    Sub Button1_Click(sender As Object, e As EventArgs)
    
      Label2.Text = "The button has been clicked"
    
    End Sub
    
    Sub Page_Load(sender As Object, e As EventArgs)
    
      If Page.IsPostBack Then
        Label1.Text = "The page has been loaded because of a button click"
      Else
        Label1.Text = "The page has been loaded for the first time"
      End If
      Label2.Text = ""
    
    End Sub
</script>
<html>
<head>
</head>
<body>
    <form runat="server">
        <p>
            <asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="Button"></asp:Button>
        </p>
        <p>
            <asp:Label id="Label1" runat="server">Label</asp:Label>
        </p>
        <p>
            <asp:Label id="Label2" runat="server">Label</asp:Label>
        </p>
    </form>
</body>
</html>



asp button on client click (C#)

<%@ Page Language="C#" %>
<script runat="server">
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        lblResult.Text = "All pages deleted!";
    }
</script>
<html>
<head>
    <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" />
    
    <asp:Label
        id="lblResult"
        Runat="server" />    
    
    </div>
    </form>
</body>
</html>



asp button postbackurl (C#)

<%@ Page Language="C#" %>
<html>
<head id="Head1" runat="server">
    <title>Button Search</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    <asp:Label
        id="lblSearch"
        Text="Search:"
        Runat="server" />
    <asp:TextBox
        id="txtSearch"
        Runat="server" />
    <asp:Button
        id="btnSearch"
        Text="Go!"
        PostBackUrl="ButtonSearchResults.aspx"
        Runat="server" />
    </div>
    </form>
</body>
</html>

<%--
<%@ Page Language="C#" %>
<script runat="server">
    void Page_Load()
    {
        if (PreviousPage != null)
        {
            TextBox txtSearch = (TextBox)PreviousPage.FindControl("txtSearch");
            lblSearch.Text = String.Format("Search For: {0}", txtSearch.Text);
        }
    }
    
</script>
<html>
<head>
    <title>Button Search Results</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    <asp:Label
        id="lblSearch"
        Runat="server" />
    
    </div>
    </form>
</body>
</html>
--%>



Button action Command and Parameter (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 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>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"
/>
<asp:Button 
    id="butAdd"
    text="Add Record"
    CommandName="Add"
    CommandArgument="None"
    OnCommand="SubmitBtn2_Click" 
    runat="server"
/>
<asp:Button 
    id="butEdit"
    text="Edit Record"
    CommandName="Edit"
    CommandArgument="None"
    OnCommand="SubmitBtn2_Click" 
    runat="server"
/>
<asp:Button 
    id="butSortA"
    text="Sort A-Z"
    CommandName="Sort"
    CommandArgument="A"
    OnCommand="SubmitBtn2_Click" 
    runat="server"
/>
<asp:Button 
    id="butSortD"
    text="Sort Z-A"
    CommandName="Sort"
    CommandArgument="D"
    OnCommand="SubmitBtn2_Click" 
    runat="server"
/>
</Font>
</Form>
</BODY>
</HTML>



Button counter: click and change button label (C#)

<%@ Page Language="C#" %>
<script runat="server">
    protected void Button_Click(object sender, EventArgs e)
    {
        Button btn = (Button)sender;
        btn.Text = (Int32.Parse(btn.Text) + 1).ToString();
    }
</script>
<html>
<head>
    <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" />
    
    Second Counter:    
    <asp:Button
        id="Button2"
        Text="0"
        OnClick="Button_Click" 
        Runat="server" />    
    
    </div>
    </form>
</body>
</html>



Button event Handler (C#)

<%@ Page Language="C#" Debug="true"%>
<script runat="server">
    void ClickHandler(object sender, EventArgs e)
    {
        if (ExtraInfoBox.Checked)
        {
            Message.Text = "You will hear from us shortly";
        }
        else
        {
            Message.Text = "You will not receive any further information from us";
        }
    }
</script>
<html>
<head>
</head>
<body>
    <form runat="server">
        <asp:CheckBox id="ExtraInfoBox" runat="server" Text="Click here to receive extra information"></asp:CheckBox>
        <br />
        <br />
        <asp:Button id="Button1" onclick="ClickHandler" runat="server" Text="Click here to submit"></asp:Button>
        <br />
        <br />
        <asp:Label id="Message" runat="server"></asp:Label>
    </form>
</body>
</html>



CausesValidation="False"

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default" %>
<!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>Default</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label2" 
                   runat="server" 
                   Text="Must Be a Date and Earlier than Today"></asp:Label><br />
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <asp:CompareValidator ID="CompareValidator1" 
                              runat="server" 
                              ControlToValidate="TextBox2"
                              ErrorMessage="TextBox2 Must Be a Date and Earlier than Today" 
                              Operator="LessThan"
                              SetFocusOnError="True" 
                              Type="Date">*</asp:CompareValidator><br />
        <asp:Button ID="Button1" 
                    runat="server" 
                    OnClick="Button1_Click" 
                    Text="Submit" />
        <asp:Button ID="Button2" 
                    runat="server" 
                    CausesValidation="False" 
                    Text="Cancel - Will Not Validate!" />
        <asp:Button ID="Button3" 
                    runat="server" 
                    Text="Validate DropDownList1 Only" 
                    ValidationGroup="DDL" /><br />
        <asp:ValidationSummary ID="ValidationSummary1" runat="server" ShowMessageBox="True" />
        <asp:ValidationSummary ID="ValidationSummary2" 
                               runat="server" 
                               ShowMessageBox="True"
                               ValidationGroup="DDL" />
    </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 Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    this.rupareValidator1.ValueToCompare = DateTime.Today.ToShortDateString();
    }
  protected void Button1_Click(object sender, EventArgs e)
  {
    this.Validate(string.Empty);
    if (this.IsValid==false)
    {
      // Do something...
    }
  }
}



Event driven page (VB.net)

<script runat="server" language="vb">
  Sub Add(sender as object, e As EventArgs)
    lblAnswer.Text = CDbl(tbxInput1.text) + CDbl(tbxInput2.text)
  End Sub
  Sub Subtract(sender as object, e As EventArgs)
    lblAnswer.Text = CDbl(tbxInput1.text) - CDbl(tbxInput2.text)
  End Sub
  Sub Factor(sender as object, e As EventArgs)
    lblAnswer.Text = CDbl(tbxInput1.text) * CDbl(tbxInput2.text)
  End Sub
  Sub Ratio(sender as object, e As EventArgs)
    lblAnswer.Text = CDbl(tbxInput1.text) / CDbl(tbxInput2.text)
  End Sub
</script>
<html>
  <form runat="server">
    <asp:textbox id="tbxInput1" runat="server" />
    <asp:button id="btnAdd" runat="server" text=" + " Onclick="Add" />
    <asp:button id="btnSubtract" runat="server" text=" ?" 
                                                         Onclick="Subtract" />
    <br/>
    <asp:textbox id="tbxInput2" runat="server" />
    <asp:button id="btnFactor" runat="server" text=" x " Onclick="Factor" />
    <asp:button id="btnRatio" runat="server" text=" ?" Onclick="Ratio" />
    <br/>
    <b>Answer = <asp:Label id="lblAnswer" runat="server" /></b>
  </form>
</html>



Event in code behind file (VB.net)

<%@ Page 
    language=VB 
    debug=true 
    src="EventsInCodeBehind.vb" 
    Inherits="clsEventsInCodeBehind" 
%>
<HTML>
<HEAD>
<TITLE>Inheriting Events in a Code-Behind Files</TITLE>
</HEAD>
<form runat="server">
<BR><BR>
<asp:label
    id="lblMessage1"
    runat="Server"
/>
<BR><BR>
<asp:label
    id="lblMessage2"
    runat="Server"
/>
<BR><BR>
Enter text to uppercase:
<BR>
<asp:textbox
    id="txtUCase"
    runat="Server"
/>
<BR><BR>
Add Numbers:
<BR>
<asp:textbox
    id="txtNumber1"
    runat="Server"
/>
<BR>
<asp:textbox
    id="txtNumber2"
    runat="Server"
/>
<asp:button 
    id="butAdd"
    text="Add"
    runat="server"
/>
</form>
</BODY>
</HTML>
<%--
Public Class clsEventsInCodeBehind
    Inherits System.Web.UI.Page
    Protected WithEvents txtUCase As _
        System.Web.UI.WebControls.TextBox
    Protected WithEvents txtNumber1 As _
        System.Web.UI.WebControls.TextBox
    Protected WithEvents txtNumber2 As _
        System.Web.UI.WebControls.TextBox
    Protected WithEvents lblMessage1 As _
        System.Web.UI.WebControls.Label
    Protected WithEvents lblMessage2 As _
        System.Web.UI.WebControls.Label
    Protected WithEvents butAdd As _
        System.Web.UI.WebControls.Button
    Private Sub Page_Init(ByVal sender As System.Object, _
        ByVal e As System.EventArgs)
        lblMessage1.Text = Date.Now
        txtUcase.AutoPostBack = True
    End Sub
    Private Sub Page_Load(ByVal sender As System.Object, _
        ByVal e As System.EventArgs)
        lblMessage2.Text = Date.Now
    End Sub
    Sub txtUCase_Change(Sender As System.Object, _
        E As System.EventArgs) Handles txtUCase.TextChanged
        Dim TempString as String
        TempString = txtUCase.Text
        txtUCase.Text = TempString.ToUpper()
    End Sub
    Sub butAdd_Click(Sender As System.Object, _
        E As System.EventArgs) Handles butAdd.Click
        butAdd.Text = CSng(txtNumber1.Text) + CSng(txtNumber2.Text)
    End Sub
End Class
--%>



OnClientClick event

<%@ 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 btnDelete_Click(sender AS Object, e As EventArgs)
        lblResult.Text = "All pages deleted!"
    End Sub
</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>



onmouseover and onmouseout events

<%@ Page Language="VB" %>
<!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>



Read input and do the calculation in Button action listener (C#)

<%@ Page Language="C#" Debug="True" %>
<script runat="server">
    void Page_Load()
    {
        btnAdd.BackColor = System.Drawing.Color.LightGray;
        btnSubtract.BackColor = System.Drawing.Color.LightGray;
        btnMultiply.BackColor = System.Drawing.Color.LightGray;
        btnDivide.BackColor = System.Drawing.Color.LightGray;
    }
    
    void Add(object sender, EventArgs e)
    {
        double answer = Convert.ToDouble(txtInput1.Text) +
                        Convert.ToDouble(txtInput2.Text);
        lblAnswer.Text = answer.ToString();
        btnAdd.BackColor = System.Drawing.Color.Yellow;
    }
    
    void Subtract(object sender, EventArgs e)
    {
        double answer = Convert.ToDouble(txtInput1.Text) -
                        Convert.ToDouble(txtInput2.Text);
        lblAnswer.Text = answer.ToString();
        btnSubtract.BackColor = System.Drawing.Color.Yellow;
    }
    
    void Multiply(object sender, EventArgs e)
    {
        double answer = Convert.ToDouble(txtInput1.Text) *
                        Convert.ToDouble(txtInput2.Text);
        lblAnswer.Text = answer.ToString();
        btnMultiply.BackColor = System.Drawing.Color.Yellow;
    }
    
    void Divide(object sender, EventArgs e)
    {
        double answer = Convert.ToDouble(txtInput1.Text) /
                        Convert.ToDouble(txtInput2.Text);
        lblAnswer.Text = answer.ToString();
        btnDivide.BackColor = System.Drawing.Color.Yellow;
    }
</script>
<html>
<head>
    <title>Calculator example v1</title>
</head>
<body>
    <form runat="server">
        <h2>Calculator Version 1 
        </h2>
        <asp:TextBox id="txtInput1" runat="server"></asp:TextBox>
        <asp:Button id="btnAdd" onclick="Add" runat="server" Text=" + "></asp:Button>
        <asp:Button id="btnSubtract" onclick="Subtract" runat="server" Text=" - "></asp:Button>
        <br />
        <asp:TextBox id="txtInput2" runat="server"></asp:TextBox>
        <asp:Button id="btnMultiply" onclick="Multiply" runat="server" Text=" x "></asp:Button>
        <asp:Button id="btnDivide" onclick="Divide" runat="server" Text="/"></asp:Button>
        <br />
        <strong>Answer = <asp:Label id="lblAnswer" runat="server"></asp:Label></strong>
    </form>
</body>
</html>



Set Button text (VB.net)

<%@ Page Language="VB" %>
<script language="vb" runat="server">
    Sub Page_Load(Source As Object, E as EventArgs)
      If Not IsPostBack Then
        MyButton.Text = "OK"
        MyDropDownList.Items.Add("http://www.nfex.ru")
        MyDropDownList.Items.Add("http://www.google.ru")
      End If
    End Sub
  </script>
<html>
 <body>
  <form id="WebForm1" method="post" runat="server">
    <asp:DropDownList id=MyDropDownList runat="server"/>
    <asp:button id=MyButton runat="server" Text=""/>
  </form>
 </body>
</html>