Adding a page control from within a user control to the collection

I have an asp.net usercontrol that represents the "popup" dialog. Basically, this is a wrapper for the jQuery user interface dialog box, which can be subclassed to easily create dialogs.

As part of this control, I need to enter a div on the page on which the control is used, either at the very top or at the bottom of the form, so that when the popup is instantiated, the parent element will be changed to this div. This allows nested pop-ups if the popup of the child does not fall into the parent popup.

The problem is that I cannot find a safe way to insert this div into the page. Usercontrol does not have a preinit event, so I cannot do this, and calling Page.Form.Controls.Add (...) on Init, Load or PreRender raises a standard exception. "The control collection cannot be changed during DataBind, Init, Load, PreRender or Unload events."

I thought I found a solution using ...

ScriptManager.RegisterClientScriptBlock(Page, Me.GetType, UniqueID + "_Dialog_Div", containerDiv, False) 

... which seemed to work fine, but recently a colleague tried to put the UpdatePanel in the dialog box and now she gets the error "Script tag registered for type" ASP.controls_order_viewzips_ascx "and the key" ctl00 $ ContentBody "$ OViewZips_Dialog_Div 'has invalid characters outside script tags: only correctly formatted script tags can be registered. "

How should you add controls to the page control collection from a user control?

+4
source share
2 answers

I'm not sure why you really need to add this div to the page form, but this should work:

  Public Class WebUserControl1 Inherits System.Web.UI.UserControl Private Sub UC_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init AddHandler Me.Page.Init, AddressOf Me.Page_Init End Sub Private Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs) Dim dialogDiv As New Panel dialogDiv.ID = "DialogDiv" If Not Page.Form.Controls.Contains(dialogDiv) Then Page.Form.Controls.AddAt(0, dialogDiv) End If End Sub End Class 

How to implement it in C #. I always get an error. Control collection cannot be changed to Load, PreRender. I need Literal Control, which will be added to my main page page from a user control. The literal control will contain a css link.

Do you want to add a literal to the HeadContent control in the main or page title (headt html element)? However, here I show both.

Here is the codebehind of your UserControl :

 public partial class UC_AddToMaster : System.Web.UI.UserControl { private void Page_Init(object sender, System.EventArgs e) { this.Page.Init += UC_AddToMaster_Init; } private void UC_AddToMaster_Init(object sender, EventArgs e) { Literal literal = new Literal{ Text = "Hi World!" }; // if you want to add it to the header of the page: if (!Page.Header.Controls.Contains(literal)) { Page.Header.Controls.AddAt(0, literal); } // if you want to add it to the master HeadContent ContentPlaceHolder control: var siteMaster = Page.Master as SiteMaster; if (siteMaster != null) { if (!siteMaster.Head.Controls.Contains(literal)) { siteMaster.Head.Controls.AddAt(0, literal); } } } } 

For the HeadContent approach mentioned above, I provided the following property in master:

 // in the master public ContentPlaceHolder Head { get { return this.HeadContent; } } 

So I needed to draw a page master for this actual type ( SiteMaster here) in UserControl . Otherwise, I would not be able to access this property.

+6
source

Usercontrol does not have a PreInit event, but nothing prevents you from adding a handler to the PreInit event of the page in your UserControl:

 Page.PreInit += new EventHandler(Page_PreInit); 

edit - you're right - you cannot grab PreInit from usercontrol, although I am surprised you cannot, but you can still change the collection of page controls by adding code to the UserControl constructor. I tried this and it works.

 public MyUsercontrol() { Page page = (Page)HttpContext.Current.Handler; Literal lit = new Literal(); lit.Text="text"; page.Controls.Add(lit); } 

Adding an event handler to Page.PreInit in the constructor compiler, but it never works.

(editing)

However, I'm not quite sure why this is necessary to achieve your goal. Why don't you just have your dialog box tool display its own div in the line, wherever you drop it into the form, and use this as the parent instead of trying to create somewhere else on the form? I cannot think of why for this it would be important to be physically rendered at the beginning or end of the form. This is a dialogue, so it will always be invisible until you use it, right?

+4
source

Source: https://habr.com/ru/post/1339435/


All Articles