ASP.NET Gridview仅在确认时删除行

时间:2012-01-26 23:14:14

标签: c# asp.net gridview confirm

我实现了gridview方法的onRowDeleting。我想用弹出消息提示用户确认是否要删除此项目。 如果用户单击“确定”,“确认”或类似的东西,那么我希望处理onRowsDeleting并删除记录。但是如果用户按下“否​​”,那么我希望取消该方法并且不删除记录。

如何做到这一点?

以下是代码中的gridviewonRowDeleting方法。

<asp:GridView runat="server" ID="gvShowQuestionnaires" HeaderStyle-CssClass="table_header" CssClass="view" AlternatingRowStyle-CssClass="alt" AlternatingRowStyle-BackColor="#f3f4f8" AutoGenerateColumns="False" 
            DataKeyNames='QuestionnaireID' OnRowDeleting="gvShowQuestionnaires_RowDeleting" OnRowEditing="gvShowQuestionnaires_RowEdit" OnSelectedIndexChanged="gvShowQuestionnaires_SelectedIndexChanged" FooterStyle-CssClass="view_table_footer" > 
    <Columns>
         <asp:BoundField DataField="QuestionnaireID" HeaderText="ID" HeaderStyle-Width="80px" ItemStyle-CssClass="bo"></asp:BoundField>
         <asp:BoundField DataField="QuestionnaireName" HeaderText="Questionnaire Name" />                     
         <asp:ButtonField CommandName="select" ButtonType="Link" Text="view results" />
         <asp:CommandField HeaderText="Options" CausesValidation="true" ShowDeleteButton="True" ShowEditButton="true" EditText="Edit">
         </asp:CommandField>
     </Columns> 
</asp:GridView>

protected void gvShowQuestionnaires_RowDeleting(object sender, GridViewDeleteEventArgs e)
{       
    int questionnaireID = (int)gvShowQuestionnaires.DataKeys[Convert.ToInt32(e.RowIndex)].Value; 
    GetData.DeleteQuestionnaire(questionnaireID);
    gvShowQuestionnaires.DataSource = DT;
    gvShowQuestionnaires.DataBind();
    lblActivity.Visible = true;
    lblActivity.Text = "Your questionnaire has been deleted";
}

5 个答案:

答案 0 :(得分:11)

您应该使用javascript在客户端执行该操作。

因此,您可以处理GridView的RowDataBound事件,将其添加到删除按钮的OnClientClick

protected void gvShowQuestionnaires_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        // reference the Delete LinkButton
        LinkButton db = (LinkButton)e.Row.Cells[3].Controls[0];

        db.OnClientClick = "return confirm('Are you certain you want to delete this questionnaire?');";
    }
}

http://msdn.microsoft.com/en-us/library/bb428868.aspx

此js-function将返回用户是否点击了okcancel。如果js-eventhandler返回false,页面将不会回发到服务器。

答案 1 :(得分:3)

&#34;返回确认(..&#34;)Javascript在正确的轨道上。

问题是ASP.NET会追加js来调用__doPostBack

因此,您可以在HTML中为删除按钮获取类似内容:

OnClickClient="return confirm('Are you sure you want to delete this record?');";javascript:__doPostBack(&#39;ctl00$Main$PlansGrid&#39;,&#39;Delete$101&#39;)"

会发生什么: 1)用户单击“删除”按钮。 2)确认显示对话框。 3)用户单击“确定”按钮。 4)确认返回True。 5)在语句执行开始时返回,并且不调用__doPostBack。

解决方案是仅在确认返回false时返回:

OnClientClick="if(!confirm('Are you sure you want to delete this Plan?')) return;"

如果用户单击OK,则确认返回True,然后执行__doPostBack。

答案 2 :(得分:1)

简单地调用一个JavaScript函数,返回一个布尔值&#34; return&#34;调用者因此如果返回false,则此链接按钮将不起作用。

ASPX

<asp:LinkButton runat="server" OnClientClick="return DeleteItem();" locationID='<%# DataBinder.Eval (Container.DataItem,"Id").ToString() %>' ID="linkDelete" OnClick="linkDelete_Click" title='<%# "Delete " + DataBinder.Eval (Container.DataItem,"City").ToString() %>' Style="float: left;"><img src="Asset/img/icon/bin.gif" alt="" hspace="3" /></asp:LinkButton>

的Javascript

<script type="text/javascript">
    function DeleteItem() {
        if (confirm("Delete this Location?")) {
            return true;
        } else {
            return false;
        }
    }
</script>

服务器端

protected void linkDelete_Click(object sender, EventArgs e)
    {
       int locationID =0;
       int.TryParse(((LinkButton)sender).Attributes["locationID"].ToString(),out locationID);

       if (locationID > 0)
       {
           Location loc = Location.GetById(locationID);
           CurrentDataContext.CurrentContext.DeleteObject(loc);
           CurrentDataContext.CurrentContext.SaveChanges();
           bindGv();
       }
    }

答案 3 :(得分:0)

另一种方式:

if (e.Row.RowType == DataControlRowType.DataRow)
        {
            // loop all data rows
            foreach (DataControlFieldCell cell in e.Row.Cells)
            {
                // check all cells in one row
                foreach (Control control in cell.Controls)
                {
                    // Must use LinkButton here instead of ImageButton
                    // if you are having Links (not images) as the command button.
                    LinkButton button = control as LinkButton;
                    if (button != null && button.CommandName == "Delete")
                        // Add delete confirmation
                        button.OnClientClick = "return confirm('Are you sure " +
                               "you want to delete this record?');";
                }
            }
        }

答案 4 :(得分:0)

 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        string id = e.Row.Cells[2].Text;
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton db = (LinkButton)e.Row.Cells[6].Controls[0];
            db.OnClientClick = "return confirm('Are you want to delete this Work Description : " + id + "?');";
        }
    }