hey guys i have a web page involving a listview and every supitem has a button !!
deal is tht wen i click on the button i need to get the INDEX of the listview subitem in which the button was clicked!!! please help me through this :)

8 Years
Discussion Span
Last Post by Lusiphur

Please provide some code samples of what you have already tried so that we can see where perhaps the problem exists in your code.

It's very difficult to troubleshoot where problems in code exist if code is not provided to troubleshoot from :)

If, on the other hand, you haven't tried any possible solutions prior to asking for the solution here, I would encourage you to do some research into what you're trying to do. :)


Protected Sub button4_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim btn As Button = sender
dim i as integer
Dim tb As TextBox
tb = New TextBox
tb = ListView1.Items.Item(b).FindControl("textbox2")
tb.Visible = True
End Sub

here "B" is the index!!! so when i click the button the textbox in listview should be visible!!!

Edited by deceivingrakesh: n/a


Ok, so I see your VB code there (and I'm sorry but I don't know VB much, I'm a C# guy) but did you read through any of the search results linked in my previous post? Because at least 2-3 of the ones on page 1 of that search should be able to solve your issue for you :)

Again, not being a VB coder I can't really help you past that. Good luck though.


dude can u tell me how to do it in c# so tht atleast ill get the logic!!! i mean theres a button and a textbox in every listview items!! so when i click on the button i should be able to make the text box visible!! so can ya guide in C# :)


It's getting late here, I'm tired, and I've never tried this before...


I think this would work in C# to do what you're looking for assuming I got your variables correct (like textBox2 and such):

private void btnEquals_Click(object sender, EventArgs e)
        Button btn = (Button)(sender); // declare variable btn as event sender
        int itemNum = -1; // declare int itemNum with init value outside of listView range
        for (int a = 0; a < listView1.Items.Count; a++) //Loop items in listView
            for (int b = 0; b < listView1.Items[a].SubItems.Count; b++) //Loop subitems in listView
                if (listView1.Items[a].SubItems[b].Name == btn.Name) // check for button within subitems
                    itemNum = a;
        TextBox tb = new TextBox();
        tb.Text = listView1.Items[itemNum].SubItems["textBox2"].ToString();
        tb.Visible = true;

No guarantees, but I think this will work. And you might be able to reverse-engineer it to VB (for VB the statement "Button btn = (Button)(sender);" would likely evaluate to "Dim btn as Button = DirectCast(sender, Button)")

Hope this helps :)


:) thanks man!!! but i just want to know the index of the listview subitem so tht i can acces the remaining controls in the subitem!!!!


suppose i click on a buttton in a listview subitem!!! then i should get the index of tht subitem :)


ok, I was assuming that the subitem was the button based on your originally suplied code snippet.

Basically I was looking at it as listView -> items(rows) -> subItems(components within the row) and so the code sample I put together for you gives you the row index based on the fact that the row contains the subItem of the button.

The logic for the looping still stands if you need to go down further in levels but I'm not sure how far down the levels go for what you're trying to do.


I just double checked, the furthest you can drill-down on a listView is:

listView -> Items -> subItems

Based on that, I'm going to have to stick with what I said before, my first loop checks each row (items) and my second loop checks each item in that row (subItems) and determines if one of those subItems is the button that called the click event. Once it finds the right button it reports back the row index (Items) and attempts to populate the outside textBox with the values of an item called "textBox2" within that row.

If that's not what you're looking for then unfortunately I'm unable to help cus even getting that much for you involved going beyond what I usually use a listView for (as I've never really gone to the level of subItems before).

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.