NoBotState is always InvalidBadResponse (AjaxContorlToolKit) - ajaxcontroltoolkit

ASPX Code:
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<cc1:ToolkitScriptManager ID="ToolkitScriptManager2" runat="server">
</cc1:ToolkitScriptManager>
<asp:TextBox ID="txtLastNm" runat="server" CssClass="ETextField1"></asp:TextBox>
<asp:ImageButton ID="btnSend" runat="server" ValidationGroup="Send" BorderWidth="0" BackColor="Transparent" ImageUrl="~/images/Buttons/submit.png" Height="34" Width="100" onclick="btnSend_Click" />
<cc1:NoBot ID="NoBot1" runat="server" OnGenerateChallengeAndResponse="NoBot1_GenerateChallengeAndResponse" ResponseMinimumDelaySeconds="3"
CutoffMaximumInstances="5" CutoffWindowSeconds="60" />
It works locally. But when I upload it on server it shows every time InvalidBadResponse
I enter data for login and click on asp button. NoBot state is InvalidBadResponse and it fails. But, then I click on browser's refresh button it asks me to resend request I say ok and now state is valid! Why?
Same thing same problem like : AjaxControlToolkit NoBotState is always InvalidBadResponse

Check out my answer here: AjaxControlToolkit NoBotState is always InvalidBadResponse
Basically, my url re-writing was messing up the ASP.NET ajax framework libraries' urls, causing them not to load - meaning the Javascript challenge would always fail because it wasn't being executed.

Related

How to attach a listener like oncomplete to p:button

I have the following code :
<h:form id="facebookForm">
<p:button value="Facebook Connect" href="#{loginPageCode.facebookUrlAuth}">
<p:ajax event="click" listener="#{loginPageCode.getUserFromSession}" update="growl confirmDialog" ></p:ajax>
</p:button>
<p:confirmDialog id="confirmDialog" message="Welcome"
header="Welcome to our website!" severity="alert" widgetVar="confirmation">
<h:panelGrid columns="1" cellpadding="10">
<h:outputText id="msg" value="Salutation : #{user.salutation}" />
<h:outputText id="msg1" value="Name and surname: #{user.name} #{user.surname}" />
<h:outputText id="msg2" value="Birthday : #{user.birthDate}" />
<h:outputText id="msg3" value="Email : #{user.email}" />
<h:outputText id="msg4" value="Postal code : #{user.postalCode}" />
<h:outputText id="msg5" value="Phone nr : #{user.phoneNumber}" />
</h:panelGrid>
<p:commandButton id="confirmation" value="Ok" onclick="confirmation.hide()" type="button" />
</p:confirmDialog>
<p:growl id="growl" life="3000" showDetail="true" />
</h:form>
I am trying to connect to facebook from my webpage, through a custom servlet which handles all the stuff regarding validation, parsing response, etc. The href inside above form contains the url to facebook authentification, which in the end redirects everything to the current page, which is index.xhtml.
What I want to do is to call a method after everything was done, something like onComplete attribute from <p:commandButton> component, because I want to display a dialog with updated managed bean, <p:confirmDialog>, and to welcome. So I need to first go to the first URL, wait until autenthification is finished, then call this method #{loginPageCode.getUserFromSession} which dinamically updates the managed bean under the hood by parsing JSON from facebook http response, and then to open the dialog which contains updated values.
I have tried also to use <p:commandButton> with type="button" to use action / actionListener / update capabilities, but unsuccessful.
Can you please advise?
Thank you.
You're overthinking/misgrasping how HTTP and ajax in general work. It's not possible to have an ajax oncomplete hook on a synchronous GET request.
Just do the job in the (post)constructor of the request/view scoped associated with index.xhtml which FB authentication is ultimately redirecting to, if necessary based on some specific request parameters which you've specified in index.xhtml callback URL.
Basically, you need to do most of your processing in the back. Try this
Define a preRenderView event in which you'll do most of the postprocessing of the facebook login. This event will be defined in index.xhtml. Your code will look something like this
<f:metadata>
<f:event type="preRenderView" listener="#{loginPageCode.postFBProcessing}" />
</f:metadata>
In postFBProcessing, you'll carry out whatever processing you need to do after return from FB (processing the JSON response, getting user from session etc).
Define a boolean dialogVisible in your backing bean and bind the <p:dialog/>'s visible property to the variable OR use RequestContext to update the dialog.
<p:confirmDialog id="confirmDialog" message="Welcome" visible="#{loginPageCode.isDialogVisible}" header="Welcome to our website!" severity="alert" widgetVar="confirmation">

grails form redirect gives 404, but url mapping works

I feel like I've entered the Twilight Zone. I have a Grails form that redirects to a URL, but gives a 404. If I go directly to that exact URL, everything works fine (that is, the URL mappings are correct).
This is an example generated form tag:
<form action="/alm/contactRefresh/itemRefreshProcess/7070" method="post">
On submit, it redirects to:
http://localhost:8080/alm/contactRefresh/itemRefreshProcess/7070
But gives this error:
HTTP ERROR 404
Problem accessing /alm/contactRefresh/itemRefreshProcess/7070. Reason:
NOT_FOUND
Powered by Jetty://
But then if I just go directly to that same URL (by focusing the browser Location bar and pressing enter), the page renders just fine, though the form params are lost because it's just a GET now. I've also tried changing the form method to GET, and that doesn't work either (throws a 404).
I've done similar forms a zillion times before with no problems. I'm sure this is some stupid user error, but I seriously can't figure out what's wrong.
Thanks for any ideas!
So, I finally started ripping parts out of the form and found out that for some reason you can’t name a Grails checkbox starting with the word "action". It must be something related to the default params["action"] entry. Though my checkbox names were a concatenation of "action_" + an id.
Anyway, there was some kind pre-processing of the checkbox form params that was blowing up before making it to the controller, and somehow that translated to a 404 instead of an actual Exception.
Originally I had this:
<g:checkBox name="action_${serviceRefreshAction.id}" value="${true}" />
Which renders this:
<input type="hidden" name="_action_7196" /><input type="checkbox" name="action_7196" checked="checked" id="action_7196" />
I changed "action" to "myAction", like this:
<g:checkBox name="myAction_${serviceRefreshAction.id}" value="${true}" />
Which renders this:
<input type="hidden" name="_myAction_7206" /><input type="checkbox" name="myAction_7206" checked="checked" id="myAction_7206" />
And now everything works fine.
Five hours of my life down the drain.
But I guess I have to forgive Grails, for the all time it saves me on a daily basis normally. :o)

How to Update asp:Label after AJAX Callback

I'm using the ajaxtoolkit:Rating. It all works fine except I'm trying to write a value to an asp label on the changed event and can't get it to work. Here are the relevant lines of code:
page.aspx
<asp:LoginView ID="LoginView1" runat="server">
<LoggedInTemplate>
<td>
<ajaxtoolkit:Rating ID="YourRating" runat="server" BehaviorID="RatingBehavior1" CurrentRating="0"
MaxRating="10" StarCssClass="ratingStar" WaitingStarCssClass="savedRatingStar"
ReadOnly="false" FilledStarCssClass="filledRatingStar" EmptyStarCssClass="emptyRatingStar"
OnChanged="YourRating_Changed" />
</td>
<td>
(<asp:Label ID="YourRatingNumber" Text="" runat="server"></asp:Label>)
</td>
</LoggedInTemplate>
<AnonymousTemplate>
<td>
Login or Register to Rate
</td>
</AnonymousTemplate>
page.aspx.cs
protected void YourRating_Changed(object sender, AjaxControlToolkit.RatingEventArgs e)
{
((HtmlGenericControl)FindControl("MainContent_LoginView1_YourRatingNumber")).InnerHtml = e.Value;
}
What I'm trying to do is update my asp:label control named "YourRatingNumber" text to e.value. I've tried many ways. I realize the asp:label is rendered as a span tag but I can't seem to access that value either. How can do this?
Thanks ... Bob
AJAX is asynchronous, meaning that a page load does not occur. Changing the value of a label server-side during an AJAX operation causes nothing client side. You would need to signal the client to update the page. You should use javascript to do this. I do not think you can do this using Web Forms. If you use MVC, you can use their AJAX helpers to do this, but it would require rewriting the site.
I would suggest that you write some javascript to cause an AJAX event and have it update the label when it gets a response. I would also suggest reading http://w3schools.com/ajax/default.asp, it explains how to use javascript to create AJAX requests, but you would need to tweak it a little to work with asp.net.
Also note, you should include whether you are using Web Forms or MVC, although they are similar, they have different handling of AJAX.

SiteFinity 4.0: Trouble Accessing Postback Data in Custom User Control

I'm trying to get a highly customized hand built form into sitefinity 4.0 and my problem is that no matter what I do I can't access the form's postback data in the code behind. My form is a user control and I've added it in the way described here:
http://www.sitefinity.com/40/help/developer-manual/controls-adding-a-new-control.html
After struggling for several hours, I created a basic test form and I'm still not able to access the postback data. I've also tried adding EnableViewState="true" all over the place but the form data is still empty on postback. The exact same user control runs and posts data perfectly outside of sitefinity. I also tried other methods of accessing the postback data and I discovered that Request.Form does contain the data I need. I'd still love to access my form elements in the usual way though, so I don't have to do Request.Form for every control on the page and loop that way, which seems really hokey.
Here's the code for the basic form:
"BasicUserControl.ascx"
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="BasicUserControl.ascx.cs" Inherits="SitefinityWebApp.UserControls.Assessments.BasicUserControl" EnableViewState="true" %>
<div id="assessmentDiv" runat="server">
<asp:TextBox ID="TextBox1" runat="server" clientidmode="Static" enableviewstate="true"></asp:TextBox>
<asp:Literal ID="Literal1" runat="server" clientidmode="Static" enableviewstate="true"></asp:Literal>
<asp:Button ID="Button1" runat="server" Text="Button" />
</div>
"BasicUserControl.ascx.cs" Code Behind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace SitefinityWebApp.UserControls.Assessments
{
public partial class BasicUserControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
Literal1.Text = TextBox1.Text;
}
}
}
}
Again, if I add the control via the method described at the link above, I am able to successfully create a Sitefinity 4.0 CMS page, drag the control onto it, run the page, step into the code behind using the debugger, yet when VS2010 reaches the line below there is no form data being posted:
Literal1.Text = TextBox1.Text;
FYI: The reason why there is no form tag in my usercontrol.ascx code above is because I get an error when running the form thru sitefinity that only one server-side form tag can exist on a .net page (sitefinity injects it's own form tag).
Thanks in advance for your help!
Ben
Never mind - I figured it out. For some reason, data isn't available at the Page_Load stage of the .net page lifecycle in a sitefinity form submission (at least not via a custom user control). If I wait until the Page_PreRender stage to retrieve the data from the form on the page, it's all there.
My current prevailing theory is that Sitefinity 4.0 grabs the postback data when a form submits and hasn't finished monkeying around with it at the Page_Load stage, so you have to wait until the Page_PreRender stage before sitefinity has injected the data back into the page cycle.
I had the same issue you did and came to the same solution. Spent a bunch of time on it - wish this answer had been around then.
Try adding enableviewstate into masterpages. I had the same situation and solved with this.
Also checked "enable view state" when creating pages.
Hope this help.
`<%# Master Language="C#" AutoEventWireup="true" CodeFile="HomePageClubManavgat.master.cs" Inherits="App_Master_HomePageClubManavgat" EnableViewState="true" %>
<script type="text/C#" runat="server">
protected override void OnInit(EventArgs e)
{
this.Page.EnableViewState = true;
base.OnInit(e);
}
`

Reload MVC2 user control with jQuery

I know I've had a ton of questions today, but still trying to get everything under control learning MVC 2 tie right way. Before I get into the question I already tried the solution offered here but I get an 500 internal server error.
So here's what I'm trying to do, I give the user the ability to select from a list of skills (loaded in a ListView user control) or add a new one to the list and select that. The adding new one is completed (using a WCF service & jQuery) but now I'm trying to reload the skills user control.
According to the solution I linked to I added an action to my AccountController
public ActionResult GetSkillControl()
{
return View("~/Views/Shared/SkillsListView.ascx");
}
I have the control inside a span (so it has a container)
<tr>
<td style="vertical-align:top;"><label for="SkillsListView" title="Skills">Skills:</label></td>
<td class="regElements"><span id="SkillListViewContainer"> <% Html.RenderPartial("SkillsListView"); %></span><%= Html.ValidationMessageFor(m => m.Skills, "*")%><br />
<span id="AddSkillError"></span>
Add: <input type="text" id="NewSkill" class="inputbox" style="width:75px;" /> <input type="button" value="Add" id="AddSkill" name="AddSkill" /></td>
<td></td>
</tr>
And in my jQuery ajax call I have
success: function () {
$('#SkillListViewContainer').load('../AccountController/GetSkillControl');
}
It's when it reaches that point that the JavaScript console in Chrome shows it returns a 500 internal server error. What am I missing here?
Based on the standard routes in ASP.NET MVC, try "/Account/GetSkillControl". Anything you use from jQuery's load method must be a valid URL. The route engine is looking or Account and not AccountController. Also by using the leading "/" it will resolving from the root of the site.