C# 如何从datalist内的datakey字段中获取值
我有一个数据列表,我设置了DataKeyFieldC# 如何从datalist内的datakey字段中获取值,c#,asp.net,C#,Asp.net,我有一个数据列表,我设置了DataKeyField <asp:DataList ID="lstItems" runat="server" RepeatDirection="Horizontal" DataKeyField="itemid" > <ItemTemplate> //contents in the datalist here </ItemTemplate> </asp:DataList> 我在页面中还有一个ima
<asp:DataList ID="lstItems" runat="server" RepeatDirection="Horizontal" DataKeyField="itemid" >
<ItemTemplate>
//contents in the datalist here
</ItemTemplate>
</asp:DataList>
我在页面中还有一个imagebutton,上面写着imgADD。我的问题是如何在服务器端单击imgADD中获得datakey的值
<asp:DataList ID="DataList_projects" runat="server" RepeatColumns="1" DataKeyNames="ID"
RepeatDirection="Horizontal" onitemcommand="DataList_projects_ItemCommand" >
<HeaderStyle ForeColor="Red" />
<ItemTemplate>
<hr style="color:" />
<asp:Label ID="ltlproject" ForeColor="#cc0000" runat="server" text="PROJECT:"></asp:Label> <b><u><asp:Label ID="lblProject" runat="server" Text='<%# Eval("TITLE")%>' ></asp:Label></u></b> <asp:LinkButton ID="lnkEdit" ForeColor="Red" CommandName="Edit" runat="server">Edit</asp:LinkButton><br />
</ItemTemplate>
</asp:DataList>
我想这会帮助你
我认为这将帮助您……ASPX代码:
}ASPX代码:
}客户端单击或服务器端单击?该按钮是在数据列表的外部还是内部?客户端单击或服务器端单击?该按钮是在数据列表的外部还是内部?
protected void DataList_projects_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "Edit")
{
string strId = DataList_projects.DataKeys[e.Item.ItemIndex].ToString();
}
}
//In the DataList ItemTemplate:
..
..
<asp:ImageButton ID="imgADD" runat="server" ImageUrl="uri" ToolTip="Add"
CommandName="Add" />
protected void DataList1_ItemCommand(object source,
DataListCommandEventArgs e)
{
if(e.CommandName == "Add")
{
string keyID;
// Get the index of the row from which this event was raised.
int idx = e.Item.ItemIndex;
// Get the DataKey with the same index.
object thisKey = DataList1.DataKeys(idx);
if(thisKey != null)
{
keyID = thisKey.ToString();
// do something here
}
}