Is there a way to universally force a page to error when a usercontrol raises an error? For example, if you write code that will create an error in a usercontrol, like this:
<asp:Label ID="lbl" runat="server" Text='<%# string.Format(("") %>' />
The page will still load like everything is working, but the portion of the page that the usercontrol occupies will contain the error "... ) expected." I want the page to actually error like it normally would if that bad code were on a normal .aspx page. Thanks in advance for any advice.
See How do I catch an exception raised on my ASCX control (not the code-behind)? for some details on this. One suggested solution involves creating an event in your user control that is fired if an exception is caught during the control's RenderChildren method. Then, your parent aspx page can handle the event raised in the ascx.
Related
I have an entry form which I want to add a file uploader I added a toolkit script manager to the master page
then i added an ajaxfileupload control <http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/AjaxFileUpload/AjaxFileUpload.aspx>
the aspx looks like this
<ajaxToolkit:AjaxFileUpload ID="AjaxFileUpload1" ThrobberID="myThrobber" ContextKeys="fred"
AllowedFileTypes="jpg,jpeg" MaximumNumberOfFiles="2" runat="server" UploadedComplete="AjaxFileUpload1_UploadedComplete" />
the method AjaxFileUpload1_UploadedComplete is empty for now.
I got this exception and no event is firing except for the page_load
Sys.ArgumentException: Sys.ArgumentException: Cannot deserialize. The
data does not correspond to valid JSON.
I realise this is an old question but i think this is caused by a 'bug' in the control. It appends query string parameters but assumes there are no existing parameters.
There are details and a fix here: https://ajaxcontroltoolkit.codeplex.com/workitem/27149 (involves changing the source and rebuilding the toolkit)
I have a page that uses CustomerDetails.ascx. It uses CM.ascx. Inside CM.ascx I use AJAX Data Controls GridView with ID="gdvRecommendation".
Sometimes when I browse to the page I get the following exception:
Script control 'gdvRecommendation' is not a registered script control. Script controls must be registered using RegisterScriptControl() before calling RegisterScriptDescriptors().
Parameter name: scriptControl
My ScriptManager defined in the master page before the content place holder.
This exception doesn't happened always.
I use CustomerDetails.ascx user controls in other pages and it works great.
Can anyone explain me the problem and what can be the solution?
Edit:
Here is the GridView usage in CM.ascx:
<td valign="top" style="height: 150px;">
<div id="divCMMessage"></div>
<div id='divRecommendation' style="width: 100%; display: inline; overflow: auto;">
<ADC:GridView ID="gdvRecommendation" Width="100%" runat="server" CellSpacing="0" CellPadding="3" HorizontalAlign="Right" ShowHeader="false" RowDataBoundEvent="onRowDataBound_gdvRecommendation">
<RowStyle Height="20px" />
<EmptyDataTemplate>no recommendations</EmptyDataTemplate>
<EmptyDataRowStyle HorizontalAlign="Right" BorderWidth="0" />
<Columns>
<ADC:GridViewImageColumn DataImageUrlField="IndImageUrl" HeaderText="" ItemStyle-Width="25px" ItemStyle-HorizontalAlign="Center"></ADC:GridViewImageColumn>
<ADC:GridViewTemplateColumn HeaderText="">
<ItemTemplate>
<asp:Label ID="TreatName" runat="server" Text=""></asp:Label>
</ItemTemplate>
</ADC:GridViewTemplateColumn>
<ADC:GridViewTemplateColumn HeaderText="">
<ItemTemplate>
<asp:Label ID="TreatType" runat="server" Text=""></asp:Label>
</ItemTemplate>
</ADC:GridViewTemplateColumn>
</Columns>
</ADC:GridView>
</div>
</td>
The CustomerDetails.ascx is inside a MultiView (on MyPage.aspx).
I had a similar problem myself and this post helped me to understand my errors:
==============================================
Script control 'ControlName' is not a registered script control
So you're here because you got the following error.
Script control 'ControlName' is not a registered script control. Script controls must be registered using RegisterScriptControl() before calling RegisterScriptDescriptors().
Parameter name: scriptControl
I myself have done a fair bit of googling on this, and there are many
solutions given all over the net. unfortunately these are all very
scenario specific solutions. Some people get their answers, and the
others, like me, don't.
Answer:
You are changing the visibility of a control at the wrong stage of the
page life cycle.
Description:
If you are changing the visibility of a control, you should always do
it during, or before the PreRender event. If you do it after (i.e. in
a End handler of an async task or during PreRenderComplete) you might
run in to this issue.
This is not an issue for simple controls such as buttons or text
boxes. But it will have adverse effects on controls such as grids.
When I say changing the visibility it could be any one of the
following situations
Having visible=false of a control during the early stages of the life cycle and being changed to visible=true during an end handler or PreRenderComplete
Changing the selected view of a MultiView during an end handler or PreRenderComplete
Any other situation where the control may not be visible during the earlier stages of the page life cycle which are set to be visible during the latter stage
Reason:
Purely
from my understanding, ASP.NET does not render the scripts or the HTML
related to a control if it is not being shown to the user. The
registering of script controls which is mentioned in the exception
seem to happen during an early stage of the life cycle. if the control
is not visible at this stage, this registration is skipped for that
control. If it's made to be visible at a latter point, you get
yourself a control without some of the relevant scripts.
Anyway this is what I have understood. I may be wrong. But if you come
across this issue, it will definitely help you to check for controls
which are changing visibility at different points of the life cycle.
You will be able to identify your specific problem by doing this and
then come up with a solution on your own.
Hope this information helps some one.
============================
Source
I faced same problem recently, above solution did not work for me. I was Hiding a control and later making it visible in PreRender based on some condition check. Hiding was working fine but when I was making it visible again, it was giving me the same error.
below is What worked for me is-
ScriptManager sm = ScriptManager.GetCurrent(Page);
sm.RegisterScriptControl(control_name);
Then making the control visible resolved my issue.
control_name.Visible = true;
Hopefully it may help someone facing this issue.
I had a similar issue with AJAX, and amusingly it occured only when in 'Debug' mode. In release mode, everything works fine. I have not reached to a conclusion on it yet.
You may want to give it a shot.
In case of a RadAjaxPanel and loading UserControls, reading the answer from jhfelectric, I came up with the following solution (simple one):
Disable AJAX on load (Me.EnableAJAX = False) and Enable it on PreRender. Because PreRender isnot called when removing a control, and is called after adding a control.
You can get this Exception when you have a RadAjaxManager in a user control that itself is also inside a RadAjaxManager, either directly or as the child of another user control.
In my own server control, I would like to implement something similar to the ListView:
<asp:ListView runat="server">
<LayoutTemplate>
<asp:PlaceHolder runat="server" id="itemPlaceholder" />
</LayoutTemplate>
</asp:ListView>
I have created an ITemplate property, I can set the layout in the aspx page, and I am doing ITemplate.InstantiateIn(myControl).
But I can't figure out how to insert controls at the placeholder. I'm guessing it would be something like MyTemplate.FindControl("itemPlaceholder").Controls.Add(myControl). I tried casting to the type of my ITemplate, but I get the error:
Unable to cast object of type 'System.Web.UI.CompiledTemplateBuilder' to type 'MyNamespace.MyLayoutTemplate'
What am I missing?
Edit: I just found this: http://www.nikhilk.net/SingleInstanceTemplates.aspx Control developers can define templates to be single instance using metadata which causes the ID'd controls within the template contents to be promoted to the page level... The parser and code-generator together work behind the scenes to add member fields, and initialize them at the right time.. It seems to be only for user controls? I tried Page.FindControl() after doing this but it didn't find anything.
Ok, this was simply a matter of user error. I instantiated the ITemplate in a Panel, so obviously Page.FindControl() (which is not recursive) wouldn't work. Once I did Panel.FindControl(), everything worked.
In the past I have used this library with sucess
http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx
I'm trying to perform two-way data binding on the controls in my user control, which is hosted inside a FormView template:
<asp:ObjectDataSource runat="server" ID="ObjectDataSource"
TypeName="WebApplication1.Data" SelectMethod="GetItem" UpdateMethod="UpdateItem">
</asp:ObjectDataSource>
<asp:FormView runat="server" ID="FormView" DataSourceID="ObjectDataSource">
<ItemTemplate>
<uc:WebUserControl1 runat="server"></uc:WebUserControl1>
</ItemTemplate>
<EditItemTemplate>
<uc:WebUserControl1 runat="server"></uc:WebUserControl1>
</EditItemTemplate>
</asp:FormView>
The web user control:
<%# Control Language="C#" ... %>
<asp:TextBox runat="server" ID="TitleTextBox" Text='<%# Bind("Title") %>'>
</asp:TextBox>
The binding works fine when the FormView is in View mode but when I switch to Edit mode, upon calling UpdateItem on the FormView, the bindings are lost. I know this because the FormView tries to call an update method on the ObjectDataSource that does not have an argument called 'Title'.
I tried to solve this by implementing IBindableTemplate to load the controls that are inside my user control, directly into the templates (as if I had entered them declaratively). However, when calling UpdateItem in edit mode, the container that gets passed into the ExtractValues method of the template, does not contain the TextBox anymore. It did in view mode!
I have found some questions on SO that relate to this problem but they are rather dated and they don't provide any answers that helped me solve this problem.
How do you think I could solve this problem? It seems to be such a simple requirement but apparently it's anything but that...
My current workaround for this is, although rather cumbersome, to subclass the FormView class and use subclassed controls in it, implementing my own data binding logic (taking the data field name from a new property) instead of using the <%# %> syntax. Apparently, the code the latter generates is the real culprit here as it doesn't support this nested control scenario.
I ended up, using old asp include statement
<--%include file = "filename" -->
instead of using user controls for dealing with the code duplication issue.
For example, I have a custom control called SampleControl. To simplify things for this question, pretend I'm making just a Panel Control clone from scratch.
<CC:SampleControl ID="Sample1" runat="server">
<asp:Label ID="Label1" runat="server" Text="Hi"></asp:Label>
<asp:Button ID="Button1" runat="server" Text="Button1"></asp:Label>
</CC:SampleControl>
To Output:
<div id="Sample1">
<span id=Label1>Hi</span>
etc.....
</div>
Where the Code is:
public class SampleControl: WebControl
{
.....Render Stuff Goes Here.....
}
The ASP controls in the middle aren't being registered by ASP.NET. When I did a Reflector on the Panel control, I couldn't find out how they got the middle controls to render. It doesn't look like they are using a Templated Control.
When I do it, I get this error:
Parser Error
Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.
Any Ideas?
First, [ParseChildren(false)] attributes lets parser know that you want your control to treat nested content as child controls.
Then make sure that somewhere in Render the RenderChildren method is called. It's the default implementation of System.Web.UI.Control.Render(), so you can do base.Render(writer) in your overriden Render method.