John M
John M

Reputation: 14688

ASP .NET - Retrieve values from selected Listview control row?

My Listview control contains 4 columns and 30 rows. I can retrieve the row number by using:

//get row of listview item
ListViewDataItem item1 = e.Item as ListViewDataItem;
int findMe = item1.DisplayIndex;

How do I then get values from one or all 4 columns?

I was trying:

this.lblReponseRoute.Text = item1.FindControl("routenameLabel").ID.ToString();

UPDATE1:

The final solution is:

//get row of listview item
ListViewDataItem item1 = e.Item as ListViewDataItem;
int findMe = item1.DisplayIndex;

//find label value
var routeLabel = (Label)ListView1.Items[findMe].FindControl("routenameLabel");
this.lblReponseRoute.Text = routeLabel.Text; 

Upvotes: 2

Views: 12433

Answers (2)

Tomy Finnegan
Tomy Finnegan

Reputation: 11

var routeLabel = (Label)item1.FindControl("routenameLabel"); 
lblResponseRoute.Text = routeLabel.ID.ToString();

It should be:

var routeLabel = (Label)item1.FindControl("routenameLabel"); 
lblResponseRoute.Text = routeLabel.Text.ToString();

.Text not .ID, we already know the name of the label.

Upvotes: 1

Josh Anderson
Josh Anderson

Reputation: 6005

If routenameLabel is a server control, I believe you're going to have to cast it as such prior to accessing the properties:

var routeLabel = (Label)item1.FindControl("routenameLabel");
lblResponseRoute.Text = routeLabel.ID.ToString();

Do you get an error on the code you've posted?

Edit: Note that in your real code you'd want to test for null before casting to the Label.

Upvotes: 2

Related Questions