2015-10-08 35 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