日期:2014-05-17  浏览次数:20382 次

为什么GridView中的按钮不能响应RowCommand事件
我在GridView的 RowDataBound事件中添加一个客户端事件
C# code
    protected void gvZhidian_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Button b = (Button)e.Row.FindControl("btnDelete");
            b.Attributes.Add("onclick", "return confirm('你确定要删除此字典吗,如果删除可能导致一些内容显示不了');");
        }
    }

主要是为了在删除的时候有一个提示,但是我点击确定和取消都不能响应RowCommand事件,如下
C# code
protected void gvZhidian_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.ToLower() == "del")
        {
            new jiaxExam().executeSQL("delete from Tzhidian where id='" + ((Button)sender).CommandArgument + "' and upid='" + Request.QueryString["id"].ToString() + "'");
            Response.Redirect("zhidian1.aspx?id=" + Request.QueryString["id"].ToString());
        }
    }

请教一下,这是怎么回事啊

------解决方案--------------------
<asp:GridView ID="GridView1" runat="server" OnRowCommand="gvZhidian_RowCommand">
</asp:GridView>
------解决方案--------------------
绑定的事件中看不出有哪儿错哦!!
------解决方案--------------------
直接这样提示:
<asp:Button id="btnDelete" runat="server" OnClientClick="return confirm('你确定要删除此字典吗,如果删除可能导致一些内容显示不了');"/>
------解决方案--------------------
首先将按钮那一列转化为模版,然后在其属性里加入下面的js语句,就能弹出一个确认窗口。
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Select"
Text="选择" >
</asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Delete" Text="删除" 
OnClientClick="return confirm( Are you sure you want to delete this record? )" >
</asp:LinkButton>
</ItemTemplate>

------解决方案--------------------
要在cs里面写的话,在GridView1_RowDataBound里写
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
ImageButton imgFlag = new ImageButton();
imgFlag = ((ImageButton)e.Row.Cells[7].Controls[2]);
if (imgFlag.AlternateText == "删除")
{
imgFlag.Attributes.Add("onclick", "javascript:return confirm( 您确信要删除吗!? )");
}

}

}
------解决方案--------------------
探讨
直接这样提示:
<asp:Button id="btnDelete" runat="server" OnClientClick="return confirm('你确定要删除此字典吗,如果删除可能导致一些内容显示不了');"/>

------解决方案--------------------
从你这句上来看 if (e.CommandName.ToLower() == "del")应该是参数错误了

<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete" Text="删除"></asp:LinkButton>

你那里一定是del,所以触发不了
------解决方案--------------------