In some of my my user controls, the sub elements are not created at page load
I know that I should understand this, but I cannot work it out. I have a page with a number of user controls on it - I have created a set of user controls to handle the data on the page, because it helps to divide it. Some of the user controls are running fine, however, others are failing.
The error is that the subcontrols are not created - i.e. they are set to null - in Page_Load. But they should have been created at this point, I thought - and the controls that do work also have subcontrols that seem to work. And it is not typos of the names, because the pages compile, and I have not messed about with the names.
Does anyone have any ideas or suggestions as to why this may be? I don;t think posting code will help, as there is nothing special in the code, but if anyone wants to see anythign then let me know.
This is c#, dotnet, web site. Thank you!!
ETA some code:
<tr><cass:Comment id="comment" runat="server" Title="Comments"/></tr>
this is the html. The cass:Comment control is working in other places.
comment.Value = value.Comment;
when I try this in Page_Load - after EnsureChildControls, I get the message:
Object reference not set to an insta开发者_JS百科nce of an object. - with not a lot of other information.
When I explore, it is the comment object that is set to null.
I should point out that this occurs on other controls, so it is not the comments control - this happens to be one I amended to have EnsureChildControls
depending on your whole design, in some cases you should ensure sub/child controls have been created, for example in many cases properties of your Composite Server Control are accessed this way:
public string Text
{
get
{
EnsureChildControls();
return label.Text;
}
set
{
EnsureChildControls();
label.Text = value;
}
}
refer here for full explanations: Composite Server Control Sample
as I said depends on the real case and classes you are using but impossible to know exactly without seing your code.
I have resolved this by rewritign the controls as server controls - not ideal, but it does the job. I think that the problem may have been caused by confusion of namespaces class names, meaning I may have confused something. But I do not have a definitive answer.
精彩评论