[英]How to update selected rows in ListView
我有asp.net Web形式的listview。 我想選擇行並單擊按鈕后更新選擇的內容。
為此,我想使用Checkbox / CheckboxList。 但是我不明白如何將有關行或從選定行中的列的信息發送到Checkbox / CheckboxList項目。
如何使用Checkbox / CheckboxList選擇行並更新它們?
我使用Asp.net Linq實體框架。
我的密碼
<asp:Button ID="ButtonTest" runat ="server" OnClick="ButtonTest_Click" /> <asp:ListView ID="ListView2" ItemType="DocCat.Models.ReqInf" SelectMethod="GetReqF" OnItemDataBound="ListView2_ItemDataBound" DataKeyNames="requestN" EnableViewState="true" runat="server" UpdateMethod="ListView2_UpdateItem" DeleteMethod="ListView2_DeleteItem" InsertMethod="ListView2_InsertItem"> <LayoutTemplate> <div class="outerContainer" style="overflow: scroll"> <table id="docTable"> <thead> <tr> <th> Выбрать </th> <th>First</th> <th>Request</th> <th>Third</th> <th>Four</th> </tr> </thead> <tbody runat="server" id="itemPlaceholder"></tbody> </table> </div> </LayoutTemplate> <ItemTemplate> <tr> <td> <asp:CheckBoxList runat="server" ID="CheckNew" ><asp:ListItem>Выбрать</asp:ListItem></asp:CheckBoxList></td> <td> </td> <td><%# Item.BirthDate.Date%></td> <td><%# Item.F1 %></td> <td><%# Item.F2 %></td> <td><%# Item.F3 %></td> </tr> </ItemTemplate> </asp:ListView>
選定的行不會顯示在Checkboxlist項目和字符串selectedItems中:
CheckBoxList cblRoles = ListView2.Items[0].FindControl("CheckNew") as CheckBoxList; string selectedItems = ""; for (int i = 0; i < cblRoles.Items.Count; i++) { if (cblRoles.Items[i].Selected) { selectedItems = selectedItems + cblRoles.Items[i].Value + ","; } }
我最近使用了這種UI。 首先創建Table UI,然后在后面的代碼中創建填充Table方法。我使用ADO.net進行數據訪問。
注意 :單擊按鈕后,創建用於獲取數據和更新數據的存儲過程。
步驟1:在該復選框的創建對象中編寫“填充表”方法,但我使用了單選按鈕。
using (mTableRow = new HtmlTableRow()){
{
#region Radio Button
using (HtmlTableCell lTableCell = new HtmlTableCell())
{
RadioButton mradioButton = new RadioButton();
mradioButton.ID = "Radio" + listInfo.ID;
mradioButton.GroupName = "rowSelector1";
mradioButton.AutoPostBack = true;
mradioButton.Checked = false;
mradioButton.CheckedChanged += new EventHandler(AvailableRadioButton_CheckedChanged);
lTableCell.Attributes["class"] = "RadioButton";
lTableCell.Controls.Add(mradioButton);
mTableRow.Cells.Add(lTableCell);
#endregion
// add all the remaining columns
// add table row to the table.
步驟2:為Checkbox事件單擊創建方法。
我的問題是找到復選框,我只是想念這個:
foreach (ListViewDataItem item in this.ListView2.Items)
{
if (item.ItemType == ListViewItemType.DataItem)
{
並且一切正常。
我的button_click方法:
List<int> ls = new List<int>();
{
foreach (ListViewDataItem item in this.ListView2.Items)
{
if (item.ItemType == ListViewItemType.DataItem)
{
CheckBox chkRow = item.FindControl("CheckBox") as CheckBox;
if (chkRow.Checked)
{
int request = int.Parse((item.FindControl("FirstFind") as Label).Text.Trim());
ls.Add(request);
}
}
}
repository.Approved(ls, newstat);
更新方式
public void Approved(List<int> list,int stat )
{
var friends = context.Requery.Where(f => list.Contains(f.parametr)).ToList();
friends.ForEach(a =>
{
a.par1 = 0;
a.par2 = stat;
});
context.SaveChanges();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.