WebControl 之Button CommandName的使用

 

<%@ Page Language="C#" AutoEventWireup="True" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html  >
<head runat="server">
    <title>Button CommandName Example</title>
<script runat="server">

      void CommandBtn_Click(Object sender, CommandEventArgs e)
      {

         switch(e.CommandName)
         {

            case "Sort":

               // Call the method to sort the list.
               Sort_List((String)e.CommandArgument);
               break;

            case "Submit":

               // Display a message for the Submit button being clicked.
               Message.Text = "You clicked the Submit button";

               // Test whether the command argument is an empty string ("").
               if((String)e.CommandArgument == "")
               {
                  // End the message.
                  Message.Text += ".";
               }
               else
               {
                  // Display an error message for the command argument.
                  Message.Text += ", however the command argument is not recogized.";
               }               
               break;

            default:

               // The command name is not recognized. Display an error message.
               Message.Text = "Command name not recogized.";
               break;

         }

      }

      void Sort_List(string commandArgument)
      {

         switch(commandArgument)
         {

            case "Ascending":
 
               // Insert code to sort the list in ascending order here.
               Message.Text = "You clicked the Sort Ascending button.";
               break;

            case "Descending":
             
               // Insert code to sort the list in descending order here.
               Message.Text = "You clicked the Sort Descending button.";
               break;

            default:
       
               // The command argument is not recognized. Display an error message.
               Message.Text = "Command argument not recogized.";
               break;

         }

      }

   </script>

</head>
 
<body>

   <form runat="server">

      <h3>Button CommandName Example</h3>

      Click on one of the command buttons.

      <br /><br />
 
      <asp:Button
           Text="Sort Ascending"
           CommandName="Sort"
           CommandArgument="Ascending"
           OnCommand="CommandBtn_Click" //Command事件 单击按钮并定义关联的命令时激发,不能采用button的Click事件,否则 没有 调不出CommandName和 CommandArgument
           runat="server"/>

      &nbsp;

      <asp:Button
           Text="Sort Descending"
           CommandName="Sort"
           CommandArgument="Descending"
           OnCommand="CommandBtn_Click"
           runat="server"/>

      <br /><br />

      <asp:Button
           Text="Submit"
           CommandName="Submit"
           OnCommand="CommandBtn_Click"
           runat="server"/>

      &nbsp;

      <asp:Button
           Text="Unknown Command Name"
           CommandName="UnknownName"
           CommandArgument="UnknownArgument"
           OnCommand="CommandBtn_Click"
           runat="server"/>

      &nbsp;

      <asp:Button
           Text="Submit Unknown Command Argument"
           CommandName="Submit"
           CommandArgument="UnknownArgument"
           OnCommand="CommandBtn_Click"
           runat="server"/>
      
      <br /><br />

      <asp:Label runat="server"/>
 
   </form>
 
</body>
</html>

展开阅读全文

((Button)sender).CommandName出了一点问题,求解。

06-22

[code=C#]“/HongTianChuan”应用程序中的服务器错误。rn--------------------------------------------------------------------------------rnrn编译错误 rn说明: 在编译向该请求提供服务所需资源的过程中出现错误。请检查下列特定错误详细信息并适当地修改源代码。 rnrn编译器错误消息: CS1061: “Button”不包含“CommandName”的定义,并且找不到可接受类型为“Button”的第一个参数的扩展方法“CommandName”(是否缺少 using 指令或程序集引用?)rnrn源错误:rnrn rnrn行 24: protected void Button2_Click(object sender, EventArgs e)rn行 25: rn行 26: switch (((Button)sender).CommandName.ToString())rn行 27: rn行 28: case "1":rn rnrn源文件: d:\Site\HongTianChuan\Button.aspx.cs 行: 26 rn[/code]rnrn[code=C#]<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Button.aspx.cs" Inherits="Button" %>rnrnrnrnrnrn 无标题页rnrnrn rn rn rn rn rn rn rn rn rn rn rn rn rnrnrn[/code]rnrn[code=C#]using System;rnusing System.Collections;rnusing System.Configuration;rnusing System.Data;rnusing System.Linq;rnusing System.Web;rnusing System.Web.Security;rnusing System.Web.UI;rnusing System.Web.UI.HtmlControls;rnusing System.Web.UI.WebControls;rnusing System.Web.UI.WebControls.WebParts;rnusing System.Xml.Linq;rnrnpublic partial class Button : System.Web.UI.Pagernrn protected void Page_Load(object sender, EventArgs e)rn rnrn rn protected void Button1_Click(object sender, EventArgs e)rn rnrn rn protected void Button2_Click(object sender, EventArgs e)rn rn switch (((Button)sender).CommandName.ToString())rn rn case "1":rn Label1.Text = "James";rn break;rn case "2":rn Label1.Text = "黑大";rn break;rn case "3":rn Label1.Text = "怀念ing";rn break;rn rn rnrn[/code]rnrn这是什么问题啊。((Button)sender)后面的CommandName点不出来 求解诶。。。rn 论坛

没有更多推荐了,返回首页