简体   繁体   中英

How I create a EventHandler for a Button in ASP.NET

My Problem:

I have a ASP.NET Application and use a ListView. I get Datalines (eMail,Username,Firstname etc..) from the Active Directory and fill this in my ListView. Every Line has a Button "Show", with this Button I want to a other WebForm. With a Session I surrender the Data to the other WebForm. My Code:

  <LayoutTemplate> <table id="UserTable" runat="server" border="0" cellspacing="10" cellpadding="5"> <tr id="Tr1" runat="server"> <th id="Th1" runat="server">Benutzer</th> <th id="Th2" runat="server">eMail</th> <th id="Th3" runat="server">Vorname</th> <th id="Th4" runat="server">Nachname</th> <th id="Th5" runat="server">Telefon</th> </tr> <tr runat="server" id="ItemPlaceholder"> </tr> </table> </LayoutTemplate> <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> //Every Line get a Button <td align="left"><asp:Button ID="Button1" Text ="Anzeigen" runat="server" /></td> </tr> </ItemTemplate> </asp:ListView> 

The Button has a onClick= "..." Argument but how I make a Event for this and how I transfer to the other WebForm with the right Session Information. I think I must work with the Index from the Line :/

I need help :(

tarasov

Try this in your aspx you do

<asp:Button ID="Button1" OnCommand="Button1_Command" CommandArgument='<%# eval("ID") %>' CommandName="Anzeigen" runat="server" Text="Button" />

in code behind you do

    protected void Button1_Command(object sender, CommandEventArgs e)
    {
        if (e.CommandName == "Anzeigen")
        {
            string sID = e.CommandArgument.ToString();
            int id = 0;
            int.TryParse(sID, out id);

            if (id > 0)
            { 
             // do stuff
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM