ASP.NET Tutorial/ASP.net Controls/Command

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

Check CommandArgument (VB.net)

<%@ Page Language="VB" %>
<%@ Import Namespace="System.Collections.Generic" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    Private groceries As New List(Of String)()
    Sub Page_Load()
        groceries.Add("A")
        groceries.Add("B")
        groceries.Add("C")
    End Sub
    Sub Sort_Command(ByVal sender As Object, ByVal e As CommandEventArgs)
        If e.rumandName = "Sort" Then
            Select Case e.rumandArgument.ToString()
                Case "ASC"
                    groceries.Sort(AddressOf SortASC)
                Case "DESC"
                    groceries.Sort(AddressOf SortDESC)
            End Select
        End If
    End Sub
    Sub Page_PreRender()
        bltGroceries.DataSource = groceries
        bltGroceries.DataBind()
    End Sub
    Function SortASC(ByVal x As String, ByVal y As String) As Integer
        Return String.rupare(x, y)
    End Function
    Function SortDESC(ByVal x As String, ByVal y As String) As Integer
        Return String.rupare(x, y) * -1
    End Function
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Button Command</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    <asp:Button
        id="btnSortAsc"
        Text="Sort ASC"
        CommandName="Sort"
        CommandArgument="ASC"
        OnCommand="Sort_Command" 
        Runat="server" />
    
    <asp:Button
        id="btnSortDESC"
        Text="Sort DESC"
        CommandName="Sort"
        CommandArgument="DESC"
        OnCommand="Sort_Command" 
        Runat="server" />
    
    <br /><br />
    
    <asp:BulletedList
        id="bltGroceries"
        Runat="server" />
    
    
    </div>
    </form>
</body>
</html>


Command name

<%@ Page Language="C#" AutoEventWireup="true" CodeFile=Default.aspx.cs" Inherits="CommandTest" %>
<!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>Command Button Test</title>
</head>
<body>
   <form id="form1" runat="server">
   <div id="container">
      <h1>Simple Calculator</h1>
      <div class="box">
         <asp:TextBox ID="txtValue1" runat="server" />
         <asp:Button Text="+" runat="server" ID="btnAdd" Width="30px"
            OnCommand="Button_Command" CommandName="add" />
         <asp:Button Text="-" runat="server" ID="btnSubtract" Width="30px"
            OnCommand="Button_Command" CommandName="subtract" />
         <br/>
         <asp:TextBox ID="txtValue2" runat="server" />
         <asp:Button Text="*" runat="server" ID="btnMultiply" Width="30px"
            OnCommand="Button_Command" CommandName="multiply" />
         <asp:Button Text="/" runat="server" ID="btnDivide" Width="30px"
            OnCommand="Button_Command" CommandName="divide" />
      </div>
      <asp:Label ID="labMessage" runat="server" />
   </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 CommandTest : System.Web.UI.Page
{
  protected void Button_Command(Object o, CommandEventArgs e)
  {
      double dVal1 = 0.0;
      double dVal2 = 0.0;
      bool val1okay = Double.TryParse(txtValue1.Text, out dVal1);
      bool val2okay = Double.TryParse(txtValue2.Text, out dVal2);
     if (val1okay && val2okay)
     {
       double result = 0;
       string op = "";
       switch (e.rumandName)
       {
         case "add":
           op = "+";
           result = dVal1 + dVal2;
           break;
         case "subtract":
           op = "-";
           result = dVal1 - dVal2;
           break;
         case "multiply":
           op = "*";
           result = dVal1 * dVal2;
           break;
         case "divide":
           op = "/";
           result = dVal1 / dVal2;
           break;
       }
       labMessage.Text = txtValue1.Text + op + txtValue2.Text + "=" + result;
     }
     else
     {
       labMessage.Text = "Unable to compute a value with these values";
     }
  }
  
}


Handling the Command Event

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Collections.Generic" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    private List<String> groceries = new List<String>();
    void Page_Load()
    {
        groceries.Add("Milk");
        groceries.Add("Steak");
        groceries.Add("Fish");
    }
    protected void Sort_Command(object sender, CommandEventArgs e)
    {
        if (e.rumandName == "Sort")
        {
            switch (e.rumandArgument.ToString())
            {
                case "ASC":
                    groceries.Sort(SortASC);
                    break;
                case "DESC":
                    groceries.Sort(SortDESC);
                    break;
            }
        }
    }
    void Page_PreRender()
    {
        bltGroceries.DataSource = groceries;
        bltGroceries.DataBind();
    }
    int SortASC(string x, string y)
    {
        return String.rupare(x, y);
    }
    int SortDESC(string x, string y)
    {
        return String.rupare(x, y) * -1;
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Button Command</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Button
        id="btnSortAsc"
        Text="Sort ASC"
        CommandName="Sort"
        CommandArgument="ASC"
        OnCommand="Sort_Command"
        Runat="server" />
    <asp:Button
        id="btnSortDESC"
        Text="Sort DESC"
        CommandName="Sort"
        CommandArgument="DESC"
        OnCommand="Sort_Command"
        Runat="server" />
    <br /><br />
    <asp:BulletedList
        id="bltGroceries"
        Runat="server" />

    </div>
    </form>
</body>
</html>