Difference with creating and adding controls in PreInit Init - asp.net

There's tons of info on the web about the ASP.NET life cycle, but i can't seem to figure out when to dynamically add controls to the page.
In general there are two situations; an aspx page with a masterpage, and one without. The book i'm currently reading (70-515 self prep) says to add controls to a page without a master page in the preinit eventhandler. To dynamically add controls to a contentpage, i should place that logic in the init eventhandler.
According to MSDN (http://msdn.microsoft.com/en-us/library/ms178472.aspx) I should create or recreate dynamic controls in the preinit eventhandler, and only read or initialize properties of controls in the init eventhandler (which makes most sense to me). Googling around I see lots of people using the init eventhandler to add controls.
So, i'm a little bit lost here - what is the correct way? And when using the preinit eventhandler, how could you add controls to your page when all controls are null? For instance, when you need to add a dynamically created textbox to a panel control?
Kind regards,

Unless you need to play around with setting control properties prior to tracking ViewState, I would personally go ahead and place my dynamic control addition logic in the OnInit event.
If you really want to dynamically add a control during the PreInit (when using master page) you can always do something like this:
protected override void OnPreInit(EventArgs e)
{
base.OnPreInit(e);
TextBox textBox = new TextBox();
textBox.Text = "Dynamic TextBox";
textBox.Width = 100;
textBox.ReadOnly = false;
var master = this.Master;
plcHolder.Controls.Add(textBox);
textBox.ApplyStyleSheetSkin(this.Page);
}
accessing the "Master" property would instantiate the controls
and it should work, but you get nested master pages scenarios (this.Master.Master ...), update panels and so on.
This might be relevant and helpful: http://weblogs.asp.net/ysolodkyy/archive/2007/10/09/master-page-and-preinit.aspx
Moreover, one reason I can think of (besides following the defined page lifecycle) MS recommends that we place all the logic for dynamic control creation in the Preinit event so we can take advantage of the theme service, which will apply all available skin properties automatically for us, before the Init event takes place.
Say your markup looks something like that:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Trace="true" Inherits="_Default" Theme="Test" %>
...
<form id="form1" runat="server">
<div>
<p>
<asp:TextBox ID="TextBox1" runat="server" TextMode="Password" Text="Control TextBox"></asp:TextBox>
</p>
<p>
<asp:PlaceHolder ID="plcHolder" runat="server"></asp:PlaceHolder>
</p>
</div>
</form>...
and you have a skin like this:
<asp:TextBox runat="server" BackColor="Yellow" Wrap="false" Text="Skin property!" > </asp:TextBox>
Just add this to your code behind:
private TextBox tb1;
protected override void OnPreInit(EventArgs e)
{
base.OnPreInit(e);
tb1 = new TextBox();
tb1.Text = "PreInit Dynamic TextBox";
Trace.Write(String.Format("tb1 Wrap Property-> {0}",tb1.Wrap));
Trace.Write(String.Format("tb1 Text Property-> {0}", tb1.Text));
Trace.Write("Add tb1 to the placeholder.");
plcHolder.Controls.Add(tb1);
Trace.Write(String.Format("tb1 Wrap Property-> {0}", tb1.Wrap));
Trace.Write(String.Format("tb1 Text Property-> {0}", tb1.Text));
}
protected override void OnInit(EventArgs e)
{
Trace.Write(String.Format("tb1 Wrap Property-> {0}", tb1.Wrap));
Trace.Write(String.Format("tb1 Text Property-> {0}", tb1.Text));
base.OnInit(e);
}
protected void Page_Load(object sender, EventArgs e)
{
Trace.Write(String.Format("tb1 Wrap Property-> {0}", tb1.Wrap));
Trace.Write(String.Format("tb1 Text Property-> {0}", tb1.Text));
}
You will notice how before going into the Init event all skin properties are already applied to the dynamically created textbox :)

The PreInit event was new to me, but I guess it makes sense, so that you have an intermediary step in between the loading of controls and viewstate load to do additional work. We've used init event to load dynamic controls and this has always worked for us with no issues. I think you'll be OK with either, but if MS recommends PreInit, I'd say go that route. THis way, in Init, you can do any additional work you may need and separate out the routine that creates the UI vs. the routine that may update it before viewstate load.
HTH.

Related

Create Dynamic Controls in PreInit Event of Page Life Cycle?

MSDN says that create Dynamic Controls in PreInit Event of Page Life Cycle.
http://msdn.microsoft.com/en-us/library/ms178472.aspx
Why?
What advantage is derived by creating in PreInit Event?.
I have seen code where developers are creating dynamic controls in the Page_Load Method?
If there any difference?.
Regards
Page_Load works fine if you don't need to worry about saving the controls' ViewState across postbacks, but if you need to persist it, the Load stage is not where you should add these controls.
Dynamic controls must exist within the page's control hierarchy before the ViewState is loaded. There's only one stage before Load View State - Initialization. That means, if you want your dynamic controls to persist view state you must add them to the control hierarchy in the page's Init event.
https://web.archive.org/web/20210302172017/https://aspnet.4guysfromrolla.com/demos/printPage.aspx?path=/articles/092904-1.aspx
But mind that you cannot access the ViewState in Init event because it's yet not loaded. So you need to use a different persistence medium to store variables across postbacks(like Session) if required.
This reply might be late for the original poster but I thought it might help some other people.
If your application/ website doesn't use master page, it's best to create controls at Page_PreInit event. But if you use master page and want to create controls on content pages at run time Page_Init is the ideal event.
You can also create controls on Page_Load but bear in mind page load is fired after View State is loaded.
Tim,
Thanks for the reply.
I did a small experiment in which I am creating and adding a TextBox control dynamically in Page_Load method.
In the Postback click event of Button on the page,I am trying to get the value of the TextBox's Text Property.
I am able to get the value in the click event of the Button when the control is dynamically added in the Page_Load event and not the OnPreInt method.
I think that the Text value is retained in the ViewState even though the control is been added in Page_Load method.
following the code:
<div>
<p>
<asp:Label ID="lbl" runat="server" />
</p>
<p>
<asp:PlaceHolder ID="plcHolder" runat="server"></asp:PlaceHolder>
</p>
<p>
<asp:Button ID="btn" runat="server" Text="Click" OnClick="btn_Click" />
</p>
</div>
private void _createTextBox()
{
TextBox textBox = new TextBox();
textBox.ID = "txtBox";
textBox.Width = 250;
textBox.ReadOnly = false;
plcHolder.Controls.Add(textBox);
}
protected void Page_Load(object sender, EventArgs e)
{
_createTextBox();
if (!this.IsPostBack)
{
Control ctrl = plcHolder.FindControl("txtBox");
if (ctrl != null)
{
TextBox txtBox = ctrl as TextBox;
txtBox.Text = DateTime.Now.ToString();
}
}
}
protected void btn_Click(object sender, EventArgs e)
{
Control ctrl = plcHolder.FindControl("txtBox");
if (ctrl != null)
{
TextBox txtBox = ctrl as TextBox;
lbl.Text = txtBox.Text;
}
}
Kindly let me know is this correct or what am i doing wrong?

UpdatePanel with dynamic controls won't postback

I am using UpdatePanel and loading dynamically the content:
<asp:UpdatePanel runat="server" ID="updatePanel" UpdateMode="Conditional">
<ContentTemplate>
<div id="dashboard-main">
<div id="dashboard-content">
<div class="d-content">
<asp:PlaceHolder runat="server" ID="ContentPanel">
</asp:PlaceHolder>
</div>
</div>
<div id="sub-content">
<ul id="nav-02">
<li class="current">
<asp:LinkButton CommandArgument="Stuff" OnClick="LoadMiniDash" runat="server" ID="myStuff">
My Stuff</asp:LinkButton></li>....
What I do is once the LinkButton is pressed, I load a control to the ContentTemplate:
protected void LoadMiniDash(object sender, EventArgs e)
{
string miniDashName = ((LinkButton)sender).CommandArgument;
Control control = Page.LoadControl("~/controls/Dashboard" + miniDashName + ".ascx");
ContentPanel.Controls.Clear();
ContentPanel.Controls.Add(control);
updatePanel.Update();
}
My problem is, that inside this newly loaded control, there is another Button. once clicked, the updatepanel gets emptied and not postback to the control is made. (it does however do a post to the page itself async. but i see the control won't reach its own code once clicked).
what can be the problem?
thanks
I just tried to rewrite your question. When you load your userControl it appears in the updatePanel. When you hit the button in the userControl, your updatePanel gets updated - and your userControl "disappears".
Imo it disappears because you do not reload your userControl. I tried the following snippet:
protected void btnLoadControl_Click(object sender, EventArgs e)
{
/// save the name of the userControl to load in a SessionVar
Session["userControl2Load"] = "ucDemo.ascx";
Control control = LoadControl("ucDemo.ascx");
phDemo.Controls.Clear();
phDemo.Controls.Add(control);
upDemo.Update();
}
/// reload your userControl on every init of the updatePanel
/// when the sessionVar is set
protected void upDemo_Init(object sender, EventArgs e)
{
if (Session["userControl2Load"] != null)
{
string controlName = Session["userControl2Load"].ToString();
Control control = LoadControl(controlName);
phDemo.Controls.Clear();
phDemo.Controls.Add(control);
}
Debug.WriteLine("upDemo Init");
}
it worked for me. Let me know if it work for you too. hth
I have faced similar issue.. and you know solution to this problem is very simple..
Logic is .. If you have one or more update panel in the same page then properties of all update panel should be same such as
Update Mode : Always
EnableViewState : true(if you are using it)
For example you have 3 update panel in the same asp.net ajax page with following id
UpdatePanel1
UpdatePanel2
UpdatePanel3
In the Update Panel 1 (UpdatePanel1) you have a button which specific a record from the gridview. On the click on the button in the update panel1, FormView in the UpdatePanel2 should display the corresponding values.
To achieve this, you need to specific Update Panel Properties of (UpdatePanel1, UpdatePanel2) as
ChildrenAsTrigger : True
EnableViewState : true
RenderMode : Block/Inline
UpdateMode : Always
Visible : true
Doing above activity values in the UpdatePanel3 is changed or you can say it's not depending on the UpdatePanel1 or UpdatePanel2
If you find it useful, please mark it as your answer else let me know...

Getting posted-back TextBox.Text in Page_Load?

I've a asp:TextBox and a submit button on my asp.net page. Once the button was clicked, the TextBos's value is posted back. I'm going to keep the the posted-back text value into session, so that other child controls can access to the value during their Page_Load. However, I always get NOTHING ("") in the Page_Load method, and I can read the text out in the button click handler. I know that the "button click event" happens after the Page_Load. So, I'm asking how can I "pre-fetch" the TextBox.text during Page_Load?
public partial class form_staffinfo : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e){
string s = staff_id.Text; //Reach this line first, but GET empty value. However, I want to keep it in the session during this moment.
}
protected void btn_submit_Click(object sender, EventArgs e) {
string s = staff_id.Text; //Reach this line afterward, value got.
}
}
-- EDITED --
<%# Control Language="C#" AutoEventWireup="true" CodeFile="form_staffinfo.ascx.cs" Inherits="form_staffinfo" %>
<asp:Label ID="Label1" runat="server" Text="Staff ID: "></asp:Label>
<asp:TextBox ID="staff_id" runat="server" ></asp:TextBox>
<asp:Button ID="btn_submit" runat="server" Text="Query" OnClick="btn_submit_Click" />
Since I can't get the TextBox's text in the Page_Load, so, I didn't include any code related to session for clear presentation.
Thank you!
William
None of the values of your server controls are available for consumption in the Page_Load. Those controls are assigned after the form is validated (which is after the form is loaded) and before the form's control's events fire (like button clicks, in your example). The values posted are in the Request.Form Collection. Look in the AllKeys property and you should see a key that ends in $staff_id if you use your example posted. There may be other characters in from of the key, depending upon if the control is nested in a master page or other control.
If you absolutely must have that value at page load, grab it from the Request.Form collection instead of the user control, but I would question the wisdom of capturing the value that early in the page lifecycle. You could conceivably capture the textbox's OnTextChanged Event if you needed to preserve the value in Session.
EDIT - Additional Explanation
if you were going to create a custom event for your user control, there are only a couple of steps to it.
Create a delegate. This is will be the common object for inter-control messaging.
public delegate void StaffIdChangedEvent(object sender, string staffId);
Declare an event using that delegate in the user control that is going to broadcast.
public event StaffIdChangedEvent StaffIdChanged;
In your user control, when you are ready to broadcast (say from the Staff_id textbox's OnTextChanged event), you just invoke the event [Its generally a best practice to check to see if the event is null]
this.StaffIdChangedEvent(this, "staff-id-value-here");
The final step is to wire the user control event up to an event handler (this prevents the null situation I mentioned above when trying to invoke the event). You could wire a handler into the hosting page.
this.form_staffinfo.StaffIdChangedEvent += this.some_method_on_page;
Just make sure the method on the page has the same method signature as the delegate used to declare the event.
Events also could be wired into each control that needs to know about them (look up multicast delegates), so you could do something like:
this.form_staffinfo.StaffIdChangedEvent += this.some_method_on_page;
this.form_staffinfo.StaffIdChangedEvent += this.some_control_on_the_page;
this.form_staffinfo.StaffIdChangedEvent += this.some_other_control_on_the_page;
In any event, I generally preferred to do this type of wiring in the page's OnInit method.
override protected void OnInit(EventArgs e)
{
base.OnInit(e);
InitializeComponent();
}
and just write your own InitializeComponent method to centralize any of this wiring you have to do.
There is something else that is setting the textbox value. Could you please check if you are overriding other event that occurs before Page_Load and modifying the textbox text property. Even, posting the code where you update session variable would be handy. From the code you have posted, it should work.
Do you have autoeventwireup disabled? I could be mistaken, but I think if it is disabled your Page_Load will not fire. If you want to leave it disabled, you can always override the OnLoad event...
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
// do stuff
}

CheckedChanged EventHandler of dynamically added Checkboxes not firing inside UpdatePanel of a Repeater

I´ve read most posts here but i can´t figure out why the "CheckedChanged" Event is not firing. Here is my situation.
I´m using a Repeater to generate Items out of a Database. Each ReapeaterItem should include an UpdatePanel, because i have to Update the Controls inside the UpdatePanel and do not want to reload the complete page. Inside these dynamically generated UpdatePanels (each RepeaterItem has one) i´m adding up to three Checkboxes dynamically (based on the Database). These Checkboxes need to fire the "CheckedChanged" event, because on some conditions i want to enable/disable/check/uncheck Checkbox1, 2 or 3 based on business logic. ... Hope you got this so far. I´m adding all Controls and have the EventHandler Added. But the generated Code does not reflect the Event Handler. I tried OnItemDataBound, OnItemCreated, PreRender, ... Events to add the Eventhandler too, but i was not able to find the CheckBox-Control with the ID.
I´m totally lost with this and on the way to use Buttons instead of Checkboxes. From what i read so far is that with Buttons i can use the CommandName from the Button and the ItemCommand-Event from the Repeater to get a workaround, but then i need to reflect the "Check" on the Page in some way.
btw, every Repeater (8) sits inside an ajaxtoolkit-accordion control.
Here i give you some Code:
aspx-Page
<asp:Repeater ID="RepeaterAccordionPane2" runat="server">
<ItemTemplate>
HTML Stuff<%# DataBinder.Eval(Container.DataItem, "Header")%>HTML Stuff<%# DataBinder.Eval(Container.DataItem, "Beschreibung")%></td>
<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode=Conditional>
<ContentTemplate>
</ContentTemplate>
</asp:UpdatePanel>
HTML Stuff
</ItemTemplate>
</asp:Repeater>
Here is the Page_Load Part
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
dvAlleArtikel = new System.Data.DataView(...Database...);
[... some other code here ...]
RepeaterAccordionPane2.DataSource = dvAlleArtikel;
//RepeaterAccordionPane2.ItemCreated +=new RepeaterItemEventHandler(RepeaterAccordionPane2_ItemCreated);
//RepeaterAccordionPane2.PreRender +=new EventHandler(RepeaterAccordionPane2_PreRender);
RepeaterAccordionPane2.DataBind();
int nUpdatePanelIndex = 0;
foreach (Control crInRepeater in RepeaterAccordionPane2.Controls)
{
if (crInRepeater.GetType() == typeof(RepeaterItem))
{
foreach (Control crInRepeaterItem in crInRepeater.Controls)
{
if (crInRepeaterItem.GetType() == typeof(UpdatePanel))
{
LiteralControl litTabelleBeginn = new LiteralControl("<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"2\">");
((UpdatePanel)crInRepeaterItem).ContentTemplateContainer.Controls.Add(litTabelleBeginn);
if (dvAlleArtikel[nUpdatePanelIndex]["ArtNr1"].ToString() != "0")
{
CheckBox CheckBox1 = new CheckBox();
CheckBox1.ID = dvAlleArtikel[nUpdatePanelIndex]["ArtNr1"].ToString();
CheckBox1.Text = (dvAlleArtikel[nUpdatePanelIndex]["CheckBoxLbl1"].ToString() == "" ? "leer" : dvAlleArtikel[nUpdatePanelIndex]["CheckBoxLbl1"].ToString());
CheckBox1.AutoPostBack = true;
CheckBox1.CheckedChanged +=new EventHandler(CheckBox1_CheckedChanged);
LiteralControl litNeueTabellenZeileBeginn = new LiteralControl("<tr><td width=10><img src=\"images/helper/spacer.gif\" width=\"10\"></td><td height=\"20\">");
LiteralControl litNeueTabellenZeileEnde = new LiteralControl("</td><td width=\"100\" height=\"20\">" + dvAlleArtikel[nUpdatePanelIndex]["ArtPrice1"].ToString() + " € </td></tr>");
((UpdatePanel)crInRepeaterItem).ContentTemplateContainer.Controls.Add(litNeueTabellenZeileBeginn);
((UpdatePanel)crInRepeaterItem).ContentTemplateContainer.Controls.Add(CheckBox1);
((UpdatePanel)crInRepeaterItem).ContentTemplateContainer.Controls.Add(litNeueTabellenZeileEnde);
}
[... some other code here...]
LiteralControl litTabelleEnde = new LiteralControl("</table>");
((UpdatePanel)crInRepeaterItem).ContentTemplateContainer.Controls.Add(litTabelleEnde);
nUpdatePanelIndex++;
}
}
}
}
This code is never reached:
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
int foo = 0;
}
This is the CheckBox-Code generated:
<input id="AccordionPane2_content_RepeaterAccordionPane2_ctl00_6200" type="checkbox" name="AccordionPane2_content$RepeaterAccordionPane2$ctl00$6200" onclick="javascript:setTimeout('__doPostBack(\'AccordionPane2_content$RepeaterAccordionPane2$ctl00$6200\',\'\')', 0)" />
The Event is generated, but when i click the Checkbox all Content in the UpdatePanel is gone and the CheckedChanged-EventHandler is not fired.
What am i doing wrong?
Thanks to all advice, i´m really stuck.
mk
The first time the page loads you are adding all the checkboxes to the Controls collection, and they get rendered. When you do a postback (through the CheckBox's AutoPostBack) you have a check if(!IsPostBack) that doesn't allow the checkboxes to be added to the Controls collection on the postback. Because of that, you won't see the controls and the page, and when the page lifecycle tries to call the events (which occurs AFTER Page_Load), the controls that created the events are no longer there.
You will need to refactor your Page_Load method so it does two things - 1, regardless of the value of IsPostBack bind the repeaters and create the dynamic controls. 2, if IsPostBack==false, i.e., an initial load, then set the values of the dynamic controls. you don't want to set the values of the dynamic controls when IsPostBack==true because then you will lose the values the user entered.
also, just a note:
if (crInRepeater.GetType() == typeof(RepeaterItem))
can be rewritten as:
if (crInRepeater is RepeaterItem)

UserControl with UpdatePanel programmatically create ScriptManager possible?

I'd like to use an UpdatePanel in my UserControl. The problem is the .aspx page the control is dropped on will NOT have a ScriptManager. I will need to create the ScriptManager in the UserControl. However if the UserControl is used, say twice on the page, then placing a ScriptManager won't work because you can only initialize ScriptManager once.
In other UserControls where I needed ScriptManager (I was using AJAX Toolkit Extensions) I was able to use this code:
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
Page.Init += new EventHandler(Page_Init);
}
void Page_Init(object sender, EventArgs e)
{
if (Page.Form != null && ScriptManager.GetCurrent(Page) == null)
Page.Form.Controls.AddAt(0, new ScriptManager());
}
..worked great, but not for the UpdatePanel case.
Note, I am NOT using Master Pages
(Another approach I was thinking of, but can't figure out how to do, is to programmatically create the UserControl inside an UpdatePanel.)
I do not think this is possible. I have tried it several different ways. You might have to bite the bullet and put a scriptmanager in your page.
What is the reason it doesn't work? Are you getting an exception from the UpdatePanel that a ScriptManager is required? Are you using System.Web.Extensions 1.0 or 3.5? I say that because a change was made to UpdatePanel in 3.5 that causes its content template to instantiate prior to OnInit, so I don't see an obvious reason why that wouldn't work. If there is an exception it would be helpful to see the stack trace.
I am hitting this same problem. The problem is that you need to add the scriptmanager before the OnInit stage - as far as I can see it needs to be done at the preinit stage. You can see this by adding a load of overrides - I found the the page got through the preinit ok, then went to the addedcontrol event and it was at (or just after, but this point makes sense) that the "You need a scriptmanager" gets thrown. I am struggling to find how to add an event handler to the Page.PreInit event from a child usercontrol as the WUCs don't have a PreInit event. Even the WUC constructor doesn't fire before that point and in the constructor the page object is null so you can't add it there. Even at the AddedControl stage of the WUC, you still don't seem to be able to access the main page ( ScriptManager oSCM = ScriptManager.GetCurrent(Page); returns null) so you can't seem to add the scriptmanager, if you need to, before the error is thrown.
/edit:-
As far as I can see it (and I've had no answer to this on the asp.net forums - surprise, surprise) the WUC doesn't start kicking in it's methods/events until after the parent's preinit stage, so there's 2 ways of doing this.
1) The way I think I would do this is to not put any content in the designer that requires the scriptmanager and to put placeholders where such content needs to go. Then in the wuc load you use the ScriptManager.GetCurrent to see if there's one already there and then create it if not. Then you dynamically add the content that requires the SCM. Something like this:-
<%# Control Language="C#" AutoEventWireup="true" CodeFile="wucTestExample.ascx.cs" Inherits="wucTestExample" %>
<asp:PlaceHolder ID="plcAJAX" runat="server" />
<asp:Label ID="lblGeneral" runat="server" Text="This is another label" />
----------------code behind---------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class wucTestExample : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
ScriptManager oSCM = ScriptManager.GetCurrent(this.Page);
if (oSCM == null)
{
oSCM = new ScriptManager();
oSCM.ID = "scmAJAX";
oSCM.EnablePartialRendering = true;
this.plcAJAX.Controls.AddAt(0, oSCM);
}
UpdatePanel udpMain = new UpdatePanel();
udpMain.ID = "udpMain";
TextBox txtMain = new TextBox();
txtMain.ID = "txtMain";
// other attrbutes here
Button btnPostback = new Button();
btnPostback.ID = "btnPostback";
btnPostback.Click += new EventHandler(btnPostback_Click);
btnPostback.Text = "Partial Postback";
Label lblPostback = new Label();
lblPostback.ID = "lblPostback";
lblPostback.Text = "initial postback";
udpMain.ContentTemplateContainer.Controls.Add(txtMain);
udpMain.ContentTemplateContainer.Controls.Add(btnPostback);
udpMain.ContentTemplateContainer.Controls.Add(lblPostback);
this.plcAJAX.Controls.Add(udpMain);
}
void btnPostback_Click(object sender, EventArgs e)
{
// implement button code here
Label lblPostback = (Label)this.plcAJAX.FindControl("lblPostback");
if (lblPostback != null)
{
lblPostback.Text = String.Format("WUC POstback at : {0}", DateTime.Now.ToLongTimeString());
}
}
}
then use it thus:-
<%# Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="TestExampleNoSCM.aspx.cs" Inherits="TestExampleNoSCM" %>
<%# Register Src="~/wucTestExample.ascx" TagName="wucTestExample" TagPrefix="ucTE" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<%--<asp:ScriptManager ID="scmAJAX" runat="server" />--%>
<asp:Label ID="lblLoadTime" runat="server" />
<ucTE:wucTestExample ID="wucTestExample" runat="server" />
</asp:Content>
----------------code behind---------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class TestExampleNoSCM : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.lblLoadTime.Text = String.Format("Page load at: {0}",DateTime.Now.ToLongTimeString());
}
}
So if you comment or uncomment the SCM in the parent page, the WUC still works either way.
2) I've seen another option where an update panel was needed and the programmer created all the controls in the designer and then looped round them in the page load (after creating the SCM, if needed, and the UDP and added all the controls on the WUC UDP, before then adding that to to placeholder, but this strikes me as rather dangerous as it seems to be double-instantiating control, and I think it may come back to bite them on the bum...
The downside with method 1 is that it's more work to create everything in your updatepanel programmatically, but if you really want to build a self-dependent WUC, that seems to be your price (and hopefully, the WUC shouldn't be that complicated, anyway). Personally, I think in my app (as the WUC won't be used outside it) I'll just make sure I add in an SCM where needed on the main page.
One other, final, note I would pitch in - I've seen people saying "add it to the master page" - this seems to be a particularly bad idea, IMHO, unless every page in your app needs the SCM as it will add a whole new level of bloat to your pages, and that doesn't seem to be a good idea as ASP.NET seems to have a good level of bloat already...
Instead of dynamically adding a ScriptManager if none exists on the page, simply do the opposite: add a ScriptManager to your ASCX and get rid of it if there's one already on the page. So...
protected override void OnInit(EventArgs e) {
base.OnInit(e);
AdjustScriptManager();
}
private void AdjustScriptManager() {
if (ScriptManager.GetCurrent(Page) != null) {
ScriptManager1 = null;
}
}
UPDATE:
Nah, after further testing this won't work, as ScriptManager1 = null does nothing helpful. If there is a way to do this (or to remove the Page control), please comment.
Solution: you can add a scriptmanager dynamically in the usercontrol by checking if the current page does not already contain a ScriptManager. Here's how:)
In the UserControl (ascx) html put this:
<asp:PlaceHolder ID="pHolder" runat="server" />
And in the code behind (ascx.cs):
//insert Scriptmanager dynamically only IF it does not already exist
private void createScriptManager(System.EventArgs e)
{
// the GetCurrent() method will return a ScriptManager from the Page
if (System.Web.UI.AjaxScriptManager.GetCurrent(this.Page) == null)
{
System.Web.UI.AjaxScriptManager manager = new System.Web.UI.AjaxScriptManager();
manager.EnablePartialRendering = true;
pHolder.Controls.Add(manager);
}
}
// call the above method from the usercontrol's OnInit
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
createScriptManager(e);
base.OnInit(e);
}
Sometimes it is necessary to define a ScriptManager dynamically. In my case I am using a usercontrol that will be put into different pages, but some of these pages already contain a ScriptManager and some dont, so how will my usercontrol know if it should define its own ScriptManager? The beauty of the above code is that the usercontrol adds a scriptmanager only if there isn't already one on the page.
Note: the System.Web.UI.AjaxScriptManager may be replaced with System.Web.UI.ScriptManager if you use an older version of Ajax.

Resources