实现对gridview删除行时弹出确认对话框的四种方法
實現對gridview刪除行時彈出確認對話框的四種方法
在.net2.0中,實現對gridview刪除行時彈出確認對話框的四種方法 1,GridView中如何使用CommandField刪除時,彈出確認框?
在VS2005提供的GridView中我們可以直接添加一個CommandField刪除列:<asp:CommandField ShowDeleteButton="True" />,完后在它的RowDeleting事件中完成刪除。但在多半我們在做這種刪除操作時都需要先讓操作者再確認下,完后再進行刪除,以避免誤操作引起的誤刪除。
可以通過下面方法給GridView刪除前加上個確認對話框。
首先,在GridView的屬性對框話框中點擊“Columns”進入它的“字段”設計器。接著在“字段”設計器中選擇以前已加上的那個CommandField“刪除”列,這時在它的屬性列表下會看到一個“將此它段轉換為 TemplateFied”的項,點擊將它轉換為TemplateFied列。
完后退出該字段設計器,切換到源碼視圖你會發現該列已由原來的:<asp:CommandField ShowDeleteButton="True" />
變為了:
CODE:
<asp:TemplateField ShowHeader=&quot;False&quot;>
??? <ItemTemplate>
???? <asp:LinkButton ID=&quot;LinkButton1&quot; runat=&quot;server&quot; CausesValidation=&quot;False&quot; CommandName=&quot;Delete&quot;??? Text=&quot;刪除&quot;></asp:LinkButton>
</ItemTemplate>
最后在<asp:LinkButton>中加入:
OnClientClick="javascript:return confirm('真的要刪除嗎?');"
或者加入:
OnClientClick="if(confirm('你確定要刪除此記錄嗎?')){return true;}else{return false;}"
這樣點擊刪除時就會先在客戶端彈出“確認要刪除嗎?”對話框,而原來在RowDeleting事件中寫的代碼完全不用改變。
注意:CommandName="delete" CommandName 一定要設為"delete",否則將不觸發GridView中的RowDeleting 事件.
注意:在事件protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)里需要設置GridView中的DataKeysName = Fid 時,才可以找到相應的ID.(Fid為表的主鍵 id)
?
2,
CODE:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
??? if (e.Row.RowType == DataControlRowType.DataRow)
??? {
??????? if (e.Row.RowIndex > -1)
??????? {
??????????? int id = Convert.ToInt32(GridView1.DataKeys[e.Row.RowIndex].Value);
??????????? LinkButton lbtnDelete = (LinkButton)e.Row.FindControl(&quot;lbtnDelete&quot;);
??????? if (lbtnDelete != null)
??????? {
??????????? lbtnDelete.CommandArgument = id.ToString();
??????????? lbtnDelete.Attributes.Add(&quot;onClick&quot;, &quot;<script>return confirm(’是否確認刪除!’)</script>&quot;);
??????? }
??? }
}
3,針對C#中的Windows窗體程序,可以先引用System.windwos.Forms,然后在進行處理;對于Web應用程序不適合。
CODE:
using System.Windows.Forms;
protected void gvNewList_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
??? DialogResult result = MessageBox.Show(&quot;確定要刪除本行嗎?&quot;, &quot;信息提示!&quot;, MessageBoxButtons.YesNo, MessageBoxIcon.Question,MessageBoxDefaultButton.Button2,MessageBoxOptions.ServiceNotification);
??? if (result == DialogResult.Yes)
??? {
??????? e.Cancel = false;
??? }
??? else
??? {
??????? e.Cancel = true;
??? }
}
4,添加一個刪除列
CODE:
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
??? TableCell tc = (TableCell)e.Row.Cells[e.Row.Cells.Count - 1];
??? for (int i = 0; i < tc.Controls.Count; i += 2)
??? {
??????? // cerco il controllo ImageButton (ho utilizzato quello)
??????? Object o = tc.Controls[i];
??????? if (o is ImageButton)
??????? {
??????????? // controllo trovato!
??????????? // ora aggiungo l’evento js onClick per chiedere conferma all’utente
??????????? ImageButton lb = (ImageButton) o;
??????????? ((ImageButton)lb).Attributes.Add(&quot;onclick&quot;, @&quot;javascript:return confirm('Attenzione: sicuro di voler cancellare?');&quot;);
??????? }
??? }
}
-------------------------------------------------------------
CODE:
<asp:TemplateField ShowHeader=&quot;False&quot;>
<ItemStyle HorizontalAlign=&quot;Center&quot; Width=&quot;16px&quot; />
<ItemTemplate>
<asp:ImageButton ID=&quot;imgDelete&quot; runat=&quot;server&quot; CausesValidation=&quot;False&quot; CommandName=&quot;Delete&quot; ImageUrl=&quot;~/img/ico_elimina.gif&quot; AlternateText=&quot;Cancella data&quot; OnClientClick=&quot;return confirm(’Sicuro di voler cancellare?’);&quot; />
</ItemTemplate>
</asp:TemplateField>
以上方法總結
---------Template 方式 -----------------------------------------------
CODE:
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<aspinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete"
Text="刪除" OnClientClick=’return confirm("Are you sure you want to delete this record?");’></aspinkButton>
</ItemTemplate>
</asp:TemplateField>
-------------RowDeleting method------------------------------------------------
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
??? Response.Write("<script language=javascript>window.confirm('確定刪除嗎?')</script>");
}
-------------RowDataBound method--------------------------------------------------------------
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
??? if (e.Row.RowType == DataControlRowType.DataRow)
??? {
??????? ((LinkButton)e.Row.Cells[4].Controls[0]).Attributes.Add("onclick", "javascript:return confirm(’確實要刪除該記錄嗎?’)");
??? }
}
另外:在web頁面中,在按鈕事件中添加彈出對話框的方法還有,
在前臺點擊按鈕如b1,事件為b1_click,在后臺寫b1_click的代碼,實現點擊后彈出:“確定要刪除嗎?”選擇確定,跳轉頁面如:aa.aspx,選擇取消,無動作!
protected void Page_Load(object sender, EventArgs e)
{
??? b1.Attributes.Add("onclick", "javascript:if(confirm('確定要刪除嗎?')){}else{return false;}");
}
轉載于:https://www.cnblogs.com/blogbai/archive/2012/09/27/2705853.html
總結
以上是生活随笔為你收集整理的实现对gridview删除行时弹出确认对话框的四种方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Sub-Projects in Xcod
- 下一篇: 伸展树