How call GetWebResourceUrl() when Page is null ? - c#

I built custom ASP.NET control and it's working fine when I add it manually (drag and drop) or by code to controls in markup.
The custom control, MsgBox, had resources like JavaScript, CSS and images embedded in and the problem appeared when I tried to Render the control in class to return its HTML code, the Page instance is null and the "GetWebResourceUrl" needs it:
Page.ClientScript.GetWebResourceUrl(.....)
is there any way to get the resourceurl ? Here is my render code:
protected override void RenderContents(HtmlTextWriter writer)
{
using (PlaceHolder plh = new PlaceHolder())
{
if (Page != null)
{
if (DesignMode || Page.Header == null)
RegisterCSSInclude(plh);
}
HtmlGenericControl container = new HtmlGenericControl("div");
container.EnableViewState = false;
container.InnerHtml = "Control html code";
plh.Controls.Add(container);
plh.RenderControl(writer);
}
}
RegisterCSSInclude is method to register my css files:
private void RegisterCSSInclude(Control target)
{
// CSS
bool linkIncluded = false;
foreach (Control c in target.Controls)
{
if (c.ID == "MsgBxStyle")
{
linkIncluded = true;
}
}
if (!linkIncluded)
{
HtmlGenericControl globalCsslink = new HtmlGenericControl("link");
globalCsslink.ID = "MsgBxGStyle";
globalCsslink.Attributes.Add("href", Page.ClientScript.GetWebResourceUrl(typeof(MessageBoxCtrl), "MessageBox.MsgBxStyles.WeDevMsgBox.css"));
globalCsslink.Attributes.Add("type", "text/css");
globalCsslink.Attributes.Add("rel", "stylesheet");
globalCsslink.EnableViewState = false;
target.Controls.Add(globalCsslink);
HtmlGenericControl csslink = new HtmlGenericControl("link");
csslink.ID = "MsgBxStyle";
csslink.Attributes.Add("href", Page.ClientScript.GetWebResourceUrl(typeof(MessageBoxCtrl), "MessageBox.MsgBxStyles." + Style.ToString().ToLower() + ".css"));
csslink.Attributes.Add("type", "text/css");
csslink.Attributes.Add("rel", "stylesheet");
csslink.EnableViewState = false;
target.Controls.Add(csslink);
}
}
Update:
PS: I'm tring to use control in generic handler (ashx) where I call ShowMsgBox method which is a method in a class and not in a page or user control.
ShowMsgBox method should create an instance of MsgBox control and render it then return the html code to ashx class :
var htmlCode = MyClass.ShowMsgBox("myMsg");
context.Response.write(htmlCode);

I built a custom ASP.NET control ...
I'm tring to use control in generic handler (ashx) ... not in a page or user control.
A Page is a handler. You want to use a convenience provided by the Page class, but you don't want to inherit from Page. The niceties of Page, such as ClientScript, expect a Page from which to get various information.
You can provide a dummy Page object to your control by setting the Page property of your custom Control:
this.Page = new Page();
...then you will need to set various properties (assuming they are public) which are expected by ClientScriptManager.GetWebResourceUrl():
this.Page.Foo = "bar";
then you can call:
this.Page.ClientScript.GetWebResourceUrl(...);

If this is the specific class that inherits from WebControl page shouldn't be null. If its another class that you are rendering as part of a hierarchy you can add a parameter of type Page and pass the reference of the current page to it.

Related

Cache Controls loaded at Runtime

My question is similar to Access to PartialCachingControl.CachedControl before Add it but since i cannot add the control to page before setting control attributes im still having problems
I have a UserControl with a public property called "Content" which i would like to cache. On the UserControl ive added:
<%# OutputCache Duration="60" VaryByParam="none" %>
Before adding the Cache Attribute i used to load the controls as followed
public static Control DocumentWidget (System.Web.UI.Page currentPage, Comito.LokalPortalen.Domain.Entity.CMS.Content.Content content)
{
Comito.LokalPortalen.FrontEndShared.Controls.Document.Widget documentWidget = (FrontEndShared.Controls.Document.Widget)currentPage.LoadControl("/FrontEndShared/Controls/Document/Widget.ascx");
if (documentWidget != null)
{
documentWidget.Content = content;
return documentWidget;
}
return null;
}
I would now like to do something like :
PartialCachingControl documentWidget = (PartialCachingControl)currentPage.LoadControl("/FrontEndShared/Controls/Document/Widget.ascx");
if (documentWidget != null)
{
System.Reflection.PropertyInfo cmsContent = documentWidget.GetType().GetProperty("Content");
documentWidget.Content = content;
return documentWidget;
}
Which fails with "PartialCachingControl doesnt contain a definition for "Content"
or like the solution supposed in Access to PartialCachingControl.CachedControl before Add it but since i cant add the control before setting attributes this doesnt Work.
Any solution

Artificially firing Page Events in ASP.NET?

I'm working with a static class in C#, and am trying to use Control.RenderControl() to get a string / mark-up representation of a Control.
Unfortunately the control (and all child controls) use event bubbling to populate certain values, for example, when instantiating, then calling RenderControl() on the following:
public class MyTest : Control
{
protected override void OnLoad(EventArgs e)
{
this.Controls.Add(new LiteralControl("TEST"));
base.OnLoad(e);
}
}
I am returned an empty string, because OnLoad() is never fired.
Is there a way I can invoke a 'fake' page lifecycle? Perhaps use some dummy Page control?
I was able to accomplish this by using a local instance of Page and HttpServerUtility.Execute:
// Declare a local instance of a Page and add your control to it
var page = new Page();
var control = new MyTest();
page.Controls.Add(control);
var sw = new StringWriter();
// Execute the page, which will run the lifecycle
HttpContext.Current.Server.Execute(page, sw, false);
// Get the output of your control
var output = sw.ToString();
EDIT
If you need the control to exist inside a <form /> tag, then simply add an HtmlForm to the page, and add your control to that form like so:
// Declare a local instance of a Page and add your control to it
var page = new Page();
var control = new MyTest();
// Add your control to an HTML form
var form = new HtmlForm();
form.Controls.Add(control);
// Add the form to the page
page.Controls.Add(form);
var sw = new StringWriter();
// Execute the page, which will in turn run the lifecycle
HttpContext.Current.Server.Execute(page, sw, false);
// Get the output of the control and the form that wraps it
var output = sw.ToString();

ASP.NET How to access a deeply nested user control on the parent page

I have a login control and at is nested 2 deep in a header control
i.e Page --> Header Control --> Login Control. I cannot get a reference to the control on the page using FindControl. I want to be able to set the visible property of the control like
if (_loginControl != null)
_loginControl.Visible = false;
I ended up using a recursive FindControl method to find the nested control.
public static Control FindControlRecursive(Control root, string id)
{
if (root.ID == id)
{
return root;
}
foreach (Control c in root.Controls)
{
Control t = FindControlRecursive(c, id);
if (t != null)
{
return t;
}
}
return null;
}
Are you needing to disable/hide the User Control from the ASP.NET page it resides on (or does the User Control exist on a master page, say)? If it's in the same page, then in your ASP.NET page's code-behind you'd do:
MyUserControlsID.Visible = false
Where MyUserControl is the ID of your User Control. To determine the ID of your User Control look at the markup of your .aspx page and you will see something like this:
<uc1:UserControlName ID="MyUserControlsID" runat="server" ... />
Happy Programming!
A good way would be to use:
Page.FindControl()
if that yields null, the control is not there.
Try calling this.FindControl("_loginControl") or this.Page.FindControl("_loginControl").
See MSDN for method details:
http://msdn.microsoft.com/en-us/library/system.web.ui.control.findcontrol.aspx
The login control, if it's registered in the markup, will also be an instance member of your codebehind page; you can refer to it from the codebehind class as if it were a normal member, using the same name you provided as the ID (I do recommend using codebehinds for most logic, instead of inlining code in the markup, BTW).
You can also use the FindControl() method of your page, which will search its control subtree for a control with a given ID. That takes longer, so I would recommend the first option unless the logic control is added dynamically and you don't always know it's there.
private List<Control> GetAllNestedUserControl(Control ph)
{
List<Control> Get = new List<Control>();
foreach (var control in ph.Controls)
{
if (control is UserControl)
{
UserControl uc = control as UserControl;
if (uc.HasControls())
{
Get = GetAllNestedUserControl(uc);
}
}
else
{
Control c = (Control)control;
if (!(control is LiteralControl))
{
Get.Add(c);
}
}
}
return Get;
}
just call this code from you any parent page and then get any control by the following code
List<Control> Get = GetAllNestedUserControl(ph);
Label l = (Label)Get.Find(o => o.ID == "lblusername");
l.Text = "changed from master";

How to get the page calling the usercontrol

I have a user control .Is there some way to get the page in which usercontrol is available ?
In your usercontrol write this method
protected void MyMethod()
{
Page myParent = this.Page;
...
}
You want the Page property.
If you need to write back to a page of a certain type, you'll have to cast:
var myUserPage = Page as MyCustomUserPageClass;
if (myUserPage != null) {
myUserPage.Foo = "bar";
}

Using Build Manager Class to Load ASPX Files and Populate its Controls

I am using BuildManager Class to Load a dynamically generated ASPX File, please note that it does not have a corresponding .cs file.
Using Following code I am able to load the aspx file, I am even able to loop through the control collection of the dynamically created aspx file, but when I am assigning values to controls they are not showing it up. for example if I am binding the value "Dummy" to TextBox control of the aspx page, the textbox remains empty.
Here's the code that I am using
protected void Page_Load(object sender, EventArgs e)
{
LoadPage("~/Demo.aspx");
}
public static void LoadPage(string pagePath)
{
// get the compiled type of referenced path
Type type = BuildManager.GetCompiledType(pagePath);
// if type is null, could not determine page type
if (type == null)
throw new ApplicationException("Page " + pagePath + " not found");
// cast page object (could also cast an interface instance as well)
// in this example, ASP220Page is a custom base page
System.Web.UI.Page pageView = (System.Web.UI.Page)Activator.CreateInstance(type);
// call page title
pageView.Title = "Dynamically loaded page...";
// call custom property of ASP220Page
//pageView.InternalControls.Add(
// new LiteralControl("Served dynamically..."));
// process the request with updated object
((IHttpHandler)pageView).ProcessRequest(HttpContext.Current);
LoadDataInDynamicPage(pageView);
}
private static void LoadDataInDynamicPage(Page prvPage)
{
foreach (Control ctrl in prvPage.Controls)
{
//Find Form Control
if (ctrl.ID != null)
{
if (ctrl.ID.Equals("form1"))
{
AllFormsClass cls = new AllFormsClass();
DataSet ds = cls.GetConditionalData("1");
foreach (Control ctr in ctrl.Controls)
{
if (ctr is TextBox)
{
if (ctr.ID.Contains("_M"))
{
TextBox drpControl = (TextBox)ctr;
drpControl.Text = ds.Tables[0].Rows[0][ctr.ID].ToString();
}
else if (ctr.ID.Contains("_O"))
{
TextBox drpControl = (TextBox)ctr;
drpControl.Text = ds.Tables[1].Rows[0][ctr.ID].ToString();
}
}
}
}
}
}
}
I saw that you got part of your code from How To Dynamically Load A Page For Processing. Read the comments too as this one by Mike.
Invert this:
((IHttpHandler)pageView).ProcessRequest(HttpContext.Current);
LoadDataInDynamicPage(pageView);
To this:
LoadDataInDynamicPage(pageView);
((IHttpHandler)pageView).ProcessRequest(HttpContext.Current);
In this case changing the order of the calls does change the end result I think. The inverse of Commutativity property. :)

Categories

Resources