在gridview中创建链接按钮

时间:2015-10-02 06:34:01

标签: c# asp.net gridview

我有一个GridView,其中的文件显示为Delete按钮。

如果我点击delete,则所选行值会更改为“否”您确定吗?“是”。 单击删除时,将创建no / yes作为LinkBut​​tons。

现在我希望它们像gridview行命令一样运行,但我不能让它们起作用。

<asp:GridView ID="gvFiles" runat="server"  AutoGenerateColumns="true"      CssClass="mGrid"  AlternatingRowStyle-CssClass="alt" PagerStyle-CssClass="pgr"   GridLines="None" AllowPaging="True" EnableViewState="true" OnRowCommand="gvFiles_RowCommand" OnPageIndexChanging="gvFiles_PageIndexChanging" >
    <Columns>                
       <asp:ButtonField ButtonType="Link" Text="delete" CommandName="remove"  />   
    </Columns>
</asp:GridView>


protected void gvFiles_RowCommand(object sender, GridViewCommandEventArgs e)        
{

            SQLConnection SQLCon = new SQLConnection();
            string sCid = Request.QueryString["CatID"].ToString();
            string sdev = Request.QueryString["devID"].ToString();
            int index = Convert.ToInt32(e.CommandArgument);
            DataTable dtFiles = (DataTable)Session["ActualDevFiles"];
            DataRow dr = dtFiles.Rows[index];
            Guid sFileID = dr.Field<Guid>("FileID");
            LinkButton btnyes = new LinkButton();
            btnyes.Text="yes";
            //btnyes.Command+=new CommandEventHandler(gvFiles_RowCommand);

            LinkButton btnno = new LinkButton();
            btnno.Text="no";
            //btnno.Command+=new CommandEventHandler(gvFiles_RowCommand);


            if (e.CommandName == "remove")
            {
                gvFiles.Rows[index].BorderStyle = BorderStyle.Solid;
                gvFiles.Rows[index].BorderColor = Color.FromName("Red");
                gvFiles.Rows[index].Cells[1].Text = "Are you sure?";
                gvFiles.Rows[index].Cells[2].Text = "yes";
                gvFiles.Rows[index].Cells[0].Text = "no";
                gvFiles.Rows[index].Cells[2].Controls.Add(btnyes);
                gvFiles.Rows[index].Cells[0].Controls.Add(btnno);
            }
            if (e.CommandName == "yes")
            {
                SQLCon.SQLCommand("DELETE TBL_Files WHERE FileID='" + sFileID + "' ");
                Response.Redirect("~/EditItem.aspx?CatID="+sCid+"&devID="+sdev);
            }
            if (e.CommandName == "no")
            {
                gvFiles.Rows[index].BorderStyle = BorderStyle.None;

            }
        }

我知道我错过了必不可少的东西,但不知道该怎么做。提前谢谢。

0 个答案:

没有答案