问题描述
|
如何获取存在于数据列表中的数据键和标签的值?
我有一个图像按钮和链接按钮,单击它们我想获取,但我做不到。
我不想使用Item_Command事件。
怎么做?
谢谢。
解决方法
在按钮单击事件内部,您可以执行以下操作以获取DataKey和Label(假设它与按钮位于同一控件集合中):
var button = sender as Button;
if (button == null) return;
var dataListItem = button.NamingContainer as DataListItem;
if (dataListItem == null) return;
var currentKey = DataList1.DataKeys[dataListItem.ItemIndex];
var myLabel = button.Parent.Controls.Cast<Control>().FirstOrDefault(x => x.ID == \"testLabel\") as Label;
if (myLabel == null) return;
var myLabelText = myLabel.Text;
,如果要在ImageButton/LinkButton
的Click上获取Data Key
值,可以将DataKey值设置为控件的CommandArgument
属性,然后在Click处理程序中从from3ѭ属性获取值。
<asp:ImageButton ID=\"ibtn\" runat=\"server\" CommandArgument=\'<%# Eval(\"DataKeyName\")%>\'
ImageUrl=\"~/Images/edit.png\"></asp:ImageButton>
同样适用于linkbutton。