完全代碼實現GridView刪除按鈕提示

來源:互聯網
上載者:User
今天終於實現了完全代碼下使GridView中的刪除按鈕實現刪除提示的功能。

首先,HTML代碼:<asp:GridView ID="SubjectGrid" runat="server">
    </asp:GridView>

第二步,初始化GridView Columns:void Page_Load(Object sender, EventArgs e)
{
    ShowGrid();
}

private void ShowGrid()
        {
            DataTable customerTable = new DataTable("Customers");
            
            grdSubject.AutoGenerateColumns = false;
            grdSubject.ShowHeader = false;
            grdSubject.DataKeyNames = new String[] { "Id" };
            DataControlFieldCollection dcfc = grdSubject.Columns;
            dcfc.Clear();
            BoundField bf;

            bf = new BoundField();
            bf.DataField = "Id";
            bf.Visible = false;
            dcfc.Add(bf);

            bf = new BoundField();
            bf.DataField = "Title";
            dcfc.Add(bf);

            bf = new BoundField();
            bf.DataField = "BeginDate";
            bf.SortExpression = "BeginDate";
            bf.HtmlEncode = false;
            bf.DataFormatString = "{0:yyyy-MM-dd}";
            dcfc.Add(bf);

            bf = new BoundField();
            bf.DataField = "EndDate";
            bf.HtmlEncode = false;
            bf.DataFormatString = "{0:yyyy-MM-dd}";
            dcfc.Add(bf);

            ButtonField selectRow = new ButtonField();
            selectRow.ButtonType = ButtonType.Button;
            selectRow.CommandName = "Select";
            selectRow.Text = "選擇";
            dcfc.Add(selectRow);

            ButtonField delRow = new ButtonField();
            delRow.ButtonType = ButtonType.Button;
            delRow.AccessibleHeaderText = "Delete";
            delRow.CommandName = "Delete";
            delRow.Text = "刪除";
            delRow.CausesValidation = true;
            dcfc.Add(delRow);

            grdSubject.DataSource = dt.DefaultView;
            grdSubject.DataBind();
        }

第三步:RowDataBoundvoid grdSubject_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //判斷是否是DataRow
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //滑鼠經過Row時的效果
                e.Row.Attributes.Add("onmouseover", "e=this.style.backgroundColor; this.style.backgroundColor='linen'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=e");

                //當開始時間大於現在時間,顯示行為藍色
                if (DateTime.Parse(e.Row.Cells[2].Text) > DateTime.Now)
                {
                    e.Row.BackColor = Color.LightSkyBlue;
                }
                //當結束時間小於現在時間,顯示行為灰色
                if (DateTime.Parse(e.Row.Cells[3].Text) < DateTime.Now)
                {
                    e.Row.BackColor = Color.Silver;
                }

                //當點擊刪除按鈕時啟用提示
                Button btn = (Button)e.Row.Cells[5].Controls[0];
                btn.Attributes.Add("onclick", "javascript:return confirm('你確認要刪除:\"" + e.Row.Cells[1].Text + "\"嗎?')");
            }
        }

其中的關鍵是Button btn=(Button)e.Row.Cells[5].Controls[0]; 即聲明此事件是由第6列中的第一個控制項調用。

第四步:執行void grdSubject_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

        }

        void grdSubject_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            //單擊Grid中按鈕時發生throw new Exception("The method or operation is not implemented.");
            int selIndex = Convert.ToInt32(e.CommandArgument);
            GridViewRow selectedRow = grdSubject.Rows[selIndex];

            if (e.CommandName == "Select")
            {
                txtSubjectTitle.Text = selectedRow.Cells[1].Text;
                dateBegin.Value = selectedRow.Cells[2].Text;
                dateEnd.Value = selectedRow.Cells[3].Text;

            }
            if (e.CommandName == "Delete")
            {
                Hsf.Touch.Dto.Subject item = new Hsf.Touch.Dto.Subject();
                item.Id = int.Parse(grdSubject.DataKeys[selIndex].Value.ToString());
                try
                {
                    TouchFactory.CreateSubjectManage().Delete(item);
                    //刪除成功,清除輸入框內容
                    Clear();
                }
                catch (Exception err)
                {
                    ShowMessageBox("刪除失敗 \n" + err.Message);
                }
            }
            
            ShowGrid();
        }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.