creating usercotrols from webcontrol

E

Ersin Gençtürk

hi , I am trying to add a usercontrol to the control collection of a
webcontrol but I get "Object reference not set to an instance of an object."
errors.On the other hand , by changing the code creating Usercontrol to
create a Webcontrol (label ,textbox etc) I can use it successfully.I think
there is a problem with childcontrolscreating.Is there any ideas ?

Note that : ListSelection is my user control.

-ersin gençtürk

I am sending the source code below :

public class ProductGroups: System.Web.UI.WebControls.WebControl,
INamingContainer

{

private bool _EditMode;


public ProductGroups()

{

}

/// <summary>

/// Turns control into editing mode

/// </summary>

public bool EditMode

{

get

{

return(_EditMode);

}

set

{

_EditMode=value;

}

}


protected override void CreateChildControls()

{

base.CreateChildControls ();

this.EnsureChildControls();

if (EditMode)

{

ListSelection ListSelection1=new ListSelection();

ListSelection1.DataValueField="ProductGroupId";

ListSelection1.DataTextField="ProductGroupName";

ListSelection1.NotSelectedItemsDataSource=//Some datasource code.

ListSelection1.NotSelectedItemsDataBind();

Controls.Add(ListSelection1);


}

else

{

Controls.Add(ProductGroupList1);

}



}

}
 
E

Ersin Gençtürk

I am gettin these errors :



[NullReferenceException: Object reference not set to an instance of an
object.]
Project.Controls.ListSelection.set_DataValueField(String value) in
c:\inetpub\wwwroot\nexumportal2\controls\listselection.ascx.cs:123
Project.Controls.NexumProductGroups.CreateChildControls() in
c:\inetpub\wwwroot\nexumportal2\controls\nexumproductgroups.cs:57
System.Web.UI.Control.EnsureChildControls() +92
System.Web.UI.Control.PreRenderRecursiveInternal() +38
System.Web.UI.Control.PreRenderRecursiveInternal() +125
System.Web.UI.Control.PreRenderRecursiveInternal() +125
System.Web.UI.Page.ProcessRequestMain() +1499


I can set Controls.ListSelection.DataValue property when I use this control
from a webpage.
 
E

Ersin Gençtürk

ok.I have found a way :

I changed this line :

ListSelection ListSelection1=new ListSelection();

to:
ListSelection
ListSelection1=(ListSelection)this.Page.LoadControl("~/controls/ListSelectio
n.ascx");



So I am dynamically loading user control.But I don't think this is the
common way to do this.No Ideas ?



Ersin Gençtürk said:
I am gettin these errors :



[NullReferenceException: Object reference not set to an instance of an
object.]
Project.Controls.ListSelection.set_DataValueField(String value) in
c:\inetpub\wwwroot\nexumportal2\controls\listselection.ascx.cs:123
Project.Controls.NexumProductGroups.CreateChildControls() in
c:\inetpub\wwwroot\nexumportal2\controls\nexumproductgroups.cs:57
System.Web.UI.Control.EnsureChildControls() +92
System.Web.UI.Control.PreRenderRecursiveInternal() +38
System.Web.UI.Control.PreRenderRecursiveInternal() +125
System.Web.UI.Control.PreRenderRecursiveInternal() +125
System.Web.UI.Page.ProcessRequestMain() +1499


I can set Controls.ListSelection.DataValue property when I use this control
from a webpage.


Ersin Gençtürk said:
hi , I am trying to add a usercontrol to the control collection of a
webcontrol but I get "Object reference not set to an instance of an object."
errors.On the other hand , by changing the code creating Usercontrol to
create a Webcontrol (label ,textbox etc) I can use it successfully.I think
there is a problem with childcontrolscreating.Is there any ideas ?

Note that : ListSelection is my user control.

-ersin gençtürk

I am sending the source code below :

public class ProductGroups: System.Web.UI.WebControls.WebControl,
INamingContainer

{

private bool _EditMode;


public ProductGroups()

{

}

/// <summary>

/// Turns control into editing mode

/// </summary>

public bool EditMode

{

get

{

return(_EditMode);

}

set

{

_EditMode=value;

}

}


protected override void CreateChildControls()

{

base.CreateChildControls ();

this.EnsureChildControls();

if (EditMode)

{

ListSelection ListSelection1=new ListSelection();

ListSelection1.DataValueField="ProductGroupId";

ListSelection1.DataTextField="ProductGroupName";

ListSelection1.NotSelectedItemsDataSource=//Some datasource code.

ListSelection1.NotSelectedItemsDataBind();

Controls.Add(ListSelection1);


}

else

{

Controls.Add(ProductGroupList1);

}



}

}
 
Top