So I have Web Forms project in Visual Studio with Master page. It the Add.aspx
I have:
<div class="form-group">
<asp:textBox runat="server" ID="surname" CssClass="textbox"></asp:textBox>
<label for="surname">Surname</label>
</div>
I want to set text in this TextBox from Master Page. In Add.aspx.cs
I have:
protected void Page_Load(object sender, EventArgs e)
{
}
public void SetSurname(String value)
{
this.surname.Text = value;
}
And at Site.Master.cs
I am trying to call SetSurname
to set it to special value:
protected void Page_Load(object sender, EventArgs e)
{
Add AddForm = new Add();
AddForm.SetSurname("Test");
}
But I am getting error: Object reference does not point to an instance of an object
pointing to this.surname.Text = value;
. Am I wrong somewhere? Mabye I should use some functions like Page_Afterload
or etc.
If i understant correctly, the TextBox
surename
exists only in Add.aspx
. If so, you won't be able to call the method SetSurname()
from anywhere else except inside of Add.aspx
, because there is no guarantee the the Control surename
exists in the current showing page.
Also, the line Add AddForm = new Add();
is meaningless, unless you use it somewhere else in the code. If you want to use SetSurname()
, you should do that from the Page_Load()
of Add.aspx
.
If I got the wohle thing wrong please notify me.