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

GridView 删除
前台
C# code

<asp:TemplateField ShowHeader="False" HeaderText="删除">
                            <ItemTemplate>
                                <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" 
                                    CommandName="del" OnClientClick="javascript:return confirm('确定删除吗?')"
                                    Text="删除"></asp:LinkButton>
                            </ItemTemplate>
                        </asp:TemplateField> 



后台
C# code

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        int index = int.Parse(e.CommandArgument.ToString());//调试,到这里出错,输入字符串的格式不正确
        int id = int.Parse(this.GridView1.DataKeys[index].Value.ToString());
        try
        {
            if (e.CommandName == "del")
            {
                OracleConnection conn = new OracleConnection(ConnectionString);
                string str = "delete from message where id = " + id + "";
                OracleCommand com = new OracleCommand(str, conn);
                conn.Open();
                com.ExecuteNonQuery();
                conn.Close();
            }
        }
        catch 
        {
            
        }
    }


求大神解答

------解决方案--------------------
<asp:TemplateField ShowHeader="False" HeaderText="删除">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" 
CommandName="del" 
CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"OnClientClick="javascript:return confirm('确定删除吗?')"
Text="删除"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField> 

 />

------解决方案--------------------
你的CommandArgument没有值..
2楼是正解,先绑定上值再取
------解决方案--------------------

CommandArgument是传的值,你这样:
前台
C# code

<asp:TemplateField ShowHeader="False" HeaderText="删除">
                            <ItemTemplate>
                                <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" 
                                    CommandName="del" CommandArgument='"<%Eval("ID")%>"' OnClientClick="javascript:return confirm('确定删除吗?')"
                                    Text="删除"></asp:LinkButton>
                            </ItemTemplate>
                        </asp:TemplateField>