How to use ItemCommand event for my ListView in my ASP.NET application

I have an ASP.NET application with ListView. On every line in my ListView, I have a LinkButton that opens a new web form called "Benutzer.aspx". my problem is that i am not getting the pointer of this line. I am using the ItemCommand event, but it does not work :(

Here is 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's the matter:(

Tarasov

+4
source share
5 answers

Try the following:

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

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

Then in codebehind:

 if (e.CommandName == "Anzeigen") { Label lbText = ListView1.Item[e.CommandArgument].FindControl("Label2"); string email = lbText.Text; Session["email"] = email; Response.Redirect("Benutzer.aspx"); } 

I hope I helped

+5
source

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

 <tr runat="server"> 

You need to try to find the control recursively:

Take a look

Best way to find a 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; } } 

Using:

 var ctrl = e.Item.FindControlRecursively("your control ID"); 
+1
source

The code you provided is just fine ... "just remove the" CommandArgument "from your listview property, bcoz .. you already have the dataindex you are looking for. By specifying a command argument, you override the default value, so just remove the command argument and your code will work fine ... :)

+1
source

I am a VB programmer. Check out this method, maybe it gives you some idea.

after binding the list to the data source, In 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 you can solve your problem.

0
source

This is HTML, then create 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(); } 
0
source

All Articles