User Control, Delegate and UpdatePanel Issue - c#

I am currently running up against a problem with the UpdatePanel. One of my pages has two User Controls I'll call A and B.
The User Control A has an UpdatePanel containing several ASP TextBox.
<asp:UpdatePanel ID="upA" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="true" style="position: relative;">
<ContentTemplate>
<-- content here -->
</ContentTemplate>
</asp:UpdatePanel>
The User Control B has an UpdatePanel containing a Panel and an AjaxModalPopup linked to it. In the Panel, there is a Repeater and an ASP button.
<asp:UpdatePanel runat="server" ID="upB" UpdateMode="Conditional">
<ContentTemplate>
<asp:Button ID="btnActivePopupContainer" runat="server" Style="display: none;" />
<asp:Panel ID="pnlPopupContainer" runat="server" CssClass="modalPopup modalPopupDraggable" Style="display: none;" Width="750px">
<asp:UpdatePanel ID="upPopup" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Panel runat="server" ID="pnlHeader" class="header">
<asp:Label ID="Label1" runat="server" Text="<%$ Resources:global, AttentionPopup %>"></asp:Label>
</asp:Panel>
<article>
<asp:Repeater runat="server" ID="rptPopup" OnItemDataBound="rptPopup_ItemDataBound">
<ItemTemplate>
<-- Content -->
</ItemTemplate>
</asp:Repeater>
</article>
<footer>
<div class="or-spacer">
<div class="mask"></div>
</div>
<div class="footer">
<asp:Panel runat="server" ID="pnlBtnBug">
<asp:Button ID="btnOK" runat="server" CssClass="btn yes" Text="<%$ Resources:global, Ok %>" OnClick="btnOK_Click" />
</asp:Panel>
</div>
</footer>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Panel>
<ajax:ModalPopupExtender ID="popupContainer" runat="server" BackgroundCssClass="modalBackground" PopupControlID="pnlPopupContainer" TargetControlID="btnActivePopupContainer" Drag="true" PopupDragHandleControlID="pnlHeader"></ajax:ModalPopupExtender>
</ContentTemplate>
When the user clicks the OK button of the UserControl B, it triggers a delegate that calls a method in the UserControl A that performs updating of some fields and the update method of the UpdatePanel "upA".
The delegate:
//Delegate Call When the user clicks the ok Button
popup.okHandler = (x) =>
{
A.ChooseOpticienCode(x.Value);
};
in the UserControl A's code behind:
public void ChooseOpticienCode(string code)
{
<-- some treatments -- >
upOpticien.Update(); <-- crash here
}
When the method upOpticien.Update() is called, I get this error:
System.InvalidOperationException: The Update method can only be called on UpdatePanel with ID 'upOpticien before Render.
I don't understand why I get this error. I tried to set the UpdateMode of the UpdatePanel to "Always", the application no longer crashes but the fields are not updated.
ideas ?

I finally found how to fix my problem. Instead of calling the delegate directly, I must use the InvokeMember function so that the delegate is executed correctly and can edit the various fields and update the UpdatePanel
page.GetType().InvokeMember(OkHandler.Method.Name, System.Reflection.BindingFlags.InvokeMethod, null, page, new[] { this });
(here, OkHandler is my delegate)
However, note that using an anonymous method will not work, it's imperative that the delegate points to a method with a name for InvokeMember works.

Related

A control with ID could not be found - One Update Panel has trigger, another UpdatePanel needs to be updated

I have an 2 Update Panel Namely (IDs) as upDMList for UpdatePanel 1 and UpdatePanel2 for second UpdatePanel. I have a Gridview inside UpdatePanel1 which has LinkButton. This Link button need to act as Trigger for second Update Panel but it always shows error
I have tried various methods
For Example - When I target Control using Gridview ID it shows this error
Exception Details: System.InvalidOperationException: A control with ID 'gvDMList$lnkAccountImage' could not be found for the trigger in UpdatePanel 'UpdatePanel2'.
When I Target Control Using only Link Button ID it shows Error
A control with ID 'lnkAccountImage' could not be found for the trigger in UpdatePanel 'UpdatePanel2'.
Even while targetting using Update Panel 1 ID the error is
A control with ID 'upDMList$gvDMList$lnkAccountImage' could not be found for the trigger in UpdatePanel 'UpdatePanel2'.
Both UpdatePanel has ChildrenAsTriggers set to true and ClientIDMode=Static
Any help would be useful
Here is relevant code
`<asp:UpdatePanel ID="upDMList" runat="server" ClientIDMode="Static" ChildrenAsTriggers="true" >
<ContentTemplate>
<div class="table-responsive">
<asp:GridView ID="gvDMList" ClientIDMode="Static" AutoGenerateColumns="false" style="margin: 0 auto" DataKeyNames="DMID" OnRowCreated="gvDMList_RowCreated" OnRowDataBound="gvDMList_RowDataBound" OnRowCancelingEdit="gvDMList_RowCancelingEdit" OnRowEditing="gvDMList_RowEditing" OnRowUpdating="gvDMList_RowUpdating" OnPageIndexChanging="gvDMList_PageIndexChanging" CssClass="table-bordered table-striped table-hover table-condensed" runat="server">
<Columns> <ItemTemplate>
<asp:LinkButton runat="server" CommandName="GetImage" ClientIDMode="Static" CommandArgument='<%#Bind("FileName") %>' ID="lnkAccountImage" OnClick="lnkAccountImage_Click" CssClass="btn btn-primary btn-xs" Text="Show Image"></asp:LinkButton></td>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ButtonType="Button" ControlStyle-CssClass ="btn btn-default" CausesValidation="false" ShowEditButton="true" ShowCancelButton="true"/>
</Columns>
</asp:GridView>
</div>
</ContentTemplate>
</asp:UpdatePanel>`
Second Update Panel
<asp:UpdatePanel ID="UpdatePanel2" ChildrenAsTriggers="true" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<div class="container">
<asp:Image ID="imgAcnt" CssClass="img-responsive img-thumbnail center-block" AlternateText="Account Details Image" runat="server" />
</div>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="lnkAccountImage" EventName="Click" />
</Triggers>
</asp:UpdatePanel>

"<!" character combination in ASP.NET textbox prevents postback

So this is very strange, and I haven't been able to find anything about it while searching.
I have an ASP.NET webforms app, and I am using the AJAX Control Toolkit's Modal Popup extender. Inside of this popup, I have a multi line textbox and some buttons. Both are ASP control. I have found that after entering the character combination "
Here is a picture of the dialog:
The dialog that won't work
And here is the markup for it:
<div id="notes" style="display: none">
<asp:UpdatePanel id="upnlNotesHeader" runat="server" class="logHeader">
<ContentTemplate>
<asp:Label ID="lblNotes" runat="server"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
<div class="logBody" style="height:200px">
<asp:UpdatePanel ID="upnlNotes" runat="server">
<ContentTemplate>
<asp:HiddenField ID="hdnNotesJswo" runat="server" />
<asp:TextBox ID="txtNotes" runat="server" TextMode="MultiLine" MaxLength="4000" Font-Names="Arial" Rows="12" Columns="62" style="margin: 5px 0 0 5px"></asp:TextBox>
</ContentTemplate>
</asp:UpdatePanel>
</div>
<div class="logBtns">
<asp:UpdatePanel runat="server">
<ContentTemplate>
<asp:Button ID="btnSaveNotes" runat="server" Text="Save" CssClass="logBtn" OnClick="btnSaveNotes_Click" />
<asp:Button ID="btnCancelNotes" runat="server" Text="Cancel" CssClass="logBtn" OnClick="btnCancelNotes_Click" />
</ContentTemplate>
</asp:UpdatePanel>
</div>
</div>
Does anyone know why this is happening?
The problem comes from the request validation performed by ASP.NET when submitting the page. You can turn it off at the page level like this:
<%# Page ValidateRequest="false" ... %>
An alternative, if you want to keep the validation, is to encode the content of the fields before submitting the form to the server, and decode it in code-behind.

AJAX rating control inside update panel leads to reload the complete page

I have used AJAX Rating control for star rating. I am trying to achieve rating without page postback, therefore I placed inside update panel but still page is used to reload.
Below is aspx page code:
<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<cc2:Rating ID="Rating1" AutoPostBack="true" OnChanged="OnRatingChanged" runat="server"
StarCssClass="Star" WaitingStarCssClass="WaitingStar" EmptyStarCssClass="Star" FilledStarCssClass="FilledStar">
</cc2:Rating>
<div style="text-align: left; padding-right: 2px;">
<asp:Label ID="lblRatingStatus" runat="server" Text=""></asp:Label>
</div>
</ContentTemplate>
</asp:UpdatePanel>
Change update mode of updatepanel from conditional to always and set ChildrenAsTriggers="True". Should work in your case.
Let me know if it doesn't work.

Button postback getting error

I have a updatepanel with a listview inside. This panel refreshes with an interval of 10 seconds, and listview source is updated and rebinded. My listview have some imagebuttons, and here comes the problem. When I click any of the listview buttons, I get a error:
"Invalid postback or callback argument." error.
I'm trying everything that I see on my serches, and no, I'll not set validation to false.
Here's a simplified template of my list.
<asp:UpdatePanel runat="server" ID="pnlRegistro" ClientIDMode="Static" OnLoad="pnlRegistro_Load">
<ContentTemplate>
<asp:ListView runat="server" ID="lvRegistro" OnPreRender="lvRegistro_PreRender" >
<LayoutTemplate>
<!-- Unread notification count -->
<span runat="server" id="spanRegistroCount" class="mws-dropdown-notif">
<asp:Label ID="lblRegistroCount" runat="server" Text="0"></asp:Label>
</span>
</LayoutTemplate>
<ItemTemplate>
<li class="<%# Convert.ToInt32(Eval("RegistroHorarioId"))==0?"read":"unread"%>">
<a>
<div style="width: 80%; float: left;">
<span class="message" <%#Eval("SpanColor")%>>
<%#Eval("Status")%>
</span>
</div>
<div style="float: right">
<asp:ImageButton runat="server" ID="btnRegistro" OnClick="btnRegistro_Click"/>
</div>
</a>
</li>
</ItemTemplate>
</asp:ListView>
</ContentTemplate>
</asp:UpdatePanel>
What can I do to fix this error and fire "btnRegistro_Click" event?
Update Panel is used for Updating Partial Page instead of doing full postback.
So,you have to add PostBackTrigger sync or async whatever you want because you are using update panel and for update panel events are defined in triggers to avoid full postback, so most probably you want asnc postback trigger.
add trigger for button event after content template end tag (</ContentTemplate>):
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnRegistro" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
you should also read Understanding ASP.NET AJAX UpdatePanel Triggers

UpdatePanel stops CommandArgument from Updating

I have a button nested inside an update panel which has a CommandArgument tied to it. This calls a method which updates some label and text in an area not contained in the UpdatePanel. If I comment out the update panel the button works correctly so I know it is coming from the update panel. Anyone know how I can pass this through?
protected void Button_Command(object sender,
System.Web.UI.WebControls.CommandEventArgs e)
{
//update textboxes and labels here
}
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<div>
<asp:DataList ID="dListItems" runat="server" DataKeyField="PRODUCT_ID" RepeatColumns="4"
RepeatDirection="Horizontal" ShowFooter="False" ShowHeader="False" CellPadding="4">
<HeaderTemplate>
No Record Found....!
</HeaderTemplate>
<ItemTemplate>
<asp:Button ID="Button" runat="server" Text="Add to Cart"
CommandArgument='<%# Eval("Id") %>' CausesValidation="False"
CssClass="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only"
OnCommand="Button_Command"
/></span></span></p>
</ItemTemplate>
</asp:DataList>
</div>
</td>
</tr>
</table>
</div>
</ContentTemplate>
which updates some label and text in an area not contained in the
UpdatePanel
That's the problem. UpdatePanel will only update what is within and not what is outside. Try to put those control also the UpdatePanel and see them worked

Categories