Witam,
Zaczynam uczyć się pisać w asp.net, korzystam z tutoriala: http://asp.net-tutorials.com/. Natknąłem się jednak na mały problem. Nie wiem czy błąd tkwi w tutorialu, czy w złym zrozumieniu przeze mnie któregoś zdania. Prosiłbym o wyjaśnienie jak z tego wybrnąć. Zamieszczam treść moich plików i wynik z kompilatora.
plik default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="default.aspx.cs" Inherits="MyFirstWebApp.WebForm1" %>
<%@ Register TagPrefix="My" TagName="UserInfoBoxControl" Src="~/UserInfoBoxControl.ascx" %>
<%@ Register TagPrefix="My" TagName="EventUserControl" Src="~/EventUserControl.ascx" %>
<%@ Reference Control="~/UserInfoBoxControl.ascx" %>
<%@ Reference Control="~/EventUserControl.ascx" %>
HelloWorldLabel.Text = "Hello World!";
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label runat="server" ID="HelloWorldLabel"></asp:Label>
<br /><br />
<asp:TextBox runat="server" ID="TextInput"></asp:TextBox>
<asp:Button runat="server" ID="GreetButton" Text="Say Hello!" OnClick="GreetButton_Click" />'
<br /><br />
<asp:DropDownList runat="server" ID="GreetList" AutoPostBack="true" OnSelectedIndexChanged="GreetList_SelectedIndexChanged">
<asp:ListItem Value="no one">No one</asp:ListItem>
<asp:ListItem Value="world">World</asp:ListItem>
<asp:ListItem Value="universe">Universe</asp:ListItem>
</asp:DropDownList>
<br /><br />
<My:UserInfoBoxControl runat="server" ID="MyUserInfoBoxControl"></My:UserInfoBoxControl>
<br /><br />
<My:UserInfoBoxControl runat="server" ID="UserInfoBoxControl1" UserName="John Doe" UserAge="45" UserCountry="Australia" />
<br /><br />
<asp:PlaceHolder runat="server" ID="phUserInfoBox" OnLoad="phUserInfoBox_Load" ></asp:PlaceHolder>
<br /><br />
<My:EventUserControl runat="server" ID="MyEventUserControl" OnPageTitleUpdated="MyEventUserControl_PageTitleUpdated" />
</div>
</form>
</body>
</html>
plik default.aspx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace MyFirstWebApp
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
HelloWorldLabel.Text = "Hello, world!";
MyUserInfoBoxControl.UserName = "Jane Doe";
MyUserInfoBoxControl.UserAge = 33;
MyUserInfoBoxControl.UserCountry = "Germany";
phUserInfoBox.Controls.Add(LoadControl("~/UserInfoBoxControl.ascx"));
UserInfoBoxControl userInfoBoxControl = (UserInfoBoxControl)LoadControl("~/UserInfoBoxControl.ascx");
userInfoBoxControl.UserName = "John Doe";
userInfoBoxControl.UserAge = 78;
userInfoBoxControl.UserCountry = "Spain";
phUserInfoBox.Controls.Add(userInfoBoxControl);
}
protected void GreetButton_Click(object sender, EventArgs e)
{
HelloWorldLabel.Text = "Hello, " + TextInput.Text;
}
protected void GreetList_SelectedIndexChanged(object sender, EventArgs e)
{
HelloWorldLabel.Text = "Hello, " + GreetList.SelectedValue;
}
protected void phUserInfoBox_Load(object sender, EventArgs e)
{
}
public Control userInfoBoxControl { get; set; }
protected void MyEventUserControl_PageTitleUpdated()
{
this.Title = MyEventUserControl.PageTitle;
}
}
}
A tutaj UserControl:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="EventUserControl.ascx.cs" Inherits="MyFirstWebApp.EventUserControl" %>
Page title:
<asp:TextBox runat="server" ID="txtPageTitle"></asp:TextBox>
<asp:Button runat="server" ID="btnUpdatePageTitle" OnClick="btnUpdatePageTitle_Click" Text="Update" />
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace MyFirstWebApp
{
public partial class EventUserControl : System.Web.UI.UserControl
{
private string pageTitle;
public event EventHandler PageTitleUpdated;
public string PageTitle
{
get { return pageTitle; }
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnUpdatePageTitle_Click(object sender, EventArgs e)
{
this.pageTitle = txtPageTitle.Text;
if (PageTitleUpdated != null)
PageTitleUpdated(this, EventArgs.Empty);
}
}
}
Błąd serwera w aplikacji '/'.
Błąd kompilacji
Opis: Wystąpił błąd w czasie kompilowania zasobu wymaganego do obsłużenia tego żądania. Przejrzyj następujące szczegółowe informacje o tym błędzie i zmodyfikuj odpowiednio kod źródłowy.
Komunikat o błędzie kompilatora: CS0123: Żadne z przeciążeń dla elementu „MyEventUserControl_PageTitleUpdated” nie pasuje do delegata „System.EventHandler”.
Błąd źródła:
Wiersz 34: <asp:PlaceHolder runat="server" ID="phUserInfoBox" OnLoad="phUserInfoBox_Load" ></asp:PlaceHolder>
Wiersz 35:
Wiersz 36: <My:EventUserControl runat="server" ID="MyEventUserControl" OnPageTitleUpdated="MyEventUserControl_PageTitleUpdated" /> <--- Tu wywala błąd
Wiersz 37: </div>
Wiersz 38: </form>
Plik źródłowy: c:\Users\Paweł\Documents\Visual Studio 2013\Projectss\MyFirstWebApp\MyFirstWebApp\default.aspx Wiersz: 36