public void Test()
{
...
}
//
Then in the parent page code-behind Website.aspx.cs I have the script
protected WebsiteFooter WebsiteFooter1;
and the following script in the Page_Render event:
WebsiteFooter WebsiteFooter1 = new WebsiteFooter();
this.WebsiteFooter1.Test();
When I run the program I get the message:
Object reference not set to an instance of an object.
I would appreciate suggestion to resolve this problem.
Thank you.That's not going to work.
I'm not sure what you're trying to do. However, if you just want to dynamically add the Footer User Control, you can use the following in your codebehind:
UserControl DynamicUserControl;
DynamicUserControl = Page.LoadControl( "WebsiteFooter.ascx" );
FooterPlaceholder.Controls.Add( DynamicUserControl );
If Test() is called from your WebsiteFooter.ascx's own code (such as in its Page_Load method), then it will execute without you having to call it.
Does that help?
Thank you for your reply. I'm sorry that my question was not clear.
what I want to be able to do is call the function, or a control's (which exists in a user control) method from the parent page or from another user control.
e.g. i have a logon.ascx & a header.ascx - which has a label control with text 'Sign In'.
when the user logs on, I want to be able to change the text in header.ascx to 'Sign Off'.
Likewise, I would also like to be able to run a function from another user control.
e.g.
uc1 has function1() which runs a query
uc2 has function2() which also runs a query
I would like to make only one trip to the server and retrieve both sets of data and bind to controls within their respective user controls.
Thanks again for your interest.
Having your ASPX page work with your ASCX user control is explained in theQuickStart Tutorials:
Exposing User Control Properties
You could, for example, have a property in your ASCX called IsUserAuthenticated. If false, the label is "Sign In"; if true, the label is "Log Off". The property would be tested and the label set during the User Control's Page_Load method.
As to having one ASCX talk to another, that's a bad idea. ASCX user controls should be "encapsulated" ... that is, they should not rely on the existence of other ASCX controls. Instead, your ASPX page should handle any communication between ASCX user controls.
Hi -
>>
Instead, your ASPX page should handle any communication between ASCX user controls.
>
That is what I am attempting to do when I get the error message 'Object reference not set to an instance of an object'.
From the aspx page I am trying to run a function which is contained in the user control UCHeader.ascx.cs
In the aspx.cs page I have -
protected UCHeader myUCHeader
--
private void btnSubmit_Click(object sender, System.EventArgs e)
{
UCHeader myUCHeader = new UCHeader();
myUCHeader.FunctionWithinUCHeader(); <-- this line gives error
}
In UCHeader.ascx.cs I have a pulic function
public void FunctionWithinUCHeader()
{
....
}
Thanks.
If you tell me what FunctionWithinUCHeader will actually do, I'll write a sample for you.
public void GetTransactionList()
{
SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["connString"]);
//
myConnection.Open();
//
SqlCommand myCommand = new SqlCommand("sp_GetTransactionList", myConnection);
myCommand.CommandType = CommandType.StoredProcedure;
//
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = myCommand;
sda.Fill(ds);
dgList.DataSource = ds.Tables[0];
dgList.DataBind();
//
myCommand.Connection.Close();
}
You should allow your user control's own Page_Load method call this GetTransactionList() method.
public void Page_Load( object sender, EventArgs e )
{
GetTransactionList();
}
User Controls should be as self-sufficient as possible ... populating its own controls, and managing its own events (such as when the page posts back).
0 comments:
Post a Comment