Hey,
bin noch ganz neu in asp.net und hab sofort ein essentielles Problem, an dem ich schon lange hänge: Ich schaffe es einfach nicht, ein einfaches OnClick-Event anzulegen. Wie geht man da normalerweise heran? Nutzt man normale HTML Tags oder die .net Controls? Ich habs momentan mit Ersterem:
C#-Quelltext
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17:
| protected void Page_Load(object sender, EventArgs e) { employees.Clear(); Datenbank.loadEmployeesIntoArray();
for (int i = 0; i < employees.Count; i++ ) { MyHtmlGenericControl entry = new MyHtmlGenericControl();
entry.ID = "entry-" + i; entry.TagName = "li"; entry.InnerHtml = "<span>" + (employees[i] as Mitarbeiter).Firstname + " " + (employees[i] as Mitarbeiter).Lastname + "</span>" + "<span>" + getAgeFromDate((employees[i] as Mitarbeiter).Birthday) + "</span><span class=\"hiding\" ></span>" ; entry.Attributes.Add("class", "employeeEntry"); entry.Attributes.Add("onclick", "displayInformation"); entry.relatedEmployee = (Mitarbeiter)employees[i];
maintable.Controls.Add(entry); } } |
Mit
C#-Quelltext
1:
| entry.Attributes.Add("onclick", "displayInformation"); |
versuche ich,
C#-Quelltext
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11:
| public void displayInformation(object sender, EventArgs e) { infoArea.InnerHtml = ""; Mitarbeiter currentEmployee = (sender as MyHtmlGenericControl).relatedEmployee;
infoArea.InnerHtml = "<li> Vorname: <span>" + currentEmployee.Firstname + "</span></li>"; infoArea.InnerHtml += "<li> Nachname: <span>" + currentEmployee.Firstname + "</span></li>"; infoArea.InnerHtml += "<li> Wohnort: <span>" + currentEmployee.Firstname + "</span></li>"; infoArea.InnerHtml += "<li> Postleitzahl: <span>" + currentEmployee.Firstname + "</span></li>"; infoArea.InnerHtml += "<li> Straße: <span>" + currentEmployee.Firstname + "</span></li>"; infoArea.InnerHtml += "<li> Geburtsdatum: <span>" + currentEmployee.Firstname + "</span></li>"; } |
aufzurufen (das "displayInformation" habe ich so irgendwo gesehen, bin mir nicht sicher, ob es richtig ist, die Klammern wegzulassen).
Hier noch mein HTML Code in default.aspx:
XML-Daten
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19:
| <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="HelloASP._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <link href="StyleSheet1.css" rel="stylesheet" type="text/css" /> <title></title> </head> <body> <form id="form1" runat="server" > <div id="wrapper" > <div id="header" >Mitarbeiterverwaltung</div> <div id="maintable" runat="server" ></div> <div id="infoArea" runat="server" ></div> </div> </form> </body> </html> |
Krieg im Firebug immer die Meldung ""displayInformation" not found". Was mache ich also alles falsch? =D Wäre nett, wenn mir jemand helfen und/oder auch eine gute Tutorial-Seite dazu posten könnte, finde nämlich leider nichts.
Danke
edit: MyHtmlGenericControl ist ein normales HtmlGenericControl mit einem zusätzlichen Feld.