2012-09-13 70 views
0

我可以使用LinkButton來知道如何從GridView中刪除行嗎?我在谷歌找到的代碼使用數據綁定GridView。我根據使用DropDownList選擇的信息來綁定信息。由於從數據庫C中刪除選定的數據#

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    string username; 
    username = HttpContext.Current.User.Identity.Name; 
    if (DropDownList1.SelectedValue.Equals("Expired")) 
    { 
     SqlConnection conn4 = new SqlConnection(My connection); 
     SqlDataAdapter adapter; 
     string mySQL2; 
     mySQL2 = 
      "SELECT Title,MessageStatus From Table_Message WHERE Username ='" 
      + username 
      + "' AND MessageStatus = 'Expired' AND Method = 'Email'"; 
     adapter = new SqlDataAdapter(mySQL2, conn4); 
     conn4.Open(); 

     DataSet ds3 = new DataSet(); 
     adapter.Fill(ds3); 
     //Execute the sql command 
     GridView1.DataSource = ds3; 
     GridView1.DataBind(); 
     conn4.Close(); 

    } 
    else if (DropDownList1.SelectedValue.Equals("Pending")) 
    { 
     SqlConnection conn3 = new SqlConnection(My connection); 
     SqlDataAdapter adapter1; 
     string mySQL; 
     mySQL = 
      "SELECT Title,MessageStatus From Table_Message WHERE Username ='" 
      + username 
      + "' AND MessageStatus = 'Pending' AND Method = 'Email'"; 
     adapter1 = new SqlDataAdapter(mySQL, conn3); 
     conn3.Open(); 

     DataSet ds2 = new DataSet(); 
     adapter1.Fill(ds2); 
     //Execute the sql command 
     GridView1.DataSource = ds2; 
     GridView1.DataBind(); 
     conn3.Close(); 
    } 

回答

0

如果允許匿名用戶訪問列表:

這可能是一個選項:

  1. 創建delete.aspx
  2. 在您的查詢不獲取主鍵列(Ex:Id,UId ...)
  3. 在網格視圖中使DataKeyNames =「Id」
  4. 在linkbutton`s on點擊事件,將用戶重定向到delete.aspx?Id ='您的數據ID'
  5. delete.aspx只能由授權用戶訪問。節省意外的數據丟失。
  6. 在delete.aspx中添加一個刪除按鈕,並且它的onclick事件使用該唯一標識刪除該記錄。
  7. 這可能是一種安全的方式。

如果要顯示房源只有授權的用戶,那麼你可能會編寫刪除代碼使用Ajax:)

  • 的OnClientClick(:編寫發送請求的JavaScript函數的:delete.aspx?Id='Id',刪除記錄出現。
0

您可以採取以下步驟

)網友電網的DataKeyNames到表

2的主鍵)上的LinkBut​​ton有作爲

<asp:TemplateField HeaderText="Action"> 
         <ItemTemplate> 
<asp:ImageButton ID="imgBtnDelete" runat="server" ImageUrl="/_layouts/images/DELETE.GIF" 
          AlternateText="Delete" CommandName="DeleteUser" CausesValidation="false" ToolTip="Delete"/> 
         </ItemTemplate> 

3)綁定rowdatabound事件中的命令參數

protected void GvwUser_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 

     ImageButton imgBtnDelete; 


     if (e.Row.RowType == DataControlRowType.DataRow) 
     {    
      imgBtnDelete = (ImageButton)e.Row.FindControl("imgBtnDelete"); 
      imgBtnDelete.CommandArgument = gvwUser.DataKeys[e.Row.RowIndex].Value.ToString(); 

     } 
    } 

4)在後面的代碼編寫的執行作爲

protected void GvwUser_RowCommand(object sender, GridViewCommandEventArgs e) 
    { 
int userId = 0; 
if (e.CommandName.Equals("DeleteUser")) 
     { 
      //get the user id 
      userId = Convert.ToInt32(e.CommandArgument.ToString()); 

      //GetUser will delete the user 
      if (DeleteUser(userId) > 0) 
      { 
      Page.ClientScript.RegisterStartupScript(this.GetType(), "Delete", "alert('User Deleted.');", true); 
      } 
}