开发者

get datakey value and label text from Datalist on click of button?

how to get the value of Datakey and lable which is present inside datalist?

i have a image button and开发者_StackOverflow linkbutton on click of them i am trying to get but i am unable to do. i don't want to use Item_Command event.

How to do?

Thanks.


If you want to Get the Data Key value on the Click of the ImageButton/LinkButton, you can set the DataKey value to the CommandArgument property of your control and then in the Click handler you can get the value from the CommandArgument property.

<asp:ImageButton ID="ibtn" runat="server" CommandArgument='<%# Eval("DataKeyName")%>'
 ImageUrl="~/Images/edit.png"></asp:ImageButton>

Same case for linkbutton as well.


Inside the button click event, you can do the following to get the DataKey and the Label (assuming it's in the same control collection as the button):

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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜