2015-10-08 32 views
1

我設計一個網頁管理員從數據庫表中使用Get按鈕進行搜索的用戶和GridView控件刪除用戶刪除在GridView控件不工作

這是我的信源編碼..

   <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AutoGenerateDeleteButton="True" DataKeyNames="username" EnableModelValidation="True" OnRowDeleting="GridView1_RowDeleting" OnSelectedIndexChanged="GridView1_SelectedIndexChanged1"> 
        <Columns> 
         <asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" ReadOnly="True" SortExpression="Id" /> 
         <asp:BoundField DataField="name" HeaderText="name" SortExpression="name" /> 
         <asp:BoundField DataField="adm" HeaderText="adm" SortExpression="adm" /> 
         <asp:BoundField DataField="mobno" HeaderText="mobno" SortExpression="mobno" /> 
         <asp:BoundField DataField="branch" HeaderText="branch" SortExpression="branch" /> 
         <asp:BoundField DataField="year" HeaderText="year" SortExpression="year" /> 
         <asp:BoundField DataField="username" HeaderText="username" ReadOnly="True" SortExpression="username" /> 
         <asp:BoundField DataField="password" HeaderText="password" SortExpression="password" /> 
         <asp:BoundField DataField="usertype" HeaderText="usertype" SortExpression="usertype" /> 
        </Columns> 
       </asp:GridView> 
       <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:CanteenConnectionString %>" SelectCommand="SELECT * FROM [Hosteller]" DeleteCommand="DELETE FROM Hosteller WHERE (username = @username)"> 
        <DeleteParameters> 
         <asp:Parameter Name="username" /> 
        </DeleteParameters> 
       </asp:SqlDataSource> 
      </td> 
     </tr> 
     <tr> 
      <td>&nbsp;</td> 
     </tr> 
     <tr> 
      <td>&nbsp;</td> 
     </tr> 
    </table> 

</form> 

protected void btnget_Click(object sender, EventArgs e) 
     { 
      DataTable dt = new DataTable(); 
      string ConString = "Data Source=sheikha-pc\\sqlexpress;Initial Catalog=Canteen;Integrated Security=True"; 
      SqlConnection con = new SqlConnection(ConString); 
      string qry = "select id,name,adm,mobno,branch,year,username,password,usertype from Hosteller where adm='" + txtsearch1.Text + "'"; 
      SqlDataAdapter adpt = new SqlDataAdapter(qry, con); 
      adpt.Fill(dt); 
      if (dt.Rows.Count == 0) 
      { 
       GridView1.EmptyDataText = "No data found"; 
       GridView1.DataBind(); 
      } 
      else 
      { 
       GridView1.DataSource = dt; 
       GridView1.DataBind(); 
      } 

     } 

這是我爲GET編寫的編碼按鈕

問題是當我clic k刪除它不起作用!請幫忙.........

+0

您將需要共享您的代碼GridView1_RowDeleting() - 你是否重新綁定在這個處理程序? – CalC

+0

@ Cal279 no theres nothing in ..實際上當我得到一個錯誤「DataSource和DataSourceID都在'GridView1'上定義。刪除一個定義。」我刪除了datasource = gridview1中定義的sqldatasource1 ...然後他們告訴我rowdeleting沒有定義...所以我試過但沒有工作,,請你幫我 – sheikha

+0

你能告訴我們GridView1_RowDeleting事件嗎? – Sankar

回答

0

看起來這裏的問題是,你正在覆蓋你的代碼背後的數據源。如果您註釋掉「btnget_Click」中的代碼並將數據源添加到GridView1,它應該可以刪除。

  <asp:GridView ID="GridView1" DataSourceID="SqlDataSource1" runat="server" AutoGenerateColumns="False" AutoGenerateDeleteButton="True" DataKeyNames="username" EnableModelValidation="True" OnRowDeleting="GridView1_RowDeleting" OnSelectedIndexChanged="GridView1_SelectedIndexChanged1"> 
       <Columns> 
        <asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" ReadOnly="True" SortExpression="Id" /> 
        <asp:BoundField DataField="name" HeaderText="name" SortExpression="name" /> 
        <asp:BoundField DataField="adm" HeaderText="adm" SortExpression="adm" /> 
        <asp:BoundField DataField="mobno" HeaderText="mobno" SortExpression="mobno" /> 
        <asp:BoundField DataField="branch" HeaderText="branch" SortExpression="branch" /> 
        <asp:BoundField DataField="year" HeaderText="year" SortExpression="year" /> 
        <asp:BoundField DataField="username" HeaderText="username" ReadOnly="True" SortExpression="username" /> 
        <asp:BoundField DataField="password" HeaderText="password" SortExpression="password" /> 
        <asp:BoundField DataField="usertype" HeaderText="usertype" SortExpression="usertype" /> 
       </Columns> 
      </asp:GridView> 

而後面的代碼:

protected void btnget_Click(object sender, EventArgs e) 
     { 


     } 

這可能不是你想要什麼,雖然。這將加載頁面加載數據,所以用戶不需要點擊按鈕來獲得結果。

如果你想讓用戶點擊按鈕,那麼你需要稍微修改一下你的代碼,並在@Cal279中填入RowDeleting()中的代碼。

+0

實際上管理員必須按年份刪除用戶,而且我的數據庫非常龐大。所以獲取按鈕提取他想刪除的用戶。因此獲得按鈕是重要的..是否有任何其他方法? – sheikha

+0

是的,看看這個鏈接和薩爾曼的回答。您可以手動添加按鈕並編寫一些代碼來刪除記錄。 http://forums.asp.net/t/1726832.aspx?adding+buttons+with+each+row+of+the+GridView – Aaron