How to get the right line index of ListView

advertisements

I wrote a ASP.NET Application that get Data from the Active Directory. I use a ListView to display this data. The User input a String (Lastname or a part of this) in a TextBox. Than the ListView list all AD Users with the same string from the TextBox. Every Line get a Button "Anzeigen" to get more Informations about the User. This ListView has six columns and every line show a User. in column number six is the button "Anzeigen". If a User click on this button open a new WebForm "benutzer.aspx" with more Informations abaout this seleced User and get a Session Value "email" from the line.

My Problem:

I don't know how I get the Index of the Line of the ListView that I need for the Session Value.

My Code:

cs file:

 protected void Button1_Command(object sender, CommandEventArgs e)
        {
            if (e.CommandName == "Anzeigen")
            {

                //This give me everyone the Value -1 back
                int selectedLine = myListView.SelectedIndex; 

                //I need the Line Index for the right Value
                Label lb = (Label)myListView.Items[selectedLine].FindControl("Label2"); 

                   string email = lb.Text;

                   Session["email"] = email; 

                Response.Redirect("Benutzer.aspx");

            }

        }

ASPX File:

...
        <ItemTemplate>

            <tr runat="server"> 

                <td align="left" ><asp:Label ID="Label1" Text='<%# Eval("Benutzer") %>' runat="server" /></td>
                <td align="left"><asp:Label ID="Label2" Text='<%# Eval("eMail") %>' runat="server" /></td>
                <td align="left"><asp:Label ID="Label3" Text='<%# Eval("Vorname") %>' runat="server" /></td>
                <td align="left"><asp:Label ID="Label4" Text='<%# Eval("Nachname") %>' runat="server" /></td>
                <td align="left"><asp:Label ID="Label5" Text='<%# Eval("Telefon") %>' runat="server" /></td>
                <td align="left"><asp:Button ID="Button1" Text="Anzeigen" OnCommand="Button1_Command" CommandName="Anzeigen" CommandArgument="myArgument" runat="server" /></td>

            </tr>

        </ItemTemplate>
...

I search and I found listview selectedindices but it don't work :( and I dont't can use it in my Application .

tarasov


Use ListView's ItemCommand rather than Button's on command

see http://www.codeproject.com/Articles/24570/Complete-ListView-in-ASP-NET-3-5 for more detail. One more thing From example you can see that author has extracted the values from e.Item. You can pass the key(email,username or whatever) as CommandArgument and can access that value directly from command argument. how to pass it

<asp:LinkButton ID="myLink" runat="server" CommandName="Anzeigen" CommandArgument='<%#Eval("KeyColumn")%>'>Anzeigen</asp:LinkButton>

Also use Linkbutton rather than Asp:Button