Button postback getting error - c#

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

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>

User Control, Delegate and UpdatePanel Issue

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.

"<!" 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.

asp.net updatePanel PostBackTrigger triggering full postback

Below is the markup page
<asp:UpdatePanel runat="server" UpdateMode="Conditional">
<ContentTemplate>
<table>
<tr>
<td>
<label>File name<label>
</td>
</tr>
<tr>
<td>
<asp:TextBox runat="server" ID="txtName" Width="150%"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<label>File</label>
</td>
<td>
<asp:FileUpload runat="server" ID="fileUpload" />
</td>
</tr>
</table>
<asp:Button runat="server" ID="btnUpload" Text="Upload file" OnClick="btnUpload_Click" />
</div>
<div class="panel-heading">File display</div>
<div class="panel-body">
<asp:GridView runat="server">
</asp:GridView>
<asp:Button runat="server" ID="btnRefresh" Text="Refresh" OnClick="btnRefresh_Click" />
</div>
</div>
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="btnUpload" />
</Triggers>
</asp:UpdatePanel>
With the above code, whenever the user clicks "btnUpload", there'll be a post back, but there is an update panel to prevent the post back, when I change the PostBackTrigger to AsyncPostBackTrigger, the page didn't post back but the fileUpload.HasFile will be false and was unable to get the file.
What's wrong with the above code?Why the postBackTrigger triggers a post back?
File upload will not work with AsyncPostBackTrigger. It will only work with PostBackTrigger.
SOLUTION : If You want to upload file with AsyncPostBackTrigger than you have to use ajaxtoolkit. And in AjaxToolkit their is Control name with "AsyncFileUpload".
Hope You know how to add AjaxToolkit to your Toolbox. If you dont know than tell me i will guide you.
You have not defined event of control, you have to tell that trigger on which event of control:
<asp:AsyncPostBackTrigger ControlID="btnUpload" EventName="Click" />
UPDATE:
you have to use async postback trigger:
AsyncPostBackTrigger - use these triggers to specify a control within or outside of the UpdatePanel that, when clicked, should trigger a partial page postback.
PostBackTrigger - use these triggers to have a control within the UpdatePanel cause a full page postback rather than a partial page postback.

Categories