Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从datalist内的datakey字段中获取值_C#_Asp.net - Fatal编程技术网

C# 如何从datalist内的datakey字段中获取值

C# 如何从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

我有一个数据列表,我设置了DataKeyField

 <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>  &nbsp;<b><u><asp:Label ID="lblProject" runat="server" Text='<%# Eval("TITLE")%>' ></asp:Label></u></b> &nbsp;  <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 
    } 
 }