[英]how to get value from a datakey field inside datalist
我有一個數據列表,並設置了DataKeyField
<asp:DataList ID="lstItems" runat="server" RepeatDirection="Horizontal" DataKeyField="itemid" >
<ItemTemplate>
//contents in the datalist here
</ItemTemplate>
</asp:DataList>
我在頁面中也有一個圖像按鈕說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>
在你的代碼后面
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
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.