Gridview里 OnRowEditing="GridView1_RowEditing", 
有一个编辑按钮 ,commandname="edit"  ,已经能够实现编辑功能。 如果想再加一个 编辑按钮,以实现不同的功能,该怎么办呢? OnRowEditing和 commandname等东西都该怎么设置?比如我再加一个编辑按钮,让commandname="edit",那OnRowEditing和OnRowUpdating事件怎么能分别出是来自哪个编辑按钮的请求呢?迷惑死了……拜托,我是菜鸟,麻烦大家说详细点,多谢了!

解决方案 »

  1.   

    可以给其他的CommandName,比如 EditFile,然后再RowCommand事件中处理
        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "EditFile")
            { 
            
            }
        }
      

  2.   

    switch(CommandName)
    {
      case "del"
      ...  case "edit"
      ..
    }
      

  3.   

    也可以结合CommandArgument来区分一行中两个Edit按钮。
    <asp:TemplateField HeaderText="编辑1">
         <ItemTemplate>
             <asp:LinkButton ID="LinkButton1" CommandName="Edit" runat="server" Text="编辑1" CommandArgument="Edit1" />
         </ItemTemplate>
         </asp:TemplateField>
    <asp:TemplateField HeaderText="编辑2">
         <ItemTemplate>
            <asp:LinkButton ID="LinkButton2" CommandName="Edit" runat="server" Text="编辑2" CommandArgument="Edit2" />
         </ItemTemplate>
    </asp:TemplateField>
    每次点击GridView上的按钮时,必定先触发GridView.RowCommand,然后才是RowEditing、RowDeleting、RowInserting之类的。
    可以在RowCommand事件里通过CommandArgument区分后,把信息存入一个ViewState(用Session也行),然后在RowEditing事件里读取ViewState信息后,根据情况进行处理。
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
            string arg = e.CommandArgument.ToString();
            if(arg == "Edit1" || arg == "Edit2")
                ViewState["X"] = arg;        
    }protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
            if (ViewState["X"] != null)
            {
                if (ViewState["X"].ToString() == "Edit1")
                {
                    Literal msg = new Literal();
                    msg.Text = "<script>alert('Edit1被点击了');</script>";
                    this.Controls.Add(msg);
                }
                else if (ViewState["X"].ToString() == "Edit2")
                {
                    Literal msg = new Literal();
                    msg.Text = "<script>alert('Edit2被点击了');</script>";
                    this.Controls.Add(msg);
                    e.Cancel = true;
                }
            }
            else
            {
                e.Cancel = true;
            }
    }