Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#3.0
how to get the text on a link button which is in a datalist.
Posted 29-Aug-10 20:26pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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

Solution 1

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.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 277
1 Maciej Los 210
2 BillWoodruff 205
3 Jochen Arndt 180
4 Sergey Alexandrovich Kryukov 165
0 OriginalGriff 5,130
1 DamithSL 4,157
2 Maciej Los 3,650
3 Kornfeld Eliyahu Peter 3,460
4 Sergey Alexandrovich Kryukov 2,811


Advertise | Privacy | Mobile
Web03 | 2.8.141216.1 | Last Updated 30 Aug 2010
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100