Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have ASP.NET Listview which is populated by a Datatable.

VB
<asp:ListView ID="lvCustomers" runat="server" GroupPlaceholderID="groupPlaceHolder1"
                                                    ItemPlaceholderID="itemPlaceHolder1" OnPagePropertiesChanging="lvCustomers_PagePropertiesChanging" ClientIDMode="AutoID">


In the page's body I have printed out the values from the table:

XML
<asp:Label ID="idlbl" runat="server" Visible="false" Text='<%# Eval("id") %>'></asp:Label>
<h4 class="timeline-title"><%# Eval("ActivityContent") %></h4>


In the code behind, I need to get the value of label 'idlbl' as a String through a button click event. How do I do this?
Posted

1 solution

Quote:
In the code behind, I need to get the value of label 'idlbl' as a String through a button click event. How do I do this?
If the Button is outside of ListView, then you need to loop through the Items of ListView and fetch inside the loop.

If the Button is inside, then you can use ListView.ItemCommand Event[^] and find the current item and its properties.
 
Share this answer
 
Comments
Dinuka Jayasuriya 13-Mar-15 7:29am    
It is inside
Then use ItemCommand Event.
Dinuka Jayasuriya 13-Mar-15 7:44am    
When does the ItemCommand get triggered? I've added a break pointed to the ItemCommand method but it doesnt get visited?
Steps
-----

1. Declare the event in markup.

<asp:ListView runat="server"
ID="lvCustomers"
OnItemCommand="lvCustomers_OnItemCommand"
......
......


2. Add the Button like this...

<asp:LinkButton runat="server"
ID="someButtonId"
Text="Some Text"
CommandName="SomeCommandName" />


3. Inside the Event do like below...

protected void lvCustomers_OnItemCommand(object sender, ListViewCommandEventArgs e)
{
Label idlbl = (Label)e.Item.FindControl("idlbl");
}
Dinuka Jayasuriya 13-Mar-15 8:14am    
Thank you but it doesnt make sense, What does the button do in your case? Does it get the value from the label? Why is the button not mapped to any of the events?

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