哪些控件有 CommandName属性?谢谢~

解决方案 »

  1.   

    sp1234请教一下
    http://topic.csdn.net/u/20081114/10/5b15e4c9-d7e4-416e-97ae-b641fcc791ba.html
      

  2.   

            if (e.CommandName == "Add")
            {
                foreach (GridViewRow row in this.GridView1.Rows)
                {
                    if (this.GridView1.DataKeys[row.RowIndex].Value.ToString() == e.CommandArgument.ToString())
                    {
                        TextBox txt = (TextBox)row.FindControl("txtType");
                        string strConn = ConfigurationManager.ConnectionStrings["DB_website"].ConnectionString;                    SqlDataSource ds = new SqlDataSource();
                        ds.ConnectionString = strConn;
                        ds.InsertCommand = string.Format("insert into tb_websitetype values({0},'{1}')", 1, txt.Text);
                        if (ds.Insert() > 0)
                        {
                            wmb.MessageBox("添加类型成功~");
                            DropDownList ddl = (DropDownList)row.FindControl("ddl");
                            ddl.DataBind();
                        }
                    }
                }
            }在这段代码中,e.CommandArgument的值会是什么? 那么button属性的CommandArgument值应该设置为什么?
      

  3.   

    就button类以及他们的继承类专有吧?
      

  4.   

    你这段代码绕了一个大圈子干了很简单的(无需对控件进行查找编程)的事情。那一类根据GridView录入的行进行数据库Insert的编程方式,可以参考:http://www.google.cn/search?hl=zh-CN&newwindow=1&rls=com.microsoft%3A*%3AIE-SearchBox&rlz=1I7GGIJ&q=asp.net+insertmethod&btnG=Google+%E6%90%9C%E7%B4%A2&meta=lr%3Dlang_zh-CN%7Clang_zh-TW&aq=f&oq=
      

  5.   

    asp.net内置的控件中有5、6个有CommandName。但是你可以为任意控件(包括用户控件)自己增加这个属性,甚至增加常见的控制方法。例如:using System;
    using System.Web.UI.MobileControls;
    using System.Web.UI.WebControls;public class MyTextBox:  TextBox
    {
        public string CommandName { get; set; }    public string CommandArgument { get; set; }    protected override void OnTextChanged(EventArgs e)
        {
            base.OnTextChanged(e);
            this.RaiseBubbleEvent(this, new CommandEventArgs(this.CommandName, this.CommandArgument));
        }
    }
    这样,使用MyTextBox替代原来的TextBox控件,就可以在文本框输入时像LinkButton的CommandName一样为父控件触发相关的操作。可是,这跟你后边描述的问题其实没有什么关系。
      

  6.   

    上面,“using System.Web.UI.MobileControls;”这一行是多余的,可以删去。不要因为有了这一行引起误会。
      

  7.   

    别的我没有用过,不知道,反正我知道LinkButton有
      

  8.   

    命令按钮都有,button ImageButton 等等
    <asp:Button id="MyButton" 
    Text="label" 
    CommandName="command" 
    CommandArgument="commandargument" 
    CausesValidation="true | false" 
    OnClick="OnClickMethod" 
    runat="server"/> 
    备注 
    Button 控件允许您在 Web 窗体页上创建普通按钮。可以创建的按钮类型有两种。可以创建“提交”按钮或“命令”按钮。 默认情况下,Button 控件是“提交”按钮。“提交”按钮没有与按钮关联的命令名(由 CommandName 属性指定),它只是将 Web 页发送回服务器。可以为 Click 事件提供事件处理程序,以通过编程方式控制单击“提交”按钮时执行的操作。 “命令”按钮通过设置 CommandName 属性而具有与按钮关联的命令名(如“排序”)。这使您可以在 Web 窗体页上创建多个 Button 控件,并在 Command 事件的事件处理程序中以编程方式确定单击了哪个 Button 控件。也可以将 CommandArgument 属性与“命令”按钮一起使用,以提供有关要执行的命令(如 Ascending)的附加信息。可以为 Command 事件提供事件处理程序,以通过编程方式控制单击“命令”按钮时执行的操作。 默认情况下,单击 Button 控件时会执行页验证。页验证确定与该页上验证控件关联的输入控件是否通过该验证控件指定的验证规则。如果某个 Button 控件(如“重置”按钮)需要禁用此行为,则将 CausesValidation 属性设置为 false。 注意 由于 <asp:Button> 元素没有内容,因此可用 /> 结束该标记,而不必使用单独的结束标记。 
    若要指定在 Button 控件中显示的标题,请设置 Text 属性。 有关 Button 控件的属性和事件的详细信息,请参见 Button 类文档。 示例 
    以下示例说明如何在 .aspx 文件中声明 submit 按钮控件。 <asp:Button id="SubmitButton" 
    Text="Submit" 
    OnClick="SubmitBtn_Click" 
    runat="server"/> 
    以下示例说明如何在 .aspx 文件中声明 command 按钮控件。 <asp:Button id="SortAscendingButton" 
    Text="Sort Ascending" 
    CommandName="Sort" 
    CommandArgument="Ascending" 
    OnCommand="CommandBtn_Click" 
    runat="server"/> 
    以下示例显示一个事件处理方法,该方法获取按钮单击并显示从该按钮的 CommandName 和 CommandArgument 属性中传递的信息。 [Visual Basic] 
    Sub CommandBtn_Click(sender As Object, e As CommandEventArgs) 
    Message.Text = "You clicked the " & e.CommandName & _ 
    " - " & e.CommandArgument & " button." 
    End Sub 
    [C#] 
    void CommandBtn_Click(Object sender, CommandEventArgs e) 

    Message.Text = "You clicked the " + e.CommandName + 
    " - " + e.CommandArgument + " button."; 
    }
      

  9.   

    我设置了断点,发现this.GridView1.DataKeys[row.RowIndex].Value.ToString() 的值是从1开始,随着循环递增,但是  e.CommandArgument的值呢就是“”,空的~  因此呢  if (this.GridView1.DataKeys[row.RowIndex].Value.ToString() == e.CommandArgument.ToString()) 
    这个条件永远都不会成立,那么要使之成立,怎么办?在button的CommandArgument属性值怎么设置?
      

  10.   


     这样设置:
                   <asp:TemplateField HeaderText="添加">
                       <ItemTemplate>
                         <asp:Button ID="btnAdd" Text="添加" CssClass="btn_long" CommandName="Add" CommandArgument="<%# ((GridViewRow)Container).RowIndex %>" runat="server" />
                       </ItemTemplate>
                    </asp:TemplateField>
      

  11.   


    刚才看错代码了:<asp:TemplateField HeaderText="添加">
                       <ItemTemplate>
                         <asp:Button ID="btnAdd" Text="添加" CssClass="btn_long" CommandName="Add" CommandArgument="<%#DataBinder.Eval(Container.DataItem,"F_FITTING_ID")%>" runat="server" />
                       </ItemTemplate>
                    </asp:TemplateField>
    其中F_FITTING_ID是你GridView1的属性DataKeyNames 设置的字段