Click to See Complete Forum and Search --> : Rendering dynamic UserControls at Runtime


reach_dev
February 22nd, 2007, 05:27 AM
Hi,

I've just joined these forums today, in an effort to find a solution to my problem.

I am making a page for an administrator to view and update the access permissions for a 'group'. The permissions are stored in a database. The number of permissions will be changing in the future so I need some sort of dynamic adaptable code.

I have designed a simple UserControl consisting of a table with 4 cells.

[Permission Name] [RadioButton Deny] [RadioButton Neutral] [RadioButton Allow]

The intention was to draw as many of these as was needed, and loop through them on update to build up a new set of Group Permissions.

---

So far, I have returned from the database a DataTable of permissions and I have attempted various methods of drawing the UserControls. Firstly, I built up a large string and Respone.Wrote it where I wanted the user controls.


Code to Generate the UserControls.

DataTable dtGroupAccess = (DataTable)sp_procs.GetGroupPermissions(group_key,dbConn.c);
this.sPermissions="";
foreach(DataRow row in dtGroupAccess.Rows) {
this.sPermissions += "<uc:PermissionsItem value='"+row["permission_value"]+"' tag='"+row["permission_name"]+"' reference='"+row["permission_ref"]+"' id='permissionid_"+row["permission_key"]+"' runat='server' />";
}



Example of Created Code

<uc:PermissionsItem value='1' tag='List Groups' reference='groups_list' id='permissionid_2' runat='server' />
<uc:PermissionsItem value='1' tag='View Group' reference='group_view' id='permissionid_3' runat='server' />
<uc:PermissionsItem value='1' tag='List Group Members' reference='group_list_members' id='permissionid_4' runat='server' />
<uc:PermissionsItem value='1' tag='Edit Group Details' reference='group_edit_details' id='permissionid_5' runat='server' />
<uc:PermissionsItem value='1' tag='Edit Group Rights' reference='group_edit_rights' id='permissionid_6' runat='server' />
<uc:PermissionsItem value='1' tag='Edit Group Members' reference='group_edit_members' id='permissionid_7' runat='server' />


This didn't work - the Controls were not rendered in the browser. I'm guessing this is because the Response.Write() was the last action handled by the server. The generated code would have simply been treated by the browser as badly formed HTML gibberish.

---

My next attempt was slightly more evolved. I created a PlaceHolder control where I wanted to draw my Controls and changed my loop:


foreach(DataRow row in dtGroupAccess.Rows) {
ucEditPermissionItem permission = new ucEditPermissionItem();
permission.value = Convert.ToString(row["permission_value"]);
permission.tag = Convert.ToString(row["permission_name"]);
permission.reference = Convert.ToString(row["permission_ref"]);
permission.ID = "permissionid_"+row["permission_key"];

phPermissions.Controls.Add(permission);
}


Where phPermissions is the placeholder. This didn't work either - however if I replaced this:

phPermissions.Controls.Add(permission);
with this:
phPermissions.Controls.Add(new Textbox);

I get a load of lovely textboxes. This leads me to believe it is definatley because of my UserControl.

Can anyone offer me any advice or point me in the right direction to get this working? It's probably worth noting that if I copy the string generated in method 1 and paste it into the .aspx page at design time the desired result is achieved. I just need to find how to render them correctly.

Thanks for any help,
Dave.

TheCPUWizard
February 22nd, 2007, 05:38 AM
Looks like you are trying to generate the controls at the wrong point in the page lifecycle. Look for "ASP.Net Page LifeCycle" on MSDN and here on CodeGuru. If you are still having problems, please post a minimal yet complete sample. There are simply too many possibilities to guess from the information you provided.

reach_dev
February 22nd, 2007, 05:51 AM
Hiya,

I will look that up now. However i'm one step closer! I'm now using this to generate my UserControls.


Control permission = new Control();
permission = LoadControl("ucEditPermissionItem.ascx");
phPermissions.Controls.Add(permission);
i++;


This works in that the UserControls are now being rendered - however, using this method I can't access the UserControl properties I need to. For example, the user control has a public string called "tag" which I need to assign as this forms the contents of the UC's first cell.

This does not work:


permission.tag = "value";


It throws 'CS0117: 'System.Web.UI.Control' does not contain a definition for 'tag'' understandably since permissions is infact a Control object, not a ucEditPermissionObject.

Any thoughts?

Cheers,
Dave.

reach_dev
February 22nd, 2007, 08:15 AM
Problem beaten. Solution was VERY similar to my second attempt. I just missed out a type cast on the LoadControl method.

Solution

ucEditPermissionItem permission = (ucEditPermissionItem)LoadControl("ucEditPermissionItem.ascx");
permission.ID = "permissionid_"+row["permission_key"];
permission.Tag = Convert.ToString(row["permission_name"]);
permission.Val = Convert.ToString(row["permission_value"]);
phPermissions.Controls.Add(permission);