[asp.net]删除在 gridview 不工作

发布时间: 2017/3/26 2:30:08
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我设计为 admin 的网页搜索用户从数据库表使用 Get 按钮和删除 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 按钮编码

问题是当我点击的删除它不工作 !请帮助...

解决方法 1:

它看起来像这里的问题是,你要重写的数据源在你后面的代码中。如果你注释掉的代码在"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)
        {


        }

这可能不是你虽然想要什么。这将加载在页加载数据,所以用户不需要单击按钮,得到的结果。

如果您希望用户单击的按钮然后你需要 rewirte 您的代码有点并将一些代码放在 RowDeleting(),作为 @Cal279 在他的评论中提出。

官方微信
官方QQ群
31647020