Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How I use the ItemCommand Event for my ListView in my ASP.NET Application

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

like image 202
Tarasov Avatar asked Jul 23 '12 11:07

Tarasov


People also ask

Which ListView event is raised when the user clicks on a row?

The ItemCommand event is raised when a button in the ListView control is clicked. This enables you to perform a custom routine whenever this event occurs.

What is ListView control in asp net?

The ListView control displays columns and rows of data and allows sorting and paging. It is by far the most popular data display control, and is ideal for understanding how data display controls interact with data retrieval controls and code.


2 Answers

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

like image 62
Ibrahem Ahmed Shehata Avatar answered Sep 25 '22 22:09

Ibrahem Ahmed Shehata


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");
like image 28
Jupaol Avatar answered Sep 22 '22 22:09

Jupaol