datalist中获取值的方法
aspx文件:
<asp:DataList ID= "dlTopSpeak " runat= "server " OnItemCommand= "dlTopSpeak_ItemCommand ">
<ItemTemplate>
<table border= "0 " cellpadding= "0 " cellspacing= "0 " width= "190px ">
<tr>
<td>
<asp:LinkButton ID= "lbtntitle " runat= "server " CommandName= "title " CommandArgument= ' <%#Eval( "SSpeakID ") %> '> <%#Eval( "speaktitle ") %> </asp:LinkButton>
<asp:Label ID= "labUserID " runat= "server " Visible= "false "> <%#Eval( "SUserID ") %> </asp:Label>
</td>
<td>
<asp:LinkButton ID= "lbtnUsername " runat= "server " CommandName= "name " CommandArgument= ' <%#Eval( "SUserID ") %> '> <%#Eval( "username ") %> </asp:LinkButton>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
cs文件:
protected void dlTopSpeak_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "title ")
{
//string ID = ((Label)e.Item.FindControl( "labUserID ")).Text;
Response.Redirect( "speak.aspx?speakID= " + e.CommandArgument.ToString() + "&userID= " + ID);
}
else if (e.CommandName == "name ")
{
Response.Redirect( "BlogList.aspx?ID= " + e.CommandArgument.ToString());
}
}
我想在点击lbtntitle的时候链接 "&userID= " + ID中的ID值是从labUserID得到的,或者是从lbtnUsername的e.CommandArgument.ToString()中得到,请问有什么方法能够实现?
谢谢!
[解决办法]
DataKeyField设置为你需要的字段
然后取值:
DataList1.DataKeys[e.Item.ItemIndex].ToString();
[解决办法]
if (e.CommandName == "title ")
{
Label labUserId = e.Item.FindControl( "labUserID ") as Label;
if(labUserId!=null)
{
string ID=labUserId.Text;
Response.Redirect( "speak.aspx?speakID= " + e.CommandArgument.ToString() + "&userID= " + ID);
}
}