简体   繁体   English

如何使用此表中的值从数据库中删除行?

[英]How can I use the value in this table to delete the row from the database?

So I'm learning ASP.NET and I've hit a roadblock in trying to create a webpage that connects to an sqlite database and allows for that database to be modified from the webpage. 因此,我正在学习ASP.NET,在尝试创建一个连接到sqlite数据库并允许从该网页修改该数据库的网页时遇到了障碍。 Currently the FindControl is returning NULL and I'm trying to be able to delete the row with delete button in the same row. 当前,FindControl返回NULL,并且我试图使用同一行中的Delete按钮删除该行。 Also the table is going to be changing sizes so I'm not sure how to make it so that the button and row are always corresponding. 另外,表格将要改变大小,所以我不确定如何制作表格,以使按钮和行始终保持对应。

Also when I look at the page in developer tools and at the table the id for the td tag for each element in the first column is in a format of id ="Repeater1_id_0" then id ="Repeater1_id_1" etc. I was testing to see if I could make the button work for the third row, but I still got null. 另外,当我在开发人员工具中的页面和表格中查看第一列中每个元素的td标签的id时,其格式分别为id =“ Repeater1_id_0”,然后id =“ Repeater1_id_1”等。我正在测试以查看如果我可以使按钮在第三行工作,但仍然为空。

Here is the code for the table from the database on the webpage in a .aspx file. 这是.aspx文件中网页数据库中表的代码。

<asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand">

            <HeaderTemplate>

                <table border="1">
                    <tr>
                        <td><b>ID</b></td>
                        <td><b>Player</b></td>
                        <td><b>Points</b></td>
                        <td><b>Steals</b></td>
                        <td><b>Blocks</b></td>
                        <td><b>Assists</b></td>
                        <td><b>MPG</b></td>
                        <td><b>Shot %</b></td>
                        <td><b>3 Point %</b></td>
                    </tr>
            </HeaderTemplate>

            <ItemTemplate>

                <tr>
                    <td runat="server"  id="id"><%# DataBinder.Eval(Container.DataItem, "id") %> </td>
                    <td><%# DataBinder.Eval(Container.DataItem, "playername") %> </td>
                    <td><%# DataBinder.Eval(Container.DataItem, "points") %> </td>
                    <td><%# DataBinder.Eval(Container.DataItem, "steals") %> </td>
                    <td><%# DataBinder.Eval(Container.DataItem, "blocks") %> </td>
                    <td><%# DataBinder.Eval(Container.DataItem, "assists") %> </td>
                    <td><%# DataBinder.Eval(Container.DataItem, "MPG") %> </td>
                    <td><%# DataBinder.Eval(Container.DataItem, "shootingpercentage") %> </td>
                    <td><%# DataBinder.Eval(Container.DataItem, "threepointpercentage") %> </td>
                    <td><asp:Button ID="btnEdit" runat="server" Text="Edit" /></td>
                    <td><asp:Button ID="btnDelete" runat="server" Text="Delete" OnClick="btnDelete_click" /></td>
                </tr>

            </ItemTemplate>

            <FooterTemplate>


            </FooterTemplate>

        </asp:Repeater>

Now here is the code for what the delete button will do when clicked on in the aspx.cs file. 现在,这里是在aspx.cs文件中单击删除按钮时将执行的操作的代码。 It doesn't work but hopefully it kind of gives an insight into what I'm trying to do. 它不起作用,但希望它能提供有关我正在尝试执行的操作的见解。

protected void btnDelete_click(object sender, EventArgs e)
    {
        NBAPlayerRepository players = new NBAPlayerRepository();
        HtmlGenericControl playerId = (HtmlGenericControl)Page.FindControl("Repeater1_id_2");
        var id =Guid.Parse( playerId.ToString());
        players.DeleteRecord(id);

        DataTable table = players.GetAll();

        this.Repeater1.DataSource = table;
        this.Repeater1.DataBind();

    }

I'm kind of a beginner at this stuff so hopefully I haven't broken it too badly. 我是这个东西的初学者,所以希望我并没有把它弄得太糟。

The correct way of handling button clicks within an ASP.Net Repeater control is to : 在ASP.Net Repeater控件中处理按钮单击的正确方法是:

(1) Set the Button CommandName parameter appropriately, eg in your case "Delete". (1)适当设置按钮CommandName参数,例如在您的情况下为“删除”。

(2) Handle the ItemCommand event on your Repeater control. (2)处理Repeater控件上的ItemCommand事件。 Check the CommandName property on the event argument object. 检查事件参数对象上的CommandName属性。 If this equals "Delete" then you can process the delete, and you can get the data (row) context by looking at the Item property of the event argument object. 如果这等于“删除”,则可以处理删除,并且可以通过查看事件参数对象的Item属性来获取数据(行)上下文。

As already mentioned by others FindControl only works for server controls, and in any event assuming the identifier format of repeater items is not recommended. 正如其他人已经提到的, FindControl仅适用于服务器控件,并且在任何情况下都假定不建议使用重复项的标识符格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM