I have a problem with this that works for one button and not for another.
The one that works is a button that calls a ModalPopup to add a new row to a GridView inside an UpdatePanel. If it’s successful, it pops up an alert with a message, else another alert with the Exception message. The code is very similar to the other one’s, except it’s in a ModalPopupExtender.
The button that throws the known exception about the EventValidation goes as follow:
Web:
<asp:Button ID="btnAlquilar" runat="server" Text="Alquilar" CssClass="AdminButtons"
OnClientClick="Click_Alquilar(); return false"/>
The JavaScript function it calls
function Click_Alquilar() {
if (index == '') {
alert("Debe elegir una película para alquilar");
}
else {
if (confirm("¿Quiere alquilar la película '" + selected.childNodes[2].innerText + "'?")) {
__doPostBack('<%= btnAlquilar.UniqueID %>', index);
}
}
}
Where index is the index of the selected row in the GridView (done with a similar architecture, and works fine).
The code-behind starts in the Page_Load method, and calls the function I’m having trouble with:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{...}
else
{
ProcessAjaxPostBack(sender, e);
}
}
private void ProcessAjaxPostBack(object sender, EventArgs e)
{
if ((Request.Params["__EVENTTARGET"] != null) && (Request.Params["__EVENTARGUMENT"] != null))
{
...
if (Request.Params["__EVENTTARGET"] == this.btnAlquilar.UniqueID)
{
index = Convert.ToInt32(Request.Params.Get("__EVENTARGUMENT").TrimStart('r', 'o', 'w'));
btnAlquilar_Click(Request.Params.Get("__EVENTARGUMENT"));
}
}
}
protected void btnAlquilar_Click(string id)
{
string message = "";
if (BAC.BAC.CheckUserAge(lblUserId.Text) < Convert.ToInt32(dgvPeliculas.Rows[index].Cells[7].Text))
{
btnBorrar.Visible = false;
btnEditar.Visible = false;
btnNuevo.Visible = false;
System.Web.UI.ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), Guid.NewGuid().ToString(), "alert('No tiene la edad mínima para alquilar la película.')", true);
}
else
{
try
{
BAC.BAC.NewAlquiler(lblUserId.Text, dgvPeliculas.Rows[index].Cells[0].Text, dgvPeliculas.Rows[index].Cells[9].Text);
}
catch (Exception ex)
{
message = Change_ExceptionMessage(ex.Message);
System.Web.UI.ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), Guid.NewGuid().ToString(), "alert('No se pudo alquilar la película: " + message + "')", true);
}
}
}
The RegisterClientScriptBlock method is THE SAME I use for the other button (which doesn’t do anything more complex than this one: if things are wrong, it changes the text of a label in the Popup and shows the alert; if it’s right, it loads the GridView and shows the success alert), and works there. Here, it throws the exception “EnableEventValidation is true so…”. I have this button registered for Event Validation on Render:
protected override void Render(HtmlTextWriter writer)
{
this.Page.ClientScript.RegisterForEventValidation(btnAlquilar.UniqueID);
base.Render(writer);
}
So why does this happen here? Why it doesn’t work this time?
EDIT: Now that I check, the label changed by the working button in the ModalPopup is wrapped in an UpdatePanel. I don’t know if it matters, but just to note it.
EDIT2: The page also works within a Master page. Don’t know if it’s of any use. I have tried wrapping both the Edit button and the GridView with UpdatePanels and using AsyncPostBackTrigger, but still I get the same exception.
1 Answer