前台<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> 
后台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 
        {
            
        }
    }求大神解答

解决方案 »

  1.   

    你看看 CommandArgument 值是什么,估计不能专为int
      

  2.   

    <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>  />
      

  3.   


    <asp:ButtonField CommandName="Edit" HeaderText="查看" Text="查看" 
                    ItemStyle-Width="50px" >
    <ItemStyle Width="50px"></ItemStyle>
                </asp:ButtonField>
    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 == "Edit")
                {
                    Response.Redirect("message.aspx?id=" + id + "");
                }
            }
            catch 
            {
              
            }
        }这样是没有问题的,是因为ButtonField?
      

  4.   

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

  5.   


    CommandArgument是传的值,你这样:
    前台<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> 后台protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            //int index = int.Parse(e.CommandArgument.ToString());//调试,到这里出错,输入字符串的格式不正确
            int id = int.Parse(e.CommandArgument.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 
            {
                
            }
        }Button有CommandArgument这个属性,LinkButton不知道有没有,没有的话你换成Button就行了