【问题标题】:Return row ID on button click using SQL and C#使用 SQL 和 C# 在单击按钮时返回行 ID
【发布时间】:2013-12-03 05:24:45
【问题描述】:

我正在创建一个允许用户将文件上传到服务器的应用程序。一旦他们上传了站点管理员,就会有一个管理页面,用于查看所有上传的文件、下载文件、删除文件并将文件“标记”为已读。我的文件上传功能正常工作,并将信息插入 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 很弱。任何帮助将不胜感激。

【问题讨论】:

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


    【解决方案1】:

    试试这个:

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

    【讨论】:

      【解决方案2】:

      我的建议是使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
                  ...
              }
          }
      }
      

      【讨论】:

      • 我在让隐藏字段真正起作用时遇到了问题。我无法编译。
      【解决方案3】:

      我的解决方案:

      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 等时遇到了很多问题。上面的方法对我有用,并解决了缺少程序集的问题。

      希望这对有类似问题的人有所帮助!

      【讨论】:

        猜你喜欢
        • 2023-03-18
        • 2020-04-25
        • 2011-12-08
        • 1970-01-01
        • 1970-01-01
        • 2019-10-09
        • 2020-01-20
        • 2021-05-22
        • 1970-01-01
        相关资源
        最近更新 更多