简体   繁体   English

使用JavaScript防止从GridView进行异步回发

[英]Prevent an asynchronous postback from gridview with javascript

I have a gridview inside an update panel with a checkbox in the first column of the gridview. 我在更新面板中有一个gridview,在gridview的第一列中有一个复选框。 When I first created it, it worked fine. 当我第一次创建它时,它运行良好。 The checkbox is set to AutoPostBack and causes an asynchronous postback where I can process the data and refresh the gridview without the rest of the page being affected. 复选框设置为AutoPostBack,并导致异步回发,在这里我可以处理数据并刷新gridview,而不会影响页面的其余部分。

But, I need to test for a certain condition on the page, client side, and display a confirm box to users that says 'This person is not set up for this. 但是,我需要在页面(客户端)上测试某种条件,并向用户显示一个确认框,提示“此人未为此设置”。 Are you sure you want to continue?' 你确定你要继续吗?'

I did this, initially, by putting a bit of code like this in the RowDataBound event of the gridview; 最初,我是通过在gridview的RowDataBound事件中放入类似的代码来完成此操作的;

CheckBox cb = (CheckBox)e.Row.FindControl("myCheckBox");
cb.Attributes.Add("onclick", "return check('" + ContactName + "');");

And had a javascript function along the lines of: 并具有以下JavaScript函数:

function check(ContactName)
{
  if (document.getElementById('Listed') == false)
  {
  if (confirm('This person is not listed, are you sure you want to continue?'))
    {
    return true;
    }
    return false;
  }
}

Please ignore typos in the code above, just typed if from memory. 请忽略上面代码中的错别字,只要是从内存中键入就可以。 The thing is, it works. 事实是,它有效。 If my local variable Listed is false, it throws up the confirm box okay. 如果我的局部变量Listed为false,则抛出确认框。

The problem is, if you select 'Okay' in the confirm box - the Postback (which should be asynchronous as it is inside an Update Panel with ChildrenAsTriggers = true) generated by the Checkbox is cancelled. 问题是,如果在确认框中选择“确定”,则取消了由复选框生成的回发(它应该是异步的,因为它位于带有ChildrenAsTriggers = true的更新面板中)。 I am having to call __doPostback after the javascript confirm to get the Postback to occur. 我必须在javascript确认后调用__doPostback才能发生Postback。 But, then, I am getting a full postback, not the asynchronous postback I want. 但是,然后,我得到了完整的回发,而不是我想要的异步回发。

So, my question is, how do you prevent the asynchronous postback from a checkbox with a javascript confirm so that, if you select Okay in the javascript confirm, the asynchronous postback takes place. 因此,我的问题是,如何通过带有JavaScript确认的复选框阻止异步回发,因此,如果在javascript确认中选择“确定”,则会发生异步回发。

As I say, at the moment, the only way I can get it to work is do a full postback after the user clicks Okay. 正如我所说,目前,让它起作用的唯一方法是在用户单击“确定”之后执行完整的回发。

The link at this location discusses how to cancel an async postback via JavaScript: 此位置上的链接讨论了如何通过JavaScript取消异步回发:

http://www.ajaxtutorials.com/ajax-tutorials/cancel-asynchronous-postback-in-c/ http://www.ajaxtutorials.com/ajax-tutorials/cancel-asynchronous-postback-in-c/

The important part is here: 重要的部分在这里:

<script type="text/javascript">
var instance = Sys.WebForms.PageRequestManager.getInstance();
instance.add_initializeRequest(instance_initializeRequest);

function instance_initializeRequest(sender, args) {
    if (args.get_postBackElement().id == 'btn_Cancel') {
        instance.abortPostBack();
        alert('Postback has been cancelled. Time will not be updated.');
    }
}
</script>

The way Garrison Neely provided should be work for you. 驻军Neely提供的方式应该适合您。

The difference between postback and async postback is async just modify some value of form before submit, and set them back after that. 回发和异步回发之间的区别是异步只是在提交之前修改form的某些值,然后在提交后将其设置回去。 the values are indicating which control initiate the request with what kinda information, also some target information used for which iframe should the response html be rendered into. 这些值指示哪个控件以什么样的信息来发起请求,还提供用于将响应HTML呈现到哪个iframe的一些目标信息。 i just bring some of my thought here, the implementation vary in different situations. 我只是在这里提出一些想法,在不同情况下,实现方式会有所不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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