C# 我如何在我的 ASP.NET 应用程序中为我的 ListView 使用 ItemCommand 事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11611291/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 18:31:04  来源:igfitidea点击:

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

c#asp.netlistviewactive-directoryitemcommand

提问by Tarasov

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 :(

我有一个带有 ListView 的 ASP.NET 应用程序。在我的 ListView 的每一行中,我都有一个 LinkBut​​ton,它打开一个新的网络表单“Benutzer.aspx”。我的问题是我没有得到这一行的索引。我使用了 ItemCommand 事件,但它不起作用:(

Here my Code:

这是我的代码:

ASPX:

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:

cs文件:

...

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

塔拉索夫

采纳答案by Ibrahem Ahmed Shehata

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:

首先你需要有按钮的索引。因此,在 html 代码中,在按钮的 CommandArgument 中添加以下内容以获取索引:

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

希望我有所帮助

回答by Jupaol

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

在 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");

回答by Ratheesh Raveendran Pillai

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... :)

您提供的代码非常好......“只需从您的列表视图属性中删除'CommandArgument',bcoz ..它已经拥有您正在寻找的数据索引。通过指定一个命令参数,您将覆盖默认参数。所以只需删除命令参数,您的代码将正常工作... :)

回答by SMHasnain

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

我是一名 VB 程序员 检查此方法可能会为您提供一些想法

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

将列表与数据源绑定后,在 itemCommand 中执行此操作

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.

如果它可以帮助您解决问题,请告诉我。

回答by user1684037

This is the HTML, then build the OnItemCommand.

这是 HTML,然后构建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(); 
}