ASP.NET dynamically reassign controls in the control tree - asp.net

Let's say I have a custom control that looks like this
<cc:MyControl runat="server" ID="myc" LinkControlID="NewParent" />
and, on the same page:
<asp:TextBox runat="server" ID="NewParent" />
What I would like to do is, from MyControl, change NewParent's parent so that it would be part of MyControl's Controls collection. When I try to do this, from OnInit, I get:
The control collection cannot be modified during DataBind, Init, Load, PreRender or Unload phases.
Which makes sense, but is there a way around this? I'm OK if NewParent remains the child of the Page as long as from MyControl I can somehow redirect the rendering to MyControl's control.
Can this be done? Thanks.
EDIT:
To clarify here's a mockup of MyControl:
public class MyControl : Panel
{
protected override void OnInit(System.EventArgs e)
{
base.OnInit(e);
if (!String.IsNullOrEmpty(LinkControlID))
{
Control link = Parent.FindControl(LinkControlID);
if (link != null)
{
Controls.Add(link);
}
}
}
public string LinkControlID { get; set; }
}
This assumes that MyControl and LinkControlID are placed on the same level in the tree hierarchy, which is OK in my case.

why don't you try adding it in Page_LoadComplete
myc.Controls.Add(NewParent);

I couldn't reproduce it, but as aman.tur suggested, have you tried another event? Overriding CreateChildControls() seems like the right place for it.

#TheVillageIdiot's suggestion worked for me:
protected void Page_Init(object sender, EventArgs e)
{
Page.LoadComplete += Page_LoadComplete;
}
private void Page_LoadComplete(object sender, EventArgs e)
{
if (InHeader)
{
Page.Header.Controls.Add(this);
}
}
Mind you, I had different requirements, but I don't think it should be much of an issue if you can access the new parent control from within your MyControl. The general format is to add an event listener on the Page to make changes on LoadComplete.

Related

Custom Image Button control click handler not firing

I've got a bit of an issue with creating a new control based on ASP.NET's ImageButton control. Everything works as expected, except for the click handler that is being hooked up in the control's OnInit override. Basically, clicking the custom image button just refreshes the page, never hitting the handler.
Now, I know this is something stupid I've done or just not understood, but I can't for the life of me figure this out. All the articles, questions and forum posts I've found on event handling issues for controls is for child controls, rather than ones that inherit from existing control types and have their own predefined handlers.
The following code is what I've written:
public class WebPaymentButton : ImageButton
{
public string DisabledImageUrl { get; set; }
public string TermsAcceptClass { get; set; }
protected override void OnPreRender(EventArgs e)
{
Page.ClientScript.RegisterClientScriptResource(typeof (WebPaymentButton), "PaymentModule.Scripts.WebPaymentButtonScript.js");
}
protected override void OnInit(EventArgs e)
{
CssClass = "WebPaymentButton";
if (!string.IsNullOrWhiteSpace(TermsAcceptClass))
{
Attributes["data-TermsClass"] = TermsAcceptClass;
}
if (!string.IsNullOrWhiteSpace(DisabledImageUrl))
{
Attributes["data-DisabledImageUrl"] = ResolveUrl(DisabledImageUrl);
}
Click += WebPaymentButton_Click;
base.OnInit(e);
}
private void WebPaymentButton_Click(object sender, ImageClickEventArgs e)
{
HttpContext.Current.Response.Redirect("http://dummy_payment_page_in_place_of_code", true);
}
}
I've tried hooking the handler up in the OnLoad and also switching it to run after the base.OnInit/OnLoad calls. Nothing has solved the handler issue. Can anyone point me in the right direction?
In case it helps, here is the markup for the button on the page:
<pm:WebPaymentButton runat="server" ImageUrl="~/pay-now.png" DisabledImageUrl="~/not-pay-now.png" TermsAcceptClass="TermsCheckbox" ID="MainPayButton" />
Have you tried overriding the OnClick event handler instead of hooking up to a new event handler?
Remove the Click += WebPaymentButton_Click line from OnInit and remove the WebPaymentButton_Click function, then add the following code to your class instead:
protected override void OnClick(ImageClickEventArgs e)
{
base.OnClick(e);
HttpContext.Current.Response.Redirect("http://dummy_payment_page_in_place_of_code", true);
}

ASP.net Web Server Control, children are NOT maintenaing state

I am developing a custom server control, following is control's markup on my ASPX page,
<uc:NoteBook ID="nb1" runat="server">
<NotePages>
<uc:NotePage ID="p1" runat="server">
<asp:DropDownList ID="ddl1" runat="server"></asp:DropDownList>
</uc:NotePage>
</NotePages>
</uc:NoteBook>
Note that this control has NotePages property which is actually a collection of NotePage control. And NotePage control can have any type of children.
On the same aspx page, in Page_Load event handler, I am loading some items in DropDownList control,
if (!this.IsPostBack)
{
this.ddl1.Items.Add(new ListItem("Class-1", "C1"));
this.ddl1.Items.Add(new ListItem("Class-2", "C2"));
this.ddl1.Items.Add(new ListItem("Class-3", "C3"));
}
Problem:
Now problem is after Post Back, the DropDownList items get cleared. Which is clearly indicating I am missing some state management here.
Can anyone please guide my how to handle it?
Following is the control code.
public class NoteBook : CompositeControl
{
[PersistenceMode(PersistenceMode.InnerProperty)]
public virtual List<NotePage> NotePages
{
get;
set;
}
public NoteBook()
{
this.NotePages = new List<NotePage>();
}
protected override void CreateChildControls()
{
foreach (var c in this.NotePages)
{
this.Controls.Add(c);
}
base.CreateChildControls();
}
}
[ParseChildren(false)]
public class NotePage : UserControl
{
}
Thanks.
You need to add the items when initializing the control not during the load event. Note: that the items need to always be added, not just when the page is not loading during a GET. By the time the load event has occurred the state has been initialized. See `Control Execution Lifecycle'
OK I solved it.
We just need to call the EnsureChildControls() method on control initialization, which causes child controls to be created before the ViewState/etc. things gets called.
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
this.EnsureChildControls();
}
I added above code in my NoteBook control.

MultiView as CompositeControl

I want to build a server control that inherits from System.Web.UI.WebControls.MultiView.
The control will contain a collection of server controls that inherit from System.Web.UI.WebControls.View in turn. However, when I add a single view to the control's View collection, nothing gets rendered...
public class WizardMultiview : System.Web.UI.WebControls.MultiView
{
// field
private WizardStart _start;
override CreateChildControls()
{
this._start = new WizardStart(); // custom view
this._start.ID = "WizardStart1";
this.Views.Add(this._start);
this.ActiveViewIndex = 0;
}
override Render(HtmlTextWriter writer)
{
// NOTE: at this point the control can't render itself.
}
}
Then, within the Default.aspx.cs I do:
public void Page_Load(object sender, EventArgs e)
{
My.Controls.WizardMultiview view = new My.Controls.WizardMultiview();
this.Panel1.Controls.Add(view); // this is a Panel on the Default.aspx page
}
The custom View control looks like this:
public class WizardStart : System.Web.UI.WebControls.View
{
override Render(HtmlTextWriter writer)
{
writer.WriteLine("I am the starting point");
}
}
When I add this control an ASPX page, nothing happens. But if I create a MultiView, and add the MultiView to a CompositeControl's Control Collection and add the view to this MultiView, it works fine? I'm sure what I want to achieve is possible?

Pass data to user components in asp.net

It is .net 2.0 here, not MVC, and I am crap at asp forms.
I have a page with user controls on it. When I click on something in the page, I want to load the usercontrol based on a parameter from the page.
I cannot do it.
In my page's FaultTree_Clicked, I get the value, then:
I tried exposing a property on the
child user control to set the value, which i set in FaultTree_Clicked,
it gets forgotten.
I tried saving it to
Session["mykey"], and loading
Session["mykey"] in the control's
Page_init... the value is blank.
I tried saving it to
ViewState["mykey"], and loading
ViewState["mykey"] in the control's
Page_init... the value is blank.
EDIT: more specific info:
Here is a cut down version of what the page(MyFault) looks like:
<form id="form" runat="server">
<div id="faulttree">
<asp:TreeView ID="FaultTree" ......>
</div>
<uc1:_DefectDetail ID="DefectDetail" runat="server" Visible="true" EnableViewState="true" />
</form>
And there is a method on the pages code behind "FaultTree_SelectedNodeChanged()".
When that method is hit, I want to load/show the DefectDetail control. The DefectControl requires a faultid, which comes off the Tree, which I successfully get in the SelectedNodeChanged method. I cannot get the faultid into the defect control.
This has to do with ASP.NET page lifecycle. By the time the click event fires, the control's init event has already happened.
In order to better assist you, please provide a more detailed explanation of what the FaultTree control is, what is the desired result and some sample code.
UPDATE:
Instead of a public property, you can simply create a public method in the control that does the desired action and invoke it from the FaultTree_SelectedNodeChangeEvent.
Example (for a public method named Refresh):
_DefectDetail.Refresh(object data);
Basically you have to use EventHandlers....
1. Add a event handler to your user control (I had a search bar UscSearchCriteriaBar1)
public event EventHandler CriteriaChanged;
+
private void InternalOnCriteriaChanged()
{
OnCriteriaChanged();
}
+
protected virtual void OnCriteriaChanged()
{
// If there are registered clients raise event
if (CriteriaChanged != null)
CriteriaChanged(this, EventArgs.Empty);
}
+
Example
public int EmployeeID
{
get
{
f (Session["EmployeeID"] != null)
{
ViewState["EmployeeID"] = Convert.ToInt32(Session["EmployeeID"]);
}
if (ViewState["EmployeeID"] == null)
ViewState["EmployeeID"] = 0;
return int.Parse(ViewState["EmployeeID"].ToString());
}
set
{
ctlEmployee.SelectedValue = value.ToString();
ViewState["EmployeeID"] = value;
Session["EmployeeID"] = value;
}
}
In your page or other control
override protected void OnInit(EventArgs e)
{
InitializeComponent();
UscSearchCriteriaBar1.CriteriaChanged += new EventHandler(this.CriteriaChanged);
base.OnInit(e);
}
private void CriteriaChanged(object sender, EventArgs e)
{
try
{
RefreshData();
}
catch (Exception ex)
{
ExceptionManager.Publish(ex);
}
}
You can get UscSearchCriteriaBar1.EmployeeID
This code should give you some ideas...was done for 1.1 should work on 2.

Update links on master page with data from child page

I have a menu of report links in my master page. I need to append an ID to the end of each whenever the user changes a value on the child page. What's a good way to accomplish this?
UPDATE: I should have mentioned that the child update is happening inside an UpdatePanel, meaning the master page is not reloaded when the change happens.
A MasterPage is really a child control of the page which it controls. You can control a MasterPage like any other control on your page (almost). All you need to do is get a reference to it.
You add a property to the code of your MasterPage, so its code may look something like this:
public partial class _default : System.Web.UI.MasterPage
{
protected string m_myString = string.Empty;
public string myString
{
get { return m_myString; }
set { m_myString = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
}
}
Then you have to cast the this.Master property to your MasterPage
public partial class index : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// Cast here to get access to your MasterPage
_default x = (_default)this.Master;
x.myString = "foo";
}
}
In response to your UPDATE:
The updated panel could write the ID to a hidden field and the menu events could look for that hidden fields in Request.Form["fieldName"].
Note that you shouldn't fieldName.Text because ASP.NET does a bad job of returning the right value for fields that have been AJAXed.

Resources