Listview Itemediting 不起作用
Posted
技术标签:
【中文标题】Listview Itemediting 不起作用【英文标题】:Listview Itemediting was not working 【发布时间】:2014-04-03 22:41:53 【问题描述】:我不知道为什么会出现这个错误,但我认为我的代码是正确的。
<asp:ListView ID="lvwData" runat="server" OnItemCommand="Student_OnItemCommand" EnableViewState="true"
DataKeyNames="id">
<LayoutTemplate>
<table class="table-list table-list-gridded" id="dataTable" border="0" cellspacing="0"
cellpadding="5">
<thead>
<th class="id">
ROW #
</th>
<th>
Student ID
</th>
<th>
First Name
</th>
<th>
Last Name
</th>
<th>
Course
</th>
<th class="action">
<span>ACTIONS</span>
</th>
</thead>
<tbody id="itemPlaceholder" runat="server">
</tbody>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td align="center" class="id">
<%# Container.DataItemIndex + 1 %>
</td>
<td>
<asp:Label ID="lblStudentID" runat="server" Text='<%# Eval("student_id") %>' />
</td>
<td>
<asp:Label ID="lblFirstName" runat="server" Text='<%# Eval("firstname") %>' />
</td>
<td>
<asp:Label ID="lblLastName" runat="server" Text='<%# Eval("lastname") %>' />
</td>
<td>
<asp:Label ID="lblCourse" runat="server" Text='<%# Eval("course") %>' />
</td>
<td align="center" class="action">
<asp:LinkButton ID="lnkEdit" class="btn btn-small" Text="Edit" CommandName="Edit"
CommandArgument='<%# Eval("ID") %>' runat="server"><i class="icon-pencil"></i></asp:LinkButton>
<asp:LinkButton ID="DeleteLinkButton" Text="Delete" CommandName="Delete" CommandArgument='<%# Eval("ID") %>'
OnClientClick="return confirm('Are you sure you want to delete this Employee?');"
runat="server" class="btn btn-small"><i class="icon-remove"></i></asp:LinkButton>
</td>
</ItemTemplate>
<EmptyDataTemplate>
<div class="centered alert alert-error">
No list of records to display.
</div>
</EmptyDataTemplate>
</asp:ListView>
后面的C#代码:
protected void Student_OnItemCommand(object sender, ListViewCommandEventArgs e)
switch (e.CommandName)
case "Delete":
ListViewDataItem dataItem = (ListViewDataItem)e.Item;
string employeeID = lvwData.DataKeys[dataItem.DisplayIndex].Value.ToString();
DBConnect db = new DBConnect();
db.Delete("delete from students where id = '" + employeeID + "'");
Response.Redirect("~/Users/Student.aspx");
break;
case "Edit":
mvStudents.ActiveViewIndex = 1;
break;
//if (String.Equals(e.CommandName, "Delete"))
//
//
//else if (string.Equals(e.CommandName, "Edit"))
//
// ListViewDataItem dataItem = (ListViewDataItem)e.Item;
// string employeeID = lvwData.DataKeys[dataItem.DisplayIndex].Value.ToString();
//
堆栈跟踪:
[InvalidOperationException: ListView 'lvwData' 引发事件 未处理的 ItemEditing。] System.Web.UI.WebControls.ListView.OnItemEditing(ListViewEditEventArgs e) +305573 System.Web.UI.WebControls.ListView.HandleEdit(Int32 项目索引)+51 System.Web.UI.WebControls.ListView.HandleEvent(EventArgs e,布尔值 原因验证,字符串验证组)+324 System.Web.UI.WebControls.ListView.OnBubbleEvent(对象源, EventArgs e) +227 System.Web.UI.Control.RaiseBubbleEvent(对象 源,EventArgs 参数)+37 System.Web.UI.WebControls.ListViewDataItem.OnBubbleEvent(对象 来源,EventArgs e) +112 System.Web.UI.Control.RaiseBubbleEvent(对象源,EventArgs args) +37 System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e)+121 System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(字符串事件参数)+156 System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(字符串 事件参数)+10 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler 源控件,字符串事件参数)+13 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +9528578 System.Web.UI.Page.ProcessRequestMain(布尔includeStagesBeforeAsyncPoint,布尔includeStagesAfterAsyncPoint) +1724
【问题讨论】:
对不起,我把它 lvwData 改成 listview1 我想如果我把 listview1 放上去会更容易理解 请在此处也包含错误堆栈跟踪。 现在包括堆栈跟踪.. 【参考方案1】:我建议您更改命令名称 原因:- 单击按钮的 CommandName 是“Edit”,然后它将 ListView 置于 Edit 模式,尝试将 CommandName=“Edit” 修改为“modify”之类的东西......这很容易
希望你能理解。
【讨论】:
以上是关于Listview Itemediting 不起作用的主要内容,如果未能解决你的问题,请参考以下文章
listView.setOnItemClickListener() 在 Android 中不起作用
当 RadioButton 单击 Listview 时 setOnItemClickListener 不起作用