简体   繁体   中英

Calling a code-behind function from JavaScript that may return another JavaScript function

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.

OK, for those who are interested in it, the problem was with the _ doPostBack calls. Since the arguments I passed ( _EVENTARGUMENT) were dynamic, I could never register those events through the RegisterForEventValidation method, becuase it asks for two constant strings (AFAIK): the control's UniqueID and the argument it will be passed with it.

So, I stopped passing arguments to the doPostBack other than the button's UniqueID, and passed the variables I was interested in through other media (cheifly, hidden fields values to global variables inside the Page class).

That solved the problem and made the program work as intended. I wrapped the buttons inside the same update panel than the GridView so as to not generate an AutoPostBack and change the Grid's values without having to refresh.

If anybody is interested in the code, I can provide.

OnClientClick="Click_Alquilar(); return false"
instead of this use
OnClientClick="return Click_Alquilar();


and in javascript
use return false;
in functions like

function Click_Alquilar() {
        if (index == '') {
            alert("Debe elegir una película para alquilar");
return false;
        }
        else {

            if (confirm("¿Quiere alquilar la película '" + selected.childNodes[2].innerText + "'?")) {
                __doPostBack('<%= btnAlquilar.UniqueID %>', index);
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM