使用SQL和C#在按钮单击时返回行ID

时间:2013-11-19 15:17:33

标签: c# asp.net sql file-upload delete-row

我正在创建一个允许用户将文件上传到服务器的应用程序。一旦他们上传了网站管理员,就会有一个管理页面,用于查看所有上传的文件,下载文件,删除文件,并将文件“标记”为已读。我有文件上传功能正常工作以及将信息插入SQL数据库(文件名,ID,文件路径,标记和日期)。我还使用“删除”和“读取”按钮在网格视图中正确显示数据。

我的问题是在点击删除按钮时删除一个条目。我怎样才能最好能够提取行的ID以便在SQL中成功创建DELETE语句?

一些代码:

admin.aspx

<asp:GridView runat="server" ID="results" AutoGenerateColumns="false" onrowcommand="results_BtnClick">
    <columns>
        <asp:BoundField DataField="id" HeaderText="ID" />
        <asp:BoundField DataField="date" HeaderText="Date" />
        <asp:BoundField DataField="filename" HeaderText="File Name"/>
        <asp:HyperLinkField DataNavigateUrlFields="filePath" Text="Download" />
        <asp:ButtonField ButtonType="Button" Text="Delete" CommandName="del" />
        <asp:ButtonField ButtonType="Button" Text="Read?" CommandName="Check"  />
        <asp:BoundField DataField="checked" HeaderText="Read Status?" />
    </columns>

    </asp:GridView>

admin.aspx.cs

protected void results_BtnClick(object sender, GridViewCommandEventArgs e)
        {
            if (IsPostBack)
            {

                DbConn dbConn = new DbConn();
                //SQL = "SELECT * FROM databasename";
                //ds = new DataSet();
                //ds = dbConn.createDataSet(SQL);
                //DataTable dt = new DataTable();
                //dt = ds.Tables[0];

                if (e.CommandName == "del")
                {
                    /* 
                     * Do Delete
                     */

                    SqlConnection oConn = new SqlConnection(dbConn.connStr);
                    SQL = "DELETE FROM databasename WHERE id=@id";
                    SqlCommand oCmd = new SqlCommand();

                    oCmd = new SqlCommand(SQL);
                    oCmd.Connection = oConn;
                    oCmd.Parameters.Add(new SqlParameter("@id", System.Data.SqlDbType.Int, 4));
                    oCmd.Parameters["@id"].Value = Convert.ToInt32(e.CommandArgument);




                    oConn.Open();

                    oCmd.ExecuteNonQuery();
                    oConn.Close();
                    sel_All(sender, e);
                }

                else if (e.CommandName == "Check")
                {
                    /* 
                     * Do Check Yes
                     */



                    SqlConnection oConn = new SqlConnection(dbConn.connStr);
                    SQL = "UPDATE databasename SET checked=@check WHERE id=@id";
                    SqlCommand oCmd = new SqlCommand();

                    oCmd = new SqlCommand(SQL);
                    oCmd.Connection = oConn;
                    oCmd.Parameters.Add(new SqlParameter("@id", System.Data.SqlDbType.Int, 4));
                    oCmd.Parameters.Add(new SqlParameter("@check", System.Data.SqlDbType.VarChar, 50));
                    oCmd.Parameters["@id"].Value = Convert.ToInt32(e.CommandArgument) + 1;
                    oCmd.Parameters["@check"].Value = "Yes";



                    oConn.Open();

                    oCmd.ExecuteNonQuery();
                    oConn.Close();
                    sel_All(sender, e);



                }
            }

我一直在寻找如何做到这一点的想法,但我的googlefu相当薄弱。任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:1)

我的建议是将ID列设置为TemplateField,其中包含一个ASP.NET HiddenField控件以保存ID值,然后在行命令事件处理程序中找到HiddenField控件并将ID值拉出来,如下所示:

标记:

<asp:GridView runat="server" ID="results" AutoGenerateColumns="false" 
              onrowcommand="results_BtnClick">
    <columns>
        <asp:TemplateField HeaderText="ID">
            <asp:HiddenField id="HiddenFieldId" runat="server" 
                             Value='<%# Eval("id") %>' />
        </asp:TemplateField>
        <asp:BoundField DataField="date" HeaderText="Date" />
        <asp:BoundField DataField="filename" HeaderText="File Name"/>
        <asp:HyperLinkField DataNavigateUrlFields="filePath" Text="Download" />
        <asp:ButtonField ButtonType="Button" Text="Delete" CommandName="del" />
        <asp:ButtonField ButtonType="Button" Text="Read?" CommandName="Check"  />
        <asp:BoundField DataField="checked" HeaderText="Read Status?" />
    </columns>
</asp:GridView>

代码隐藏:

protected void results_BtnClick(object sender, GridViewCommandEventArgs e)
{                
    if (e.CommandName == "del")
    {
        // Get the hidden field control from the current row
        var theHiddenField = e.Row.FindControl("HiddenFieldId") as HiddenField;

        // The as operator will return null for an unsuccessful cast
        // so check if we have something before we try to use it
        if(theHiddenField != null)
        {
            // Get the ID value
            var theId = Convert.ToInt32(theHiddenField.Value);

            // Do delete with ID value
            ...
        }
    }
}

答案 1 :(得分:0)

试试这个:

int id=Convert.ToInt32( GridView1.SelectedRow.Cells[0].Text);

答案 2 :(得分:0)

我的解决方案:

protected void results_BtnClick(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "del")
                {

                    int id = Convert.ToInt32(e.CommandArgument);
                    int primaryID = Convert.ToInt32(results.DataKeys[id].Value);

                ....Do Delete....
                }
        }

我在尝试使用Rows RowIndex之类的时候遇到了很多问题。上面的方法适用于我,并解决了丢失程序集的问题。

希望这可以帮助有类似问题的人!