I'm trying to load a user control in a web service, but it doesn't execute the Load_Page().
I tried to use:
HttpContext.Current.Server.Execute(page, text, false);
But it returns a NullException. It is my code:
public string Controle(string url)
{
Page page = new Page();
UserControl userControl = (UserControl)page.LoadControl(url);
//userControl.EnableViewState = false;
HtmlForm form = new HtmlForm();
form.Controls.Add(userControl);
page.Controls.Add(form);
StringWriter text = new StringWriter();
HtmlTextWriter htmltext = new HtmlTextWriter(text);
userControl.RenderControl(htmltext);
return text.ToString();
}
Hope it is clear.
You need to initialize the control using a method that does something like
this.Load += Page_Load;
Please refer to this post which explains exactly what needs to be done to achieve this. Be sure to read the section stating; "One word of caution:"
http://weblogs.asp.net/srkirkland/archive/2007/11/05/dynamically-render-a-web-user-control.aspx
This related post will also be able to assist:
Page_Load not firing in UserControl
Related
I have some code in the Render event of my Master page that needs to be run on every postback, as it's used to translate some of the HTML content. However, the event does not seem to trigger on postback when I'm using an UpdatePanel and a ScriptManager with partial rendering set to true. Can I force the event to fire, or can I run my code elsewhere to produce the same result?
protected override void Render(HtmlTextWriter writer)
{
string originalContent = String.Empty;
string translatedContent = String.Empty;
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter htw = new HtmlTextWriter(sw))
{
base.Render(htw);
htw.Close();
originalContent = sw.ToString();
}
}
// Translate content:
translatedContent = ApplyGlobalization(Page, originalContent);
// Write updated HTML:
writer.Write(translatedContent);
}
Solved the problem by creating my own UpdatePanel with Render overridden. I put the same code into that method, but had to make some changes to adjust to the translation method to accustom to the special output that the UpdatePanel creates on partial postback.
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();
I was having some problems regarding the creation of a Page dynamically I do :
p = New Page();
Page myPage = new Page();
FormAtt ctrl = (FormAtt)myPage.LoadControl("path/to/my/file.ascx"); // here lies the gridview of evil
myPage.Controls.Add(ctrl);
Problem is i receive
Control ... must be placed inside a form tag with runat=server
Ok, so I've found out that I Need to override the VerifyRenderingInServerForm method to be able to call a formless page, But how can i override VerifyRenderingInServerForm since i don't have a ASPX file.
ps: I have a related question and I don't know what to do, since they are different questions but the solution goes to the same problema , and I gave up on the last solution - see : Form is Null in Dynamically created Pages
You can try to use a custom class which already overrides VerifyRenderingInServerForm:
public partial class MyCustomPage : System.Web.UI.Page
{
public override void VerifyRenderingInServerForm(Control control)
{
}
protected void Page_Load(object sender, EventArgs e)
{
var p = new MyCustomPage();
FormAtt uc = (FormAtt)p.LoadControl("path/to/my/file.ascx");
p.Controls.Add(uc);
}
}
Similar to your previous question. You need to add HtmlForm first. ASP.Net need a form tag in order to add controls to a page.
Page myPage = new Page();
HtmlForm form = new HtmlForm();
FormAtt ctrl = (FormAtt)myPage.LoadControl("path/to/my/file.ascx");
form.Controls.Add(ctrl);
myPage.Controls.Add(form);
i do it to render a component
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
Page p = new Page();
System.Web.UI.HtmlControls.HtmlForm form = new System.Web.UI.HtmlControls.HtmlForm();
p.Controls.Add(form);
form.Controls.Add(gv);
form.RenderControl(htmlWrite);
string j = stringWrite.ToString();
I need to load a silverlight application in a portion of an aspx page on a button click on that page page. Some init parameters need to be passed to the silverlight application based on the user inputs on the host page on button click. How to do that?
I presume I need to create the silverlight object from code-behind to set custom InitParameters. Any idea how to do that?
Extending to what is mentioned here, you can do something like this:
HtmlGenericControl myHtmlObject = new HtmlGenericControl("object");
myHtmlObject.Attributes["data"] = "data:application/x-silverlight-2";
myHtmlObject.Attributes["type"] = "application/x-silverlight-2";
myHtmlObject.Attributes["width"] = "100%";
myHtmlObject.Attributes["height"] = "100%";
this.Page.Controls.Add(myHtmlObject);
HtmlGenericControl mySourceParam = new HtmlGenericControl("param");
mySourceParam.Attributes["name"] = "source";
mySourceParam.Attributes["value"] = "ClientBin/MySilverlightApplication.xap";
myHtmlObject.Controls.Add(mySourceParam);
HtmlGenericControl myOnErrorParam = new HtmlGenericControl("param");
myOnErrorParam .Attributes["name"] = "onError";
myOnErrorParam .Attributes["value"] = "onSilverlightError";
myHtmlObject.Controls.Add(myOnErrorParam);
HtmlGenericControl myInputParam = new HtmlGenericControl("param");
myOnErrorParam .Attributes["name"] = "InitParameters";
myOnErrorParam .Attributes["value"] = "param1=Hello,param2=World";
myHtmlObject.Controls.Add(myInputParam);
this.Page.Controls.Add(myHtmlObject);
I'm trying to load a user control in code and store the output as a string variable. At the moment I've got the below code:
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
WidgetControlBase widget = (WidgetControlBase)LoadControl("/controls/mycontrol.ascx");
widget.RenderControl(htw);
The problem is that the RenderControl method doesn't fire any of the control events so anything I add in the control Page_Load doesn't happen.
I've tried loading the control in a PlaceHolder and then rendering the PlaceHolder like so:
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
WidgetControlBase widget = (WidgetControlBase)LoadControl("/controls/mycontrol.ascx");
myPlaceholder.Controls.Add(widget);
myPlaceholder.RenderControl(htw);
However this seems to do the same as just using RenderControl direct on the control, it doesn't fire any events either.
Is there a way I can get a string containing the contents of my control while still firing the control events?
I think you are trying to render the control at the wrong point in the pages life cycle. Try moving the code from Page_Load to PreInit like so:
protected override void OnPreInit(EventArgs e)
{
//custom code
base.OnPreInit(e);
}
Detailed information about the page life cycle can be found here:
http://www.codeasp.net/articles/asp.net/20/aspnet-page-lifecycle