c# - 从 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!");
我试过了:
- item.ColumnName - 但得到错误ListView 不包含 '' 的定义
- item.DataItem - 输出显示整体看起来像一个对象 (System.Web.UI.WebControls.ListViewDataItem)
- item.DataItem.ColumnName - 但得到错误对象不包含''的定义
我希望在 SQL 中更新的列值是否必须显示在 ASPX 页面上?
我看了看:
https://msdn.microsoft.com/en-us/library/bb398790.aspx#ModifyingDataUsingTheListViewControl
解决方案
为什么不简单地将发件人转换为 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>
推荐阅读
- python - 在嵌入式 Flinkrunner (apache_beam [GCP]) 中使用 pub/sub io 运行光束流管道 (Python) 时出错
- django - 自定义 Django 管理员搜索
- jquery - 为什么这个函数不会将页面加载到 div 中?(jQuery AJAX)
- c# - Visual Studio 2019 中的 SQL Server 数据库
- c++ - 如何在 vs 代码“C++”中安装和运行 boost 库
- c++ - U 没有命名类型 - 模板编译错误
- python-3.x - 更改时间列的数据类型-Pandas
- r - “上面列出了初始化中遇到的错误”(R)
- javascript - react-js中如何从组件中取出props数据?
- python - Scipy 和 Sklearn Yeo-Johnson 归一化结果不匹配