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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

trouble getting asp namespace to show in web application codebehind

Status
Not open for further replies.

wcormie

Programmer
Feb 27, 2009
7
US
I'm trying to add a control to my webpage in the codebehind using something like this:

Code:
placeHolder.Controls.Add(new asp.mycontrol_ascx);

but the asp namespace isn't showing up, what do I need to do or reference to get it to show up?
 
if you want to add a web user control dynamically use
Code:
myusercontrol ctrl = (myusercontrol)Page.LoadControl("~/directory/to/myusercontrol.ascx");
you can also do this using markup and a page declaration
Code:
<%@Register tagprefix="foo" src="~/directory/to/myusercontrol.ascx" tagname="bar" %>

<foo:bar runat="server" id="blah" />

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
So no way to get it so that it I can reference it by asp.mycontrol then?
 
The problem is that my control is in another project under that solution, and doesn't have a virtual directory, how do I reference it in that case?
 
Code:
<%@Register tagprefix="foo" namespace="full.namespace.thenameofcontrol, name.of.assmebly" tagname="bar" %>

<foo:bar runat="server" id="blah" />
or something like that

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
This still leaves me with the problem of having to register every control on the actual aspx page.

What I'm trying to do, is have a gateway page with a placeholder. in the codebehind I check a querystring, and add the control based on that querystring. The controls are in a different project under the same solution.

Since there are several controls, which for all intents and purposes are my pages as well, I can't register each one on the gateway.aspx page.

 
What dll is the asp Namespace contained in?
 
there isn't a difference between hard coding the initialization a new instance in code, or declaring a new instance in markup. if you were dynamically loading the web user controls from a remote assembly, then that would be different.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
ok, well when I create a new instance of the control in the code behind, and load that in the placeholder, then try and set a textbox in the control codebehind to a value returned by a database query, it tells me the textbox control is null. How do I initialize the control hierarchy after loading the control into the placeHolder?
 
if you mean this
the Page loads a user control which contains a textbox. How can the page access the textbox of the user control.

In short, it shouldn't. the page does not require knowledge of the internals to the user control. Instead the user control should have public accessors which control its state. the page would call these public accessors.

here is a simple example of setting the text property on a user control
Code:
public class MyUserControl : UserControl
{
   ...
   public string Text
   {
      get {return textbox.Text;}
      set {textbox.Text = value;}
   }
}
Code:
void Load(eventArgs e)
{
   instanceOfMyUserControl.Text = "foo";
}

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
The actual user control itself checks for an id in a querystring. If it is greater than 0, the user control fires a method to grab data from a database, at the data layer, return the datatable, and sets the textbox value to a cell value in the datatable.


This is all done in the user control.

This is working fine when the user control is in the same project as the main page that loads the user control into a panel, using the virtual directory reference ("~/controlfolder/control.ascx").

But when the control is in a separate project, under the same solution, I have to load an instance of the control.

In this case, when an instance of the control is used, and the actual control, not the page that is loading the control, tries to set its textbox control to something, the textbox control is null.
 
The actual user control itself checks for an id in a querystring. If it is greater than 0, the user control fires a method to grab data from a database, at the data layer, return the datatable, and sets the textbox value to a cell value in the datatable.
this could lead to problems down the road. for example a new developer must know about the query string value. the querystring is related to the page, not the user control. this logic should be preformed on the page which tells the user control to load the data.

example
Code:
public void load_data(int id)
{
   if (id == 0) return;
   a_textbox.text = new data_service().get_data_by(id).a_property;
}
Code:
public void Load(EventArgs e)
{
   int id = 0;
   int.TryParse(Request.Params["query string key"], out id);
   my_user_control.load_data(id);
}
now what we have is a clear separation of concerns and encapsulated logic which follows the "tell don't ask" principle.

the page gets the value for the control, in this case, the params (form, querystring or context) and casts the value to an integer. the page tells the control to load_data.

the user control will then determine if the data should actually be fetched. doesn't care where the data comes from, it just wants an id to get data. it could come from a post, get, querystring, context, hard coded value, etc.

this doesn't apply directly to your problem just an alternative to your current setup. your problem is the control is instantiated, but it doesn't walk through the page life cycle so the controls and other page specific events are not fired. using page.loadcontrol or the declarative syntax walks it through the page life cycle.

I'm not sure where to go from here. In the past I have included the web user controls in the web project itself, Not an external project. Since then I have moved away from webforms in favor of MVC.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top