2

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; 

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks. I wasn't getting any specific error - just the ID of the label when I want it's value. I have edited my post with the final code solution.
1
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.