控件的事件属性不就能实现很多功能吗?为什么还有CommandName属性,这个属性怎么用呢?有什么作用? 请高手指点一下!3ks

解决方案 »

  1.   

    当在 Web 页上具有多个 Button 控件时,可使用 CommandName 属性来指定或确定与每一 Button 控件关联的命令名。可以用标识要执行命令的任意字符串来设置 CommandName 属性。然后,可以编程方式确定 Button 控件的命令名并执行相应的操作。
      

  2.   

    使用 CommandName 属性将命令名(如 Add 或 Remove)与按钮相关联。可以将 CommandName 属性设置为任意字符串,该字符串标识当单击命令按钮时要执行的操作。然后可以编程确定 ItemCommand 事件的事件处理程序中的命令名并执行适当的操作
      

  3.   

    CommandName是类似于Button的象LinkButton等控件的一个属性,写的时候直接就可以在HTML里边写值就行的。如<asp:LinkButton id="linkAdd" runat="server" CommandName="Add" Text="增加"></asp:LinkButton>
    CommandName的用处是在服务器端事件中可以用到这个属性,根据属性值来确定适当的操作。比如在DataGrid的ItemCommand事件中,你可以根据CommandName来判断执行哪个操作:如:
    if (e.CommandName == "Add")
    {
        //Do something about adding
    }
    if (e.CommandName == "Update")
    {
        //Do something about updating
    }
      

  4.   

    Button控件的CommandName属性相当于ID属性,只是一个作用标识,提示出单击按钮所要进行的操作。通过它的CommandName属性来调用相对应的操作。
      

  5.   

    我这里有一段代码:当我把CommandName="select"去掉的话就无法对显示的内容编辑了,我不知道是为什么?请告知
    <asp:DataList ID="DataList1" Runat="server" SelectedItemStyle-BackColor="#cccc99" 
                  RepeatLayout="Table" RepeatDirection="Horizontal" DataKeyField="UserID">
        <ItemTemplate>
            <asp:LinkButton ID="button1" Runat="server" Text='<%# ((DataRowView)Container.DataItem["FirstName"].ToString() %>' CommandName="select" />
        </ItemTemplate>
        <SelectedItemTemplat
    <%# ((DataRowView)Container.DataItem)["Phone"] %><br>
    <%# ((DataRowView)Container.DataItem)["Address"] %><br>
        </SelectedItemTemplate>
    </asp:DataList>