Click here to Skip to main content
15,891,682 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how to get the text on a link button which is in a datalist.
Posted

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

XML
<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>


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

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


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

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


Get the LinkButton Text in the event handler method:

C#
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;
       }
}


Please let me know if you need to get the text using JavaScript
 
Share this answer
 
v2
Comments
srujanac# 30-Aug-10 2:01am    
thanks i think this would help me i'll try and let you know
demouser743 30-Aug-10 2:14am    
Reason for my vote of 5
Nice way of explanation
Al-Farooque Shubho 30-Aug-10 2:34am    
Thanks
rakeshjena 17-Jan-14 14:05pm    
why you have chosen bind here why not eval
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900