Error while deleting all child controls from the parent control - c#

I have the next aspx page:
<%# Page Title="" Language="C#" MasterPageFile="~/MasterPage.Master" AutoEventWireup="true"
CodeBehind="newsEditor.aspx.cs" Inherits="ExpertSiteV2.newsEditor" %>
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<%# Register Assembly="ExpertSiteV2" Namespace="ExpertSiteV2" TagPrefix="custom" %>
<asp:Content ID="Content3" ContentPlaceHolderID="Main" runat="server">
<asp:Panel ID="Panel1" runat="server">
<asp:Panel ID="Panel2" runat="server" Width="660" Style="margin-bottom: 10px;">
<asp:Label ID="Label1" runat="server" Text="Label" Width="150">Заголовок новости</asp:Label>
<asp:TextBox ID="newsTitle" runat="server" Width="500" Style="float: right;"></asp:TextBox>
</asp:Panel>
<custom:CustomEditor ID="Editor3" runat="server" Height="300" Width="660" BackColor="White" />
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</asp:ToolkitScriptManager>
<asp:Panel ID="Panel3" runat="server" Style="margin-top: 5px;" CssClass="buttonPanel">
<asp:ImageButton ID="SaveImageButton1" runat="server" ImageUrl="img/save_32.png"
ToolTip="Сохранить новость" />
<asp:LinkButton ID="SaveLinkButton1" runat="server" ToolTip="Сохранить новость">Сохранить</asp:LinkButton>
<asp:ImageButton ID="ImageButton2" runat="server" CausesValidation="False" ImageUrl="img/block_32.png"
PostBackUrl="news.aspx" ToolTip="Вернуться к странице новостей" />
<asp:LinkButton ID="LinkButton2" runat="server" ToolTip="Вернуться к странице новостей"
CausesValidation="False" PostBackUrl="news.aspx">Отмена</asp:LinkButton>
</asp:Panel>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Заголовок должен быть заполнен!"
ControlToValidate="newsTitle" Display="Dynamic">
</asp:RequiredFieldValidator>
</asp:Panel>
</asp:Content>
I want to delete all the contents of Panel1. I write the code:
Panel1.Controls.Clear();
But it doesn't work and I get the message:
Page cannot be null. Please ensure
that this operation is being performed
in the context of an ASP.NET request.
Description: An unhandled exception
occurred during the execution of the
current web request. Please review the
stack trace for more information about
the error and where it originated in
the code.
Exception Details:
System.InvalidOperationException: Page
cannot be null. Please ensure that
this operation is being performed in
the context of an ASP.NET request.
Source Error:
An unhandled exception was generated
during the execution of the current
web request. Information regarding the
origin and location of the exception
can be identified using the exception
stack trace below.
Stack Trace:
[InvalidOperationException: Page
cannot be null. Please ensure that
this operation is being performed in
the context of an ASP.NET request.]
System.Web.UI.ScriptManager.get_IPage()
+373832 System.Web.UI.ScriptManager.OnPagePreRenderComplete(Object
sender, EventArgs e) +54
System.Web.UI.Page.OnPreRenderComplete(EventArgs
e) +8698462
System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean
includeStagesAfterAsyncPoint) +1029
Blockquote
What's wrong? How should I do it properly?

Okay, i've written something like this:
<%# Page Title="" Language="C#" MasterPageFile="~/MasterPage.Master" AutoEventWireup="true"
CodeBehind="newsEditor.aspx.cs" Inherits="ExpertSiteV2.newsEditor" %>
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<%# Register Assembly="ExpertSiteV2" Namespace="ExpertSiteV2" TagPrefix="custom" %>
<asp:Content ID="Content3" ContentPlaceHolderID="Main" runat="server">
<asp:Panel ID="Panel4" runat="server">
<asp:Panel ID="Panel1" runat="server">
<asp:Panel ID="Panel2" runat="server" Width="660" Style="margin-bottom: 10px;">
<asp:Label ID="Label1" runat="server" Text="Label" Width="150">Заголовок новости</asp:Label>
<asp:TextBox ID="newsTitle" runat="server" Width="500" Style="float: right;"></asp:TextBox>
</asp:Panel>
<custom:CustomEditor ID="Editor3" runat="server" Height="300" Width="660" BackColor="White" />
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</asp:ToolkitScriptManager>
<asp:Panel ID="Panel3" runat="server" Style="margin-top: 5px;" CssClass="buttonPanel">
<asp:ImageButton ID="SaveImageButton1" runat="server" ImageUrl="img/save_32.png"
ToolTip="Сохранить новость" />
<asp:LinkButton ID="SaveLinkButton1" runat="server" ToolTip="Сохранить новость">Сохранить</asp:LinkButton>
<asp:ImageButton ID="ImageButton2" runat="server" CausesValidation="False" ImageUrl="img/block_32.png"
PostBackUrl="news.aspx" ToolTip="Вернуться к странице новостей" />
<asp:LinkButton ID="LinkButton2" runat="server" ToolTip="Вернуться к странице новостей"
CausesValidation="False" PostBackUrl="news.aspx">Отмена</asp:LinkButton>
</asp:Panel>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Заголовок должен быть заполнен!"
ControlToValidate="newsTitle" Display="Dynamic">
</asp:RequiredFieldValidator>
</asp:Panel>
</asp:Panel>
</asp:Content>
Here is one more panel that contains everything. And I've written:
Panel4.Controls.Clear();
It works fine.
I still don't know why the Panel1 doesn't want to remove child controls. If someone knows just write the answer.

Can't you not just hide the panel? Panel1.Visible = false. why do you wanna remove the controls.
Aha ok I get the question now:
Check out this project, http://www.codeproject.com/KB/user-controls/DynamicUC.aspx

Related

Modal Popup Extender Not Working

I have this piece of code in my aspx page open a modal popup. I found this code online but for some reason the page refreshes and the popup does not appear. I tried several code snippets but the issue keeps on occurring. It must be an issue with my page.
This is my .aspx code
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
<style type="text/css">
.ModalPopupBG
{
background-color: Gray;
filter: alpha(opacity=70);
opacity: 0.7;
}
.popup
{
background:white;
}
</style>
<script>
function onAgree() {
alert("Thank you for your approval")
}
function onDisagree() {
alert("I appreciate your feedback; i will work harder next time")
}
</script>
<div>
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server"></asp:ToolkitScriptManager>
<p>This is a paragraph.</p>
<p>This is a paragraph.</p>
<p>This is a paragraph.</p>
<asp:Button ID="Button1" runat="server" Text="feedback"/>
<asp:Panel ID="Panel1" runat="server" CssClass ="popup">
please give me your feedback about the paragraph above?
<hr />
<asp:Button ID="btnagree" runat="server" Text="Agree" />
<asp:Button ID="btndisagree" runat="server" Text="Disagree" />
</asp:Panel>
<asp:ModalPopupExtender ID="ModalPopupExtender1"
runat="server"
TargetControlID ="Button1"
PopupControlID="Panel1"
BackgroundCssClass="modalBackground"
DropShadow="true"
OkControlID="btnagree"
CancelControlID="btndisagree"
OnOkScript="onAgree()"
OnCancelScript ="onDisagree()">
<Animations>
<OnShown>
<Fadein />
</OnShown>
<OnHiding>
<Fadeout />
</OnHiding>
</Animations>
</asp:ModalPopupExtender>
</div>
</asp:Content>

Show a calendar extendar within Asp.net application

I have an Asp.net application in which i'd like to add a calendar extendar. So i use this code:
<%# Page Title="" Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="Home.aspx.vb" Inherits="Home" %>
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<cc1:ToolkitScriptManager ID="ScriptManager1" runat="server" EnableScriptGlobalization="true"></cc1:ToolkitScriptManager>
<span style=" margin-left: 60%">
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="error1" Text="error1" ControlToValidate="FirstDate" ForeColor="Red" Font-Bold="true"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="error2" Text="error2" ControlToValidate="FirstDate" ValidationExpression="^[0-3]+[0-9]\/[0-1]+[0-9]\/[0-9]{4}" ForeColor="Red" Font-Bold="true"></asp:RegularExpressionValidator>
<asp:TextBox ID="FirstDate" runat="server" ></asp:TextBox>
<cc1:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="FirstDate" CssClass="test">
</cc1:CalendarExtender>
</span>
</asp:Content>
The problem is : when i click into the TextBox the calendar isn't shown :
Why this happens?
How can i modify my code to fix this error?
I don't believe that the CalendarExtender behaves the way you're expecting. At least, it doesn't for me. What I usually do is add an ImageButton and make the CalendarExtender's PopupButtonID equal to the ImageButton's ID.

Conflicts Mootools Scriptmanager Ajax Asp.net?

I was trying differents ways to make this work but any success. I am trying to integrate mootools with an asp.net application. I just want to use it to add some effects to my site. I am also ussing update panels, scriptmanager because dont want full postbacks. On the console raise an error TypeError: clientID.startsWith is not a function and when make an update the brwoser reset....
This is the whole code. Hope help me and thanks agan!
<%# Master Language="C#" AutoEventWireup="true" CodeFile="Default.master.cs" Inherits="guiaEscoles" %>
<!DOCTYPE html>
<html>
<head runat="server">
<title>Title go here</title>
<link href="~/css/main.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" LoadScriptsBeforeUI="true" EnablePartialRendering="true" >
<Scripts>
<asp:ScriptReference Path="~/scripts/mootools-core-1.4.5-full-compat-yc.js"></asp:ScriptReference>
<asp:ScriptReference Path="~/scripts/mootools-more-1.4.0.1-yc.js" ></asp:ScriptReference>
<asp:ScriptReference Path="~/scripts/jsecoles.js"></asp:ScriptReference>
</Scripts>
</asp:ScriptManager>
<asp:Panel runat="server" DefaultButton="" >
<asp:UpdatePanel runat="server" UpdateMode="Always">
<ContentTemplate>
<asp:DropDownList runat="server" ID="DropDownListCountries" >
</asp:DropDownList>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Panel>
<asp:ContentPlaceHolder ID="MainContent" runat="server" />
</form>
</body>
</html>
<%# Page Title="" Language="C#" MasterPageFile="~/Default.master" AutoEventWireup="true" CodeFile="search.aspx.cs" Inherits="searchschool" %>
<%# MasterType VirtualPath="~/Default.master" %>
<asp:Content ID="ContentSearch" ContentPlaceHolderID="MainContent" runat="Server">
<div id="PlaceItem" runat="server">
<div id="ulLugar" class="">
<ul>
<li>
<asp:DropDownList runat="server" ID="DropDownListCountryBottom" AutoPostBack="true" OnSelectedIndexChanged="DropDownListCountryBottom_SelectedIndexChanged" EnableViewState="true">
</asp:DropDownList>
</li>
<li>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="DropDownListCountryBottom" EventName="SelectedIndexChanged" />
</Triggers>
<ContentTemplate>
<asp:DropDownList runat="server" ID="DropDownListCounty" OnDataBound="DropDownListCounty_DataBound" AutoPostBack="true" OnSelectedIndexChanged="DropDownListCounty_SelectedIndexChanged" EnableViewState="true">
</asp:DropDownList>
</ContentTemplate>
</asp:UpdatePanel>
</li>
<li>
<asp:DropDownList runat="server" ID="DropDownListDistrict" OnSelectedIndexChanged="DropDownListDistrict_SelectedIndexChanged" AutoPostBack="true" EnableViewState="true">
</asp:DropDownList>
</li>
</ul>
</div>
<div id="divStudies" >
<asp:CheckBox ID="CheckBoxStudies" runat="server" Text="<%$ Resources:Escoles, CheckBoxPQPILabel %>" OnCheckedChanged="CheckBoxStudies_CheckedChanged" AutoPostBack="true" />
</div>
<asp:Repeater ID="RepeaterThemes" runat="server" OnItemDataBound="RepeaterThemes_ItemDataBound">
<HeaderTemplate>
<ul>
</HeaderTemplate>
<FooterTemplate></ul></FooterTemplate>
<ItemTemplate>
<li class="liTema">
<asp:CheckBox ID="CheckBoxTheme" runat="server" OnCheckedChanged="CheckBoxTheme_CheckedChanged" AutoPostBack="true" EnableViewState="false" /></li>
</ItemTemplate>
</asp:Repeater>
<asp:UpdatePanel runat="server" UpdateMode="Always">
<ContentTemplate>
<asp:GridView runat="server" ID="GridViewEscoles" CellPadding="0" CellSpacing="0" BorderStyle="None">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label Text='<%# Eval("Field_1") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
here is the myjs file:
window.addEvent('domready', function () {
alert("Hello");
//var countTotal = $$('.liTema').length;
//alert(countTotal);
});
That is due to a conflict between mootools & Microsoft Ajax code, According to mootools they blamed the error on microsoft's code. I was looking for a solution but didn't find till now

Textbox inside Repeater not making UpdateProgress to trigger

I have following code in .aspx
<asp:UpdatePanel ID="upnGrid" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Repeater ID="rpt1" runat="server" OnItemDataBound="rpt1_ItemDataBound">
<ItemTemplate>
<table width="100%">
<tr>
<td align="right" width="25%">
<asp:Label ID="lbl1" Font-Bold="true" Font-Size="16px" Text=' <%# DataBinder.Eval(Container.DataItem,"Name") %>'
runat="server" />
</td>
<td>
<asp:HiddenField ID="hdn1" Value='<%# DataBinder.Eval(Container.DataItem, "Length") %>'
runat="server" />
</td>
<td>
<asp:TextBox ID="txt1" Font-Size="16px" Text='<%# DataBinder.Eval(Container.DataItem, "Value") %>'
AutoPostBack="true" Width="100px" OnTextChanged="txt1_TextChanged" runat="server" onkeydown="javascript:return OntxtEnter(event);"/>
</td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
</ContentTemplate>
<%--<Triggers>
<asp:AsyncPostBackTrigger ControlID="rpt1"/>
</Triggers>--%>
</asp:UpdatePanel>
code for UpdateProgress is at the end of aspx -
<asp:Panel ID="UpdateProgressPanel" runat="server">
<center>
<asp:UpdateProgress ID="UpdateProgress2" runat="server" AssociatedUpdatePanelID="upnGrid"
DynamicLayout="true">
<ProgressTemplate>
<img src="../Images/loading.gif" alt="">
</ProgressTemplate>
</asp:UpdateProgress>
</center>
</asp:Panel>
<ajaxtoolkit:modalpopupextender id="ModalProgress" runat="server" targetcontrolid="UpdateProgressPanel"
popupcontrolid="UpdateProgress2" dropshadow="false" backgroundcssclass="modalBackground">
</ajaxtoolkit:modalpopupextender>
As suggested by some answers I made AsyncPostBackTrigger to be registered in code behind, like -
var control = e.Item.FindControl("txt1");
ScriptManager.GetCurrent(Page).RegisterAsyncPostBackControl(control);
I am not getting, why it is not working.
Please suggest.
Edit
I tried putting only required textbox in UpdatePanel, It is showing following error
'System.Web.UI.Control' does not contain a definition for 'DataItem' and no extension method 'DataItem' accepting a first argument of type 'System.Web.UI.Control' could be found (are you missing a using directive or an assembly reference?)
Try modifying your UpdateProgress control as shown below:
<asp:UpdateProgress ID="UpdateProgress1" runat="server" AssociatedUpdatePanelID="UpdatePanel1">
<ProgressTemplate>
<asp:Panel ID="Panel1" runat="server">
<asp:Panel ID="Panel2" runat="server">
<asp:ImageButton ID="ibProgress" runat="server" ImageUrl="~/Images/progress.gif"
Visible="true" />Loading...
</asp:Panel>
</asp:Panel>
</ProgressTemplate>
</asp:UpdateProgress>
You cannot use it in such a way. Because inside the repeater the TextBox ID is different.You have to follow different mechanism.

fire a textbox's OnTextChanged while enabled=false

I have an ajax toolkit calendar extender attached to my textbox and i a trying to fire the OnTextChanged after the date was entered into the textbox.
The thing is i don't want the user to enter data manually so i disabled the textbox but the event won't fire for a disabled text box.
is there anyway around it? i thought about changing to a label but is doesn't have OnTextChanged event.
Thanks
<asp:TableCell CssClass="cssWidth" Width="150px">
<asp:CalendarExtender ID="CalendarExtender1" PopupButtonID="Image1" runat="server" TargetControlID="TextBoxAddDate" Format="dd/MM/yyyy"></asp:CalendarExtender>
<asp:TextBox ID="TextBoxAddDate" ReadOnly="true" CssClass="cssWidth" ToolTip="תאריך הוספה" runat="server" Style="font-size: large;background-color:aliceblue;" AutoPostBack="true" AutoCompleteType="Search" MaxLength="0" TextMode="SingleLine" OnTextChanged="txtSearch_TextChanged" ViewStateMode="Enabled" autocomplete="off" >
</asp:TextBox>&nbsp
<asp:ImageButton runat="Server" ID="Image1"
ImageUrl="~/Calendar_scheduleHS.png" AlternateText="Click to show calendar" /><br />
</asp:TableCell>
Can you use ReadOnly instead of Enabled = false?
UPDATE:
Ok, this is the complete solution that worked for me:
<%# Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<script type="text/javascript">
function dateSelectionChanged(x) {
debugger;
javascript: __doPostBack('TextBoxAddDate', '')
}
</script>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<h2>
Welcome to ASP.NET!
</h2>
<p>
To learn more about ASP.NET visit www.asp.net.
</p>
<p>
You can also find <a href="http://go.microsoft.com/fwlink/?LinkID=152368&clcid=0x409"
title="MSDN ASP.NET Docs">documentation on ASP.NET at MSDN</a>.
</p>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:CalendarExtender ID="CalendarExtender1" PopupButtonID="Image1" runat="server" TargetControlID="TextBoxAddDate" Format="dd/MM/yyyy" OnClientDateSelectionChanged="dateSelectionChanged" ></asp:CalendarExtender>
<asp:TextBox ID="TextBoxAddDate" CssClass="cssWidth" ToolTip="תאריך הוספה" runat="server" Style="font-size: large;background-color:aliceblue;" AutoCompleteType="Search" MaxLength="0" TextMode="SingleLine" OnTextChanged="txtSearch_TextChanged" ViewStateMode="Enabled">
</asp:TextBox>&nbsp
<asp:ImageButton runat="Server" ID="Image1"
ImageUrl="~/Calendar_scheduleHS.png" AlternateText="Click to show calendar" /><br />
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
It seems that the CalendarExtender "steals" away the events from the texbox it uses.
Using this approach I managed to fire up the OnTextChanged event and the handler txtSearch_TextChanged gets executed.
Did you make the textbox ReadOnly?

Categories