Code:
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = "Sample text";
}
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = "Sample text";
}
E sad ako tu kontrolu ubacim na stranicu preko designer-a, sve radi super.
Medjutim, ako hocu da tu kontrolu kreiram u nekoj od metoda stranice i dodam je stranici npr.:
Code:
protected void Page_Load(object sender, EventArgs e)
{
MyControl con = new MyControl();
this.Controls.Add(con);
}
protected void Page_Load(object sender, EventArgs e)
{
MyControl con = new MyControl();
this.Controls.Add(con);
}
tada mi u metodi Page_Load controle baca null reference exception, kad dodje do linije "Label1.Text = "SampleText"", zato sto kontrola Label1 nije kreirana. Kako to rijesiti?
...