首页 > 解决方案 > 从 ListView 中的复选框更新 SQL

问题描述

目标:单击 ListView 中的复选框时更新 SQL 表中的行。我想从 sql 更新以下列: IsApproved ApprovalType

我能够在从页面上的 Listview 中单击的行中找到复选框。然而,这就是我所得到的。下面的当前代码,以及我尝试过的代码和随后的错误。


代码

aspx

<asp:ListView ID="lstUsers" runat="server" ItemType="Program.Data.Table" DataKeyNames="ID" SelectMethod="lstUsers_GetData"
    OnItemCommand="lstUsers_ItemCommand" DeleteMethod="lstUsers_DeleteItem" OnSorting="lstUsers_Sorting"
    OnItemDataBound="lstUsers_ItemDataBound"
    ItemPlaceholderID="litPlaceHolder" >

<LayoutTemplate>
        <div class="table-responsive">
            <table class="table">
                <thead>
                    <tr>                            
                        <th>Last Name</th>
                        <th>First Name</th>
                        <th>UserName</th>
                       <th>Approve User</th>

            <td><%# Item.LastName %></td>
            <td><%# Item.FirstName %></td>
            <td><%# Item.UserName %></td>
            <td> <asp:CheckBox ID="cbApproved" runat="server" AutoPostBack="true" OnCheckedChanged="Approve_Click" /> </td>

时间

protected void Approve_Click(object sender, EventArgs e)
    {
        foreach (ListViewItem item in lstUsers.Items)
        {
            if ((item.FindControl("cbApproved") as CheckBox).Checked == true)
            {
                System.Diagnostics.Debug.WriteLine("it was checked!");

我试过了:

我希望在 SQL 中更新的列值是否必须显示在 ASPX 页面上?

我看了看:

https://msdn.microsoft.com/en-us/library/bb398790.aspx#ModifyingDataUsingTheListViewControl

Asp.Net CheckBoxList 更新一个 SqlDataSource

标签: c#asp.netsql-serverlistviewcheckbox

解决方案


为什么不简单地将发件人转换为 CheckBox?

protected void Approve_Click(object sender, EventArgs e)
{
    if ((sender as CheckBox).Checked == true)
    {
        System.Diagnostics.Debug.WriteLine("it was checked!");
    }
}

您必须将数据绑定到IsPostBack检查中的 ListView 才能正常工作。

更新

使用 DataKeyNames 可以轻松完成您想要的操作。将其作为属性添加到 ListView。

<asp:ListView ID="ListView1" runat="server" DataKeyNames="IDColumn">

然后当复选框被更改时,读取正确的键

protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack == false)
    {
        ListView1.DataSource = Common.fillBooks();
        ListView1.DataBind();
    }
}

protected void Approve_Click(object sender, EventArgs e)
{
    //cast the sender back to the checkbox
    CheckBox cb = sender as CheckBox;

    if (cb.Checked)
    {
        //get the parent of the parent (itemtemplate > listiew)
        ListView lv = cb.Parent.Parent as ListView;

        //get the dataitem from the namingcontainer
        ListViewDataItem item = cb.NamingContainer as ListViewDataItem;

        //get the correct datakey with the index of the dataitem
        int ID = Convert.ToInt32(lv.DataKeys[item.DataItemIndex].Values[0]);

        //update database here with the ID
    }
}

列表显示

<asp:ListView ID="ListView1" runat="server" DataKeyNames="id" ItemType="Namespace.Class">
    <ItemTemplate>
        <div class="table-responsive">
            <table class="table">
                <tr>
                    <td>
                        <asp:CheckBox ID="cbApproved" runat="server" AutoPostBack="true" OnCheckedChanged="Approve_Click" />
                    </td>
                </tr>
            </table>
        </div>
    </ItemTemplate>
</asp:ListView>

推荐阅读