為GridView添加刪除確認的若干種方法

來源:互聯網
上載者:User

先介紹最簡單的方法

1.利用OnClientClick事件

<ItemTemplate>
<asp:ImageButton ID="IbnDel" runat="server" ToolTip="刪除" CommandName="Delete" ImageUrl="/Images/delete01.gif" OnClientClick="javascript:return confirm('你確定要刪除嗎?將不可恢複哦');" />
</ItemTemplate>

2.後台代碼裡寫,算最簡便的一種方法了

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
   if (e.Row.RowType == DataControlRowType.DataRow)
   {
      e.Row.Cells[8].Attributes.Add("onclick", "return confirm('刪除此條記錄?');");
   }
}

3.遍曆每個元素,找到你要的控制項,添加刪除確認.

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
   LinkButton l;
   for(int i=0;i<e.Row.Cells.Count;i++)
      {
         for (int j = 0; j < e.Row.Cells[i].Controls.Count; j++)
            {
               if (e.Row.Cells[i].Controls[j].GetType().ToString() == "System.Web.UI.WebControls.DataControlLinkButton")
                  {
                     l = (LinkButton)e.Row.Cells[i].Controls[j];
                     if (l.Text == "刪除")
                        {
                           l.Attributes.Add("onclick", "return confirm('確定要刪除這個分類嗎?');");
                        }
                  }
            }
      }
}

4.最後附上DataGrid的刪除確認方法

private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
        {
            switch(e.Item.ItemType)//添加刪除提示
            {
                case ListItemType.AlternatingItem:
                case ListItemType.EditItem:
                case ListItemType.Item:
                {
                    LinkButton lbtDelete = (LinkButton)e.Item.Cells[2].Controls[0];
                    lbtDelete.Attributes.Add("onclick","return confirm('您確定要刪除[" + e.Item.Cells[0].Text + "]的資料嗎?此操作將刪除[" + e.Item.Cells[0].Text + "]的所有小類以及所有新聞!');");
                    break;
                }
            }
        }


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.