Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Object reference not set to an instance of an object

Status
Not open for further replies.

cyrus71

Programmer
Feb 6, 2006
34
SE
i want to change values of controls in a User Control when i clikc on a button in a another user control, but i get following error:
Object reference not set to an instance of an object

Code:
//in My DownUserControl i call
protected void BTN_Click(object sender, EventArgs e)
{
TopUserControl TopUC = new TopUserControl();
TopUC.Title = "hello";
}

//and here I get the error in the TopUserControl:
public string Title
{
get { return TitleLable.Text }
set { TitleLable.Text = value }
}
 
Your problem is: you create the only instance of this control within your button click so it is disposed just after you write TopUC.Title = "hello";


what you need to do is put your usercontrol declaration outside of the function.

public class MyClass
{
private TopUserControl TopUC = new TopUsercontrol();

public MyClass()
{
InitializeComponents();
}

protected void BTN_Click(object sender, EventArgs e)
{
TopUC.Title = "hello";
}

// Then you can get the Title property from your usercontrol

private void button_click(object sender, EventArgs e)
{
MessageBox.Show(TopUC.Title);
}

}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top