problem med delegate
Hejsa EJeg sidder og roder med en delegate så jeg kan finde ud af hvordan sådan en virker.
Jeg har fundet følgende eksempel, som jeg også kunne bruge i noget kode, men jeg kan ikke få det til at spille.
http://www.codeproject.com/aspnet/paginguc.asp
ovenstående ex. har 5 delegates, men jeg har kun brug for den ene (tror jeg) og har derfor lavet om i koden, ellers var der vel heller ikke nogen udfordring vel :)
min kode:
------------------------UserControl----------------------------------
public partial class userControls_selectPage : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
populateShowPage(this.TotalPages);
EnableDisableButtons(this.CurrentPage, this.TotalPages);
setDropDownPageNumber(this.CurrentPage);
}
public delegate void PageChangedEventHandler(object sender, ucSelectEventArgs e);
public class ucSelectEventArgs : EventArgs
{
private int m_iCurrentPage;
public ucSelectEventArgs()
{
}
public int arCurrentPage
{
get { return m_iCurrentPage; }
set { m_iCurrentPage = value; }
}
}
private void populateShowPage(int totalNumPages)...
private void setDropDownPageNumber(int currentPage)...
private void EnableDisableButtons(int currentPage, int totalPages)...
public event PageChangedEventHandler PageChanged;
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
this.ddShowPage.SelectedIndexChanged += new EventHandler(this.ddShowPage_SelectedIndexChanged);
}
public int CurrentPage
{
get {return Convert.ToInt32(ViewState["currentPage"]);}
set {ViewState["currentPage"] = value;}
}
public int TotalPages
{
get {return Convert.ToInt32(ViewState["totalPages"]);}
set {ViewState["totalPages"] = value;}
}
protected virtual void OnPageChanged(ucSelectEventArgs args)
{
if (PageChanged != null)
{
// Invoke the delegates.
PageChanged(this, args);
}
}
protected void imgNext_Click(object sender, ImageClickEventArgs e)
{
ucSelectEventArgs args = new ucSelectEventArgs();
CurrentPage += 1;
args.arCurrentPage = CurrentPage;
setDropDownPageNumber(CurrentPage);
EnableDisableButtons(CurrentPage, TotalPages);
OnPageChanged(args);
}
protected void imgPrew_Click(object sender, ImageClickEventArgs e)...
protected void imgFirst_Click(object sender, ImageClickEventArgs e)...
protected void imgLast_Click(object sender, ImageClickEventArgs e)...
protected void ddShowPage_SelectedIndexChanged(object sender, EventArgs e)
{
ucSelectEventArgs args = new ucSelectEventArgs();
args.arCurrentPage = CurrentPage;
EnableDisableButtons(CurrentPage, TotalPages);
OnPageChanged(args);
}
}
------------------------End UserControl----------------------------------
Og på hovedsiden har jeg bla:
protected userControls_selectPage ucSelectPage2;
private void InitializeComponent()
{
this.ucSelectPage2.PageChanged += new userControls_selectPage.PageChangedEventHandler(this.PageChanged);
this.Load += new System.EventHandler(this.Page_Load);
this.Init += new EventHandler(this.Page_Init);
}
private void Page_Init(object sender, System.EventArgs e)
{
this.ucSelectPage.CurrentPage = 1;
this.tblVin.CssClass = "mainTable";
}
protected void PageChanged(object sender, userControls_selectPage.ucSelectEventArgs e)
{
int currentPage = e.arCurrentPage;
fillData(currentPage);
lblLink.Text = "[xxx]";
}
PageChanged på hovedsiden bliver bare aldrig kaldt...
Hvad gør jeg galt ???