ASP.Net/Asp Control/Button
Содержание
- 1 Add action listener to a server Button (VB.net)
- 2 Add action listener to asp Button by delegate (C#)
- 3 asp:button: command name and arguments (VB.net)
- 4 asp:button event to change the label text (VB.net)
- 5 asp button on client click (C#)
- 6 asp button postbackurl (C#)
- 7 Button action Command and Parameter (VB.net)
- 8 Button counter: click and change button label (C#)
- 9 Button event Handler (C#)
- 10 CausesValidation="False"
- 11 Event driven page (VB.net)
- 12 Event in code behind file (VB.net)
- 13 OnClientClick event
- 14 onmouseover and onmouseout events
- 15 Read input and do the calculation in Button action listener (C#)
- 16 Calculator Version 1
- 17 Set Button text (VB.net)
Add action listener to a server Button (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 EventArgs)
lblMessage.Text = "You pressed the alone button."
End Sub </SCRIPT> <HTML> <HEAD> <TITLE>Button Controls Sample Page</TITLE> </HEAD> <BODY> <form runat="server"> <asp:Label
id="lblMessage" Text="Press a button!" runat="server"
/>
<asp:Button
id="butAlone" text="Alone Button" OnClick="SubmitBtn1_Click" runat="server"
/>
</Form>
</BODY>
</HTML>
</source>
Add action listener to asp Button by delegate (C#)
<source lang="csharp">
<%@ 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" />
<asp:label id="Label1" runat="server">Please click da button!</asp:label> </form>
</body> </html>
</source>
asp:button: command name and arguments (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 CommandButton_Click(Sender As Object, E As CommandEventArgs)
lblButtonPressed.Text = "You pressed: " _ & E.rumandName & "
With this argument: " _ & E.rumandArgument & "
"
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"
/>
<asp:button
id="butSortName" text="Sort By Name" oncommand="CommandButton_Click" commandname="Sort" commandargument="By Name" runat="server"
/>
<asp:button
id="butSortPrice" text="Sort By Price" oncommand="CommandButton_Click" commandname="Sort" commandargument="By Price" runat="server"
/>
<asp:button
id="lnkbutNewSearch" text="New Search" oncommand="CommandButton_Click" commandname="New Search" commandargument="None" runat="server"
/> </form> </BODY> </HTML>
</source>
asp:button event to change the label text (VB.net)
<source lang="csharp">
<%@ 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">
<asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="Button"></asp:Button>
<asp:Label id="Label1" runat="server">Label</asp:Label>
<asp:Label id="Label2" runat="server">Label</asp:Label>
</form>
</body> </html>
</source>
asp button on client click (C#)
<source lang="csharp">
<%@ 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">
<asp:Button id="btnDelete" Text="Delete Website" OnClick="btnDelete_Click" OnClientClick="return confirm("Are you sure?");" Runat="server" /> <asp:Label id="lblResult" Runat="server" />
</form>
</body> </html>
</source>
asp button postbackurl (C#)
<source lang="csharp">
<%@ Page Language="C#" %> <html> <head id="Head1" runat="server">
<title>Button Search</title>
</head> <body>
<form id="form1" runat="server">
<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" />
</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">
<asp:Label id="lblSearch" Runat="server" />
</form>
</body> </html> --%>
</source>
Button action Command and Parameter (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 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"> <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"
/> </Form> </BODY> </HTML>
</source>
Button counter: click and change button label (C#)
<source lang="csharp">
<%@ 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">
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" />
</form>
</body> </html>
</source>
Button event Handler (C#)
<source lang="csharp">
<%@ 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>
<asp:Button id="Button1" onclick="ClickHandler" runat="server" Text="Click here to submit"></asp:Button>
<asp:Label id="Message" runat="server"></asp:Label> </form>
</body> </html>
</source>
CausesValidation="False"
<source lang="csharp">
<%@ 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">
<asp:Label ID="Label2" runat="server" Text="Must Be a Date and Earlier than Today"></asp:Label>
<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>
<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" />
<asp:ValidationSummary ID="ValidationSummary1" runat="server" ShowMessageBox="True" /> <asp:ValidationSummary ID="ValidationSummary2" runat="server" ShowMessageBox="True" ValidationGroup="DDL" />
</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... } }
}
</source>
Event driven page (VB.net)
<source lang="csharp">
<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" />
<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" />
Answer = <asp:Label id="lblAnswer" runat="server" /> </form>
</html>
</source>
Event in code behind file (VB.net)
<source lang="csharp">
<%@ 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">
<asp:label
id="lblMessage1" runat="Server"
/>
<asp:label
id="lblMessage2" runat="Server"
/>
Enter text to uppercase:
<asp:textbox
id="txtUCase" runat="Server"
/>
Add Numbers:
<asp:textbox
id="txtNumber1" runat="Server"
/>
<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 --%>
</source>
OnClientClick event
<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 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">
<asp:Button id="btnDelete" Text="Delete Website" OnClick="btnDelete_Click" OnClientClick="return confirm("Are you sure?");" Runat="server" />
<asp:Label id="lblResult" Runat="server" />
</form>
</body> </html>
</source>
onmouseover and onmouseout events
<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"> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server">
<title>Button Expando</title>
</head> <body>
<form id="form1" runat="server">
<asp:Button id="btnSubmit" Text="Submit" onmouseover="this.value="Click Here!"" onmouseout="this.value="Submit"" Runat="server" />
</form>
</body> </html>
</source>
Read input and do the calculation in Button action listener (C#)
<source lang="csharp">
<%@ 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">
Calculator Version 1
<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>
<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>
Answer = <asp:Label id="lblAnswer" runat="server"></asp:Label> </form>
</body> </html>
</source>
Set Button text (VB.net)
<source lang="csharp">
<%@ 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>
</source>