我有一个显示文章评论的ListView。 每个评论都有一个按钮,单击该按钮可删除该评论,前提是已记录的人员是评论的所有者或管理员。ListView:单击按钮时它的所有者删除一行
我需要一种在ListView中以某种方式存储评论ID的方法,然后我需要检查评论作者的ID是否与记录用户的ID相同(存储在Session中),然后,如果它们匹配,则显示点击时删除评论的按钮。
请帮助。
我有一个显示文章评论的ListView。 每个评论都有一个按钮,单击该按钮可删除该评论,前提是已记录的人员是评论的所有者或管理员。ListView:单击按钮时它的所有者删除一行
我需要一种在ListView中以某种方式存储评论ID的方法,然后我需要检查评论作者的ID是否与记录用户的ID相同(存储在Session中),然后,如果它们匹配,则显示点击时删除评论的按钮。
请帮助。
指定的DataKeyNames上ListView的标记属性,然后拿到DataKey在按钮单击事件
<asp:ListView runat="server" ID="myListView" DataKeyNames="CommentId" ...
,并在按钮点击事件,你可以得到的ID
(int)myListView.DataKeys[itemIndex]["CommentId"]
或得到commentId在第四ListItem
int commentId = (int)myListView.DataKyes[3]["CommentId"];
编辑,因为你mentinti您的要求,进一步
您可以访问当前项目是结合列出ItemDataBound事件视图
只需添加上的ListView标记ItemDataBound事件属性,事件
<asp:ListView onitemdatabound="myListView_ItemDataBound" runat="server" ID="myListView" ...
protected void myListView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
ListViewDataItem dataItem = (ListViewDataItem)e.Item;
int commentId = (int)DataBinder.Eval(dataItem, "CommentId");
// get author id based on comment id
// or if you have auther id within the datasource
// by which you are binding the listview then
int ID_Author = (int)DataBinder.Eval(dataItem, "ID_Author");
// get a reference to the delete button in the item
// for instance you may do by this
Control delete_button = e.Item.FindControl("deleteButtonId");
// will hide if the author id don't match with the session id
delete_button.Visible = ID_Author.Equals((int)Session["loggedin_userId"]);
}
}
内做你的逻辑
在ListView的ItemDataBound事件中,您可以获取绑定到ListView的数据项。这个对象将拥有你需要的所有ID。
要处理click事件,您需要ItemCommand事件。文章的ID可以通过删除按钮作为命令参数传递。
希望它有帮助。
谢谢,以便从当前行中提取评论的ID。现在我需要以某种方式检查评论的属性ID_Author是否等于记录用户的ID(存储在会话中),然后显示或不显示按钮。有任何想法吗? – barjed
@barjed,你可以在ListView的ItemDataBound事件中做这种类型的逻辑;因为我为你添加了示例代码。 –
我做到了,非常感谢! – barjed