6

I have a ASP.NET Application with a ListView. In every Row in my ListView I have a LinkButton that open a new webform "Benutzer.aspx". my Problem is that I don't get the Index of this Row. I use the ItemCommand Event but it not work :(

Here my Code:

ASPX:

...

        <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:LinkButton runat="server" Text="Anzeigen" CommandName="Anzeigen" OnCommand="ListView1_ItemCommand" CommandArgument="myArguments"></asp:LinkButton></td>

            </tr>

        </ItemTemplate>

...

cs file:

...

protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            if (e.CommandName == "Anzeigen")
            {
                Label lbText = (Label)e.Item.FindControl("Label2");

               string email = lbText.Text;

               Session["email"] = email;

               Response.Redirect("Benutzer.aspx");

            }
        }

...

What is the matter :(

tarasov

1
  • What's the exact problem? My guess is that you cannot find the label is that right?
    – Jupaol
    Jul 23, 2012 at 11:09

5 Answers 5

9

Try this:

First you need to have the index of the button. So in the html code add this in the CommandArgument of the button to get the index:

CommandArgument='<%# Container.DataItemIndex %>'

Then in the codebehind:

if (e.CommandName == "Anzeigen")
{
      Label lbText = ListView1.Item[e.CommandArgument].FindControl("Label2");
      string email = lbText.Text;           

           Session["email"] = email;           

           Response.Redirect("Benutzer.aspx");           
}

Hope I Helped

1
  • Get I a Int DataTyp by Container.DataItemIndex? I get a error by Label lbText = ListView1.Item[(int)e.CommandArgument].FindControl("Label2"); Errortext: The specified cast is not valid. ...but I like your Idea...hope it is a little error ;(
    – Tarasov
    Jul 23, 2012 at 12:08
1

You cannot find the control because it is contained in the child control collection of another server control:

<tr runat="server">

You need to try to find the control recursively:

Take a look

Better way to find control in ASP.NET

Or you can use this extension method:

public static class ControlExtensions
{
    public static Control FindControlRecursively(this Control control, string targetControlID)
    {
        if (control == null)
        {
            return null;
        }

        var ctrl = control.FindControl(targetControlID);

        if (ctrl == null)
        {
            foreach (Control child in control.Controls)
            {
                ctrl = FindControlRecursively(child, targetControlID);

                if (ctrl != null)
                {
                    break;
                }
            }
        }

        return ctrl;
    }
}

Usage:

var ctrl = e.Item.FindControlRecursively("your control ID");
2
  • what I need for a Namespace for this class?
    – Tarasov
    Jul 23, 2012 at 11:41
  • Now I habe remove runat="server" by <tr> tags...and my <Button> shows so: <asp:Button ID="Button1" Text="Anzeigen" OnCommand="Button1_Command" CommandName="Select" CommandArgument="MyArgument" runat="server" /> but I don't can start the Method :protected void Button1_Command(object sender, ListViewCommandEventArgs e) { if (e.CommandName == "Select") { var index = e.Item.FindControlRecursively("Label2"); Label lb = (Label)myListView.Items[selectedIndex].FindControl("Label2"); ... } }
    – Tarasov
    Jul 23, 2012 at 11:50
1

The code you have furnished is simply fine... "just remove the 'CommandArgument' from your listview property , bcoz..its already have the dataindex you are looking for. By specifying a command argument you are overriding the default one. So just remove the command argument and your code will work fine... :)

1

I am a VB programmer Check this method may b it gives you some idea

after binding the list with datasource, In the itemCommand do this

Dim <sometext> As Label = TryCast(e.Item.FindControl("Anzeigen"), Label)

    If e.CommandName = "Anzeigen" Then
     'do what ever you like 
     'also you can use <sometext> if you want to extract data from list
     'simply use <sometext>.<whatproperty>, you can also store it in sessions like the email you are using.

         Session("email") = email         

       Response.Redirect("Benutzer.aspx");  
    End If

let me know if it helps you solve your problem.

1

This is the HTML, then build the OnItemCommand.

<asp:ListView ID="lvFiles" runat="server"  DataKeyNames="FileName" OnItemCommand="lvFiles_ItemCommand">
  <ItemTemplate>
    <tr runat="server">
      <td style="width:80px">
        <asp:LinkButton runat="server" 
                        ID="SelectEmployeeButton" 
                        Text="Download File"   
                        CommandName='<%#Eval("FileName")%>'
                        CommandArgument='<%#Eval("FileName")%>' />
      </td> 
    </tr>
  </ItemTemplate>
</asp:ListView>

Here is the code behind...

protected void lvFiles_ItemCommand(object sender, ListViewCommandEventArgs e)
{
    string v = e.CommandArgument.ToString(); 
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.