如何检索同一行中的ListView项的值作为一个LinkButton [英] How to retrieve the value of a ListView item in the same row as a LinkButton
问题描述
我有这显示一组群体,从数据库中检索动态生成的ListView。 ListView控件模板看起来是这样的:
I have a dynamically generated ListView which displays a set of groups, retrieved from a database. The ListView template looks like this:
<asp:ListView ID="lvGroups" runat="server">
<ItemTemplate>
<tr>
<td>
<asp:Label ID="lblGroupName" runat="server" Text='<%# Eval("GroupName") %>' />
</td>
<td>
<asp:LinkButton ID="lnkRemove" runat="server" Text="Remove" OnClick="lnkGroupRemove" OnClientClick="Confirm()" />
</td>
</tr>
</ItemTemplate>
</asp:ListView>
正如你所看到的,有一个价值,它是从数据库中抽取,然后一个LinkButton用于去除值。当点击LinkButton的,它会显示一个JavaScript确认信息,如果您单击是在该消息,那么具体的条目将被从数据库中删除。
As you can see, there is a value, which is pulled from a database, and then a linkbutton for removing that value. When the linkbutton is clicked, it displays a javascript confirmation message, and if you click Yes on that message, then that specific entry will be removed from the database.
不幸的是我不能简单地删除组名,其中ID =行号,因为你既可以添加和删除组,这样就会很快变得不一致。
Unfortunately I cannot simply remove the GroupName where the ID = row number, because you can both add and remove groups, so that will quickly become inconsistent.
我所真正需要的是检索来自同一行被点击,如果我能想出如何做到这一点的话,我可以轻松地配置数据库查询成功删除条目的LinkButton的的组名的方式。如果您有其他解决方案,但是,这也很大。
All I truly need is a way to retrieve the GroupName from the same row as the linkbutton that was clicked, if I can figure out how to do that, then I can easily configure a database query to successfully remove the entry. If you have another solution, however, that is also great.
我很伤心地说,我没有例如code为 lnkGrou premove
事件,因为我根本不知道从哪里开始这一问题。
I'm sad to say that I have no example code for the lnkGroupRemove
event, as I simply have no clue where to start on this problem.
在这个问题上的任何帮助将非常AP preciated!
Any help on this subject would be much appreciated!
推荐答案
您可以使用的DataKeyNames
属性:
<asp:ListView ID="lvGroups" runat="server" DataKeyNames="ID,GroupName" >
和这种方式获得的价值在code-背后:
and get the value this way in code-behind:
protected void lnkGroupRemove(object sender, EventArgs e)
{
ListViewItem item = (sender as LinkButton).NamingContainer as ListViewItem;
int ID = (int)lvGroups.DataKeys[item.DataItemIndex].Values["ID"];
string groupName = (string)lvGroups.DataKeys[item.DataItemIndex].Values["GroupName"];
}
我添加了一个ID值,表明附加字段可以被添加到的DataKeyNames
和code-后面,包括在数据源字段,但检索不显示在ListView。
I added an ID value to show that additional fields can be added to DataKeyNames
and retrieved from code-behind, including fields that are in the data source but not displayed in the ListView.
这篇关于如何检索同一行中的ListView项的值作为一个LinkButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!