数据列表中的链接按钮 [英] link button in a datalist

查看:62
本文介绍了数据列表中的链接按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在数据列表中的链接按钮上获取文本.

how to get the text on a link button which is in a datalist.

推荐答案

您没有提到需要在哪里获取文本的链接. LinkButton.在Code后面?或者,在JavaScirpt中.

假设您需要CodeBehind中的文本,则可以按以下方式获取文本:

在DataList控件中添加ItemCommand 事件

You didn''t mention where you need to get the text of the LinkButton. In Code behind? Or, in JavaScirpt.

Assuming that you need the text in CodeBehind, you can get the text as follows:

Add an ItemCommand event in the DataList control

<asp:DataList ID="DataList1" runat="server"

           onitemcommand="DataList1_ItemCommand">
       <ItemTemplate>
           <asp:Label ID="Label1" runat="server" Text='<%#Bind("Name") %>'></asp:Label>
           <asp:LinkButton ID="LinkButton1" runat="server" Text="Click Me"></asp:LinkButton>
       </ItemTemplate>
       </asp:DataList>



然后,在ItemCommand事件处理程序中获取LinkBut​​ton的文本,如下所示:



And, get the Text of the LinkButton in the ItemCommand event handler as follows:

protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
       LinkButton linkButton = e.CommandSource as LinkButton;
       string commandText = linkButton.Text;
}



或者,如果需要在页面加载(数据绑定)时获取LinkBut​​ton文本,则可以按以下方式获取它:

将ItemDataBound事件处理程序添加到DataList



Or, if you need to get the LinkButton text at page load (Data Bound), you can get it as follows:

Add an ItemDataBound event handler to the DataList

<asp:DataList ID="DataList1" runat="server"
            onitemcommand="DataList1_ItemCommand"
            onitemdatabound="DataList1_ItemDataBound">



在事件处理程序方法中获取LinkBut​​ton Text:



Get the LinkButton Text in the event handler method:

protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
       if (e.Item.ItemType == ListItemType.Item)
       {
           LinkButton linkButton = e.Item.FindControl("LinkButton1") as LinkButton;
           string commandText = linkButton.Text;
       }
}



请让我知道是否需要使用JavaScript



Please let me know if you need to get the text using JavaScript


来获取文本.您可以使用FindControl在单击功能上找到该特定的linkbutton.然后将linkbutton的文本值分配给您需要分配的文本框.
You can use FindControl to find that particular linkbutton on the on- click function.Then assign the text value of linkbutton to the textbox you need to assign.


这篇关于数据列表中的链接按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆