creating Custom FormView - asp.net

I'm extending the formview class to create a custome formview which has a server control +formview + other server control below it.
the problem is i cant uses
protected override void OnInit(EventArgs e)
{
Parent.Controls.Add(FormViewButtons);
Parent.Controls.Add(this);
Parent.Controls.Add(MessageContainerControl);
}
because i get exception: "The control collection cannot be modified during DataBind, Init, Load, PreRender or Unload phases.".
any Ideas?
tanks.

You ought to be creating a Composite Custom Control, not extending the FormView.

I don't really understand why should the FormView manipulate the page's controls.
I think you should extend the Page and on this you should do what you want.

Related

How do I bind to a data source in a user control

when I have controls on a page and I want to data bind them I usually do this in the Page_Load event.
However I have some controls to be databound within a user control. In the code behind for the user control is there a similiar event to Page_Load I could use to databinding?
The DataBindChildren sounds appropriate for what you are trying to do
protected override void DataBindChildren()
{
// Bind a data source to the server control's child controls.
}
If you have created a user control using an ascx it has a Page_Load event that can be accessed exactly like a page.
http://msdn.microsoft.com/en-us/library/fb3w5b53.aspx

How to disable auto data bind of ASP.NET page?

How can I prevent ASP.NET page from automatically binding data controls on the page? I want to increase performance and I want to do binding of each data control based on my own order.
Simple, don't setup data binding on the controls in the designer.
You would then have to bind the controls inside the code behind part of the page with code.
Not quite what the OP asked for but it is also possible to cancel the Select operation on the Datasource control by adding an event handler to the Selecting event.
public void DataSource_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
{
if (CancelSelect())
{
e.Cancel=true;
return;
}
}

Page unload event in asp.net

Is it possible to call a write a Page_Unload event in code behind similar to Page_Load event? I wanted to call a method on Page Unload. How do I achieve that?
With AutoEventWireup which is turned on by default on a page you can just add methods prepended with **Page_***event* and have ASP.NET connect to the events for you.
In the case of Unload the method signature is:
protected void Page_Unload(object sender, EventArgs e)
For details see the MSDN article.
Refer to the ASP.NET page lifecycle to help find the right event to override. It really depends what you want to do. But yes, there is an unload event.
protected override void OnUnload(EventArgs e)
{
base.OnUnload(e);
// your code
}
But just remember (from the above link): During the unload stage, the page and its controls have been rendered, so you cannot make further changes to the response stream. If you attempt to call a method such as the Response.Write method, the page will throw an exception.
There is an event Page.Unload. At that moment page is already rendered in HTML and HTML can't be modified. Still, all page objects are available.

ASP.net page life cycle

I am having an ASP.net page in my page i am having this as my code behind files.
on first access the page the page preinit, init, load methods are called. on postbacks
the preinit, init, load methods are called.
My question is LoadViewstate and control state events (Overridden methods) are not firing after postbacks also
protected override void OnPreInit(EventArgs e)
{
base.OnPreInit(e);
}
protected override void LoadViewState(object savedState)
{
base.LoadViewState(savedState);
}
protected override void LoadControlState(object savedState)
{
base.LoadControlState(savedState);
}
protected void Page_Init(object sender, EventArgs e)
{
}
protected void Page_Load(object sender, EventArgs e)
{
// lblName.Text = ViewState["Test"].ToString();
}
This method is used primarily by the
.NET Framework infrastructure and is
not intended to be used directly from
your code. However, control developers
can override this method to specify
how a custom server control restores
its view state. For more information,
see ASP.NET State Management Overview.
The LoadViewState method restores the
view-state information that was saved
during a previous SaveViewState
request. The WebControl class
overrides the base LoadViewState
method to handle the ViewState, Style,
and Attributes properties.
Also note
Control State Sometimes you need to
store control-state data in order for
a control to work properly. For
example, if you have written a custom
control that has different tabs that
show different information, in order
for that control to work as expected,
the control needs to know which tab is
selected between round trips. The
ViewState property can be used for
this purpose, but view state can be
turned off at a page level by
developers, effectively breaking your
control. To solve this, the ASP.NET
page framework exposes a feature in
ASP.NET called control state.
The ControlState property allows you
to persist property information that
is specific to a control and cannot be
turned off like the ViewState
property.
Asp.Net StateManagement link
If your control is a customer server control take a look at
iStateManager
And for the complete overview of viewstate - had to search my bookmarks try
Truly understanding viewstate
ASP.NET optimizes this call, and calls the LoadViewState only if there is any custom data written to the view state.
If you set something to the view state in the first call (e.g. ViewState["foo"] = 42;), the LoadViewState will be called in the next (and subsequent) callbacks.

How to create a MaskedEditExtender on the fly?

I want to create a number of masked edit extenders from codebehind. Something like:
private MaskedEditExtender m_maskedEditExtender;
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
m_maskedEditExtender = new MaskedEditExtender()
{
BehaviorID = "clientName"
};
m_maskedEditExtender.Mask = "999999999";
this.Controls.Add(m_maskedEditExtender);
}
protected override void Render(HtmlTextWriter writer)
{
m_maskedEditExtender.RenderControl(writer);
}
When I do this, I get a NullReferenceException on OnLoad of MaskedEditExtender. What is the correct way of doing that? Please note that putting the extender into a repeater-like control and using DataBind does not work for me.
Edit: I do not have an update panel. Turns out I also need to specify a target control on serverside.
See ASP.NET Page Life Cycle Overview if this is in a Page subclass. If you scroll down to the event list, that page advises you to use the PreInit event to create any dynamic controls. It's necessary to do that early to ensure that ASP.NET cleanly loads ViewState at the right stage, among other things.
If you are doing this in a web user control or custom control, though, override CreateChildControls and do this in there.
Post a more complete code example if that doesn't help.
Your example is not providing a TargetControlID.
Do you have an updatePanel on the page? I had problems dynamically creating extenders as they weren't being added to the updatePanel content.
I also think you have to do somethin with the ScriptManager (registering the extender) but I could be mistaken (I don't have access to the code I did dynamic extenders at the moment).
Provide the proper TargetControlID value to MaskedEditExtender

Resources