从JavaScript调用一个code-功能背后可能返回另一个JavaScript函数 [英] Calling a code-behind function from JavaScript that may return another JavaScript function

查看:130
本文介绍了从JavaScript调用一个code-功能背后可能返回另一个JavaScript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的问题,即一个按钮,而不是适用于另一个。

这作品之一是调用ModalPopup新行添加到GridView控件的UpdatePanel内的按钮。如果它是成功的,它会弹出一个消息警报,与异常消息别的另一个警报。的code是非常相似的其他人的,除了它在一个ModalPopupExtender

抛出异常称为对EventValidation按钮进入如下:

网站

 < ASP:按钮的ID =btnAlquilar=服务器文本=Alquilar的CssClass =AdminButtons
                                 的OnClientClick =Click_Alquilar();返回false/>

JavaScript函数调用

 函数Click_Alquilar(){
        如果(指数==''){
            警报(德贝elegir UNA PELICULA第alquilar);
        }
        其他{            如果(确认(¿Quiere alquilar LA PELICULA'+ selected.childNodes [2] .innerText +?)){
                __doPostBack('<%= btnAlquilar.UniqueID%GT;',索引);
            }
        }
    }

其中指数是在GridView所选行的索引(与类似的架构完成,正常工作)。

在code,后面开始在Page_Load方法,并要求我遇到的麻烦功能:

 保护无效的Page_Load(对象发件人,EventArgs的发送)
        {
            如果(!Page.IsPostBack)
            {...}
            其他
            {
                ProcessAjaxPostBack(发件人,E);
            }
        }
私人无效ProcessAjaxPostBack(对象发件人,EventArgs的发送)
        {
            如果((Request.Params [__ EVENTTARGET] = NULL)及!&安培;!(Request.Params [__ EVENTARGUMENT] = NULL))
            {
                ...                如果(Request.Params [__ EVENTTARGET] == this.btnAlquilar.UniqueID)
                {
                    指数= Convert.ToInt32(Request.Params.Get(__ EVENTARGUMENT)TrimStart('R','O','W'));
                    btnAlquilar_Click(Request.Params.Get(__ EVENTARGUMENT));
                }            }
        }保护无效btnAlquilar_Click(字符串ID)
        {
            字符串消息=;
            如果(BAC.BAC.CheckUserAge(lblUserId.Text)LT; Convert.ToInt32(dgvPeliculas.Rows [指数] .Cells [7]。文本))
            {
                btnBorrar.Visible = FALSE;
                btnEditar.Visible = FALSE;
                btnNuevo.Visible = FALSE;
                System.Web.UI.ScriptManager.RegisterClientScriptBlock(页,Page.GetType(),Guid.NewGuid()的ToString(),警报('不tiene拉EDAD极小第alquilar LA PELICULA。'),真正的);
            }
            其他
            {
                尝试
                {
                    BAC.BAC.NewAlquiler(lblUserId.Text,dgvPeliculas.Rows [指数] .Cells [0]。文本,dgvPeliculas.Rows [指数] .Cells [9]。文本);
                }
                赶上(异常前)
                {
                    消息= Change_ExceptionMessage(ex.Message);
                    System.Web.UI.ScriptManager.RegisterClientScriptBlock(页,Page.GetType(),Guid.NewGuid()的ToString(),警报('没有本质pudo alquilar LA PELICULA:+消息+'),真) ;
                }
            }        }

RegisterClientScriptBlock方法是一样的我用其他的按钮(这并不做任何事情比这个更复杂​​的:如果事情是错误的,它改变在弹出的标签的文本和显示警报;如果是没错,它加载GridView和展示了成功的警报),并在那里工作。在这里,抛出异常EnableEventValidation是真的如此......。我有这个按钮注册事件验证上渲染:

 保护覆盖无效渲染(HtmlTextWriter的作家)
        {
            this.Page.ClientScript.RegisterForEventValidation(btnAlquilar.UniqueID);
            base.Render(作家);
        }

那么,为什么这发生在这里?为什么它不工作这一次?

编辑:现在,我检查,由工作按钮,在ModalPopup改变标签被包裹在一个UpdatePanel。我不知道它的问题,只是要注意了。

EDIT2:本网页也适用母版页中。不知道这是什么用途。我曾尝试都包裹编辑按钮,并与UpdatePanel的使用AsyncPostBackTrigger GridView的,但我仍然得到同样的异常。


解决方案

 的OnClientClick =Click_Alquilar();返回false
而不是此使用
的OnClientClick =返回Click_Alquilar();
而在JavaScript
使用返回false;
像功能功能Click_Alquilar(){
        如果(指数==''){
            警报(德贝elegir UNA PELICULA第alquilar);
返回false;
        }
        其他{            如果(确认(¿Quiere alquilar LA PELICULA'+ selected.childNodes [2] .innerText +?)){
                __doPostBack('<%= btnAlquilar.UniqueID%GT;',索引);
            }
        }
    }

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.

解决方案

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);
            }
        }
    }

这篇关于从JavaScript调用一个code-功能背后可能返回另一个JavaScript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆