Pokusavam da u potpunosti ovladam delegatima i event handlingom, pa sam u tu svrhu napravio c# web projekat koji se sastoji od jedne forme i jedne user kontrole.
User kontrola ima dva dugmeta za napred i nazad, sa jednom labelom izmedju koja sadrzi neki broj. Web forma ima labelu i gore spomenutu user kontrolu. Text labele treba da bude identican onom od labele u user kontroli, a pritiskom na dugme napred sadrzaj labele kontrole se inkrementira, a pritiskom na dugme nazad, sadrzaj labele kontrole se dekrementira. Ovo je jednostavno i bazicno.
Ono sto ja zelim je
1. da se pritiskom na dugme napred/nazad izmeni sadrzaj labele na web formi.
2. da ja sam formiram klasu koja bi sadrzala argumente koji bi se slali web formi. (u ovom slucaju to bi bio sadrzaj labele)
3. da user kontrola ima svoj namespace zbog mogucnosti koriscenja u nekim druygim projektima.
Ovo sam sve probao da uradim, ali konstanto dobijam gresku
Object reference not set to an instance of an object.
Source za user kontrolu glasi:
Code:
namespace mojaKontrola
{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
public delegate void IndexChangedHandler(object kontrola, RecordIndexEventArgs myEA);
/// <summary>
/// Summary description for RecordIndex.
/// </summary>
///
public class RecordIndex : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Label lbIndex;
protected System.Web.UI.WebControls.Button Button2;
private int _curIndex;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}
#region properties
public int Index
{
set
{
this._curIndex = value;
}
get
{
return this._curIndex;
}
}
#endregion
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Button2.Click += new System.EventHandler(this.Button2_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
#region Dugmad
private void Button2_Click(object sender, System.EventArgs e)
{
this._curIndex = (System.Convert.ToInt32((this.lbIndex.Text))) + 1;
this.SetCurrentCursor();
}
private void Button1_Click(object sender, System.EventArgs e)
{
this._curIndex = (System.Convert.ToInt32( this.lbIndex.Text)) - 1;
this.SetCurrentCursor();
}
#endregion
#region delegati i events
public event IndexChangedHandler OnIndexChanged;
#endregion
private void SetCurrentCursor()
{
this.lbIndex.Text = this._curIndex.ToString();
OnIndexChanged(this, new RecordIndexEventArgs(this._curIndex));
}
}
#region Event argumenti za handlere ove klase
public class RecordIndexEventArgs : System.EventArgs
{
private int index;
public RecordIndexEventArgs(int i)
{
index = i;
}
public int Index
{
set
{
this.index = value;
}
get
{
return index;
}
}
}
#endregion
}
namespace mojaKontrola
{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
public delegate void IndexChangedHandler(object kontrola, RecordIndexEventArgs myEA);
/// <summary>
/// Summary description for RecordIndex.
/// </summary>
///
public class RecordIndex : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Label lbIndex;
protected System.Web.UI.WebControls.Button Button2;
private int _curIndex;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}
#region properties
public int Index
{
set
{
this._curIndex = value;
}
get
{
return this._curIndex;
}
}
#endregion
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Button2.Click += new System.EventHandler(this.Button2_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
#region Dugmad
private void Button2_Click(object sender, System.EventArgs e)
{
this._curIndex = (System.Convert.ToInt32((this.lbIndex.Text))) + 1;
this.SetCurrentCursor();
}
private void Button1_Click(object sender, System.EventArgs e)
{
this._curIndex = (System.Convert.ToInt32( this.lbIndex.Text)) - 1;
this.SetCurrentCursor();
}
#endregion
#region delegati i events
public event IndexChangedHandler OnIndexChanged;
#endregion
private void SetCurrentCursor()
{
this.lbIndex.Text = this._curIndex.ToString();
OnIndexChanged(this, new RecordIndexEventArgs(this._curIndex));
}
}
#region Event argumenti za handlere ove klase
public class RecordIndexEventArgs : System.EventArgs
{
private int index;
public RecordIndexEventArgs(int i)
{
index = i;
}
public int Index
{
set
{
this.index = value;
}
get
{
return index;
}
}
}
#endregion
}
Source za formu glasi:
Code:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace delegat
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label lbIme;
protected mojaKontrola.RecordIndex ri;
private void Page_Load(object sender, System.EventArgs e)
{
this.lbIme.Text = "0";
// Put user code to initialize the page here
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
private void ri_OnIndexChanged(object kontrola, mojaKontrola.RecordIndexEventArgs myEA)
{
this.lbIme.Text = ((RecordIndex)kontrola).Index.ToString();
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
this.ri.OnIndexChanged += new mojaKontrola.IndexChangedHandler(this.ri_OnIndexChanged);
}
#endregion
}
}
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace delegat
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label lbIme;
protected mojaKontrola.RecordIndex ri;
private void Page_Load(object sender, System.EventArgs e)
{
this.lbIme.Text = "0";
// Put user code to initialize the page here
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
private void ri_OnIndexChanged(object kontrola, mojaKontrola.RecordIndexEventArgs myEA)
{
this.lbIme.Text = ((RecordIndex)kontrola).Index.ToString();
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
this.ri.OnIndexChanged += new mojaKontrola.IndexChangedHandler(this.ri_OnIndexChanged);
}
#endregion
}
}
Greska glasi Object reference not set to an instance of an object.
na liniju:
Code:
this.ri.OnIndexChanged += new mojaKontrola.IndexChangedHandler(this.ri_OnIndexChanged);
this.ri.OnIndexChanged += new mojaKontrola.IndexChangedHandler(this.ri_OnIndexChanged);
koja se nalazi u kodu za webformu u f-ji InitializeComponent()...
Zasto ovo nece da radi?
PS: Greska se pojavljuje u run time-u, nakon uspesnog kompajliranja
To me boxing is like a ballet, except
there is no choreography, no dancing and the dancers hit each other...
there is no choreography, no dancing and the dancers hit each other...