SP2010 - httpcontext.response.write()不工作的LinkBut​​ton的的onClick事件 [英] SP2010 - httpcontext.response.write() not working for LinkButton's onClick event

查看:128
本文介绍了SP2010 - httpcontext.response.write()不工作的LinkBut​​ton的的onClick事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能是一个简单的疏忽,我已经错过了(虽然我依稀记得大概的Response.Write的内部工作按预期在某些情况下不工作一些模糊的博文,但我不记得这是否是其中之一):

probably a simple oversight I've missed (though I vaguely recall some obscure blogpost about the inner workings of Response.Write not working as expected in some situations but I don't remember if this is one of them):

的情况是,我对在SP2010运行控制链接按钮,如果我不使用HttpContext.Response.Write(),一切正常(即我可以为标签更改为.text值)。但是,如果我叫Context.Response.Write(),而我可以调试,并通过code步骤,似乎没有发生任何更多的(没有写回和改变其他控件没有出现)。它正在_layouts一个应用程序页面上运行,出现在一个模式对话框。

The situation is, I have a Link Button on a control running in SP2010, and if I don't use HttpContext.Response.Write(), everything works as expected (ie I can change the .Text value for a Label). However, if I call Context.Response.Write(), while I can debug and step through the code, nothing seems to happen any more (nothing is written back and changes to other controls do not appear). It's being run on an application page in _layouts, appearing in a modal dialog.

(基本上,我试图做到这一点 - 的 HTTP://acveer.word$p$pss.com/2011/01/25/using-the-sharepoint-2010-modal-dialog/ ,但它不工作。编辑:如果我将其更改为ASP:按钮,它仍然不工作)

(basically, I'm trying to do this - http://acveer.wordpress.com/2011/01/25/using-the-sharepoint-2010-modal-dialog/ but it doesn't work. If I change it to a asp:Button, it still doesn't work)

下面是一些code,如果你有兴趣:

Here's some code if you're interested:

的.aspx:

@ Page Language="C#" AutoEventWireup="true" ... 
<asp:LinkButton CssClass="button remove" runat="server" OnClick="remove_Click" Text="Remove" ID="remove"></asp:LinkButton>

.aspx.cs:

.aspx.cs:

    public void remove_Click(object sender, EventArgs e)
    {
        ....

        //if successful
        HttpContext context = HttpContext.Current;
        if (HttpContext.Current.Request.QueryString["IsDlg"] != null)
        {
            testControl.Text = "test code";

            //doesn't work, and prevents line above from working
            Context.Response.Write("<script type='text/javascript'>alert('hi!');</script>");
            Context.Response.Flush();
            Context.Response.End();

           // context.Response.Write("<script type='text/javascript'>window.frameElement.commitPopup()</script>");
           // context.Response.Flush();
           // context.Response.End();
        }           
    }

跨类似的东西有人进来?

Anyone come across something similar?

编辑:一些更有趣的作品,可以帮助,

some more interesting pieces that may help,


  • 按钮本身就在于一个UpdatePanel

  • 我有一个AsyncPostbackTrigger分配

推荐答案

从Web窗体code使用的Response.Write背后是有问题的最好的。作为一个经验法则:永远用不完的Response.Write从Web窗体页或用户控件

Using Response.Write from Web Forms code behind is problematic at best. As a rule of the thumb: never ever use Response.Write from a Web Forms page or user control.

的回复于原因是有问题的,是因为它是不是页的控制树的一部分,和呈现的基础设施。这意味着,在这事件中使用时将输出正常的页面流之外的文本,通常外适当的HTML页面结构。

The reason Response.Write is problematic, is because it is not part of the page's control tree, and rendering infrastructure. This means that when used within events in it will output the text outside of the normal page flow, and usually outside of the proper HTML page structure.

这也是为什么事情出差错,当你在组合使用它们的UpdatePanel。作为UpdatePanel的是专为从页面更换零件,基础设施需要知道的部分。一个发生的Response.Write完全不在这里,而且也知道去哪里渲染它没有真正的方法。在最好的情况下,ScriptManager将执行Response.Clear消灭你的Response.Writes,在最坏的情况,你会打破UpdatePanel的协议身体,你会得到一个JavaScript错误。

This is also why things go awry when you're using them in combination with UpdatePanels. As UpdatePanels are specifically designed to replace parts from a page, the infrastructure needs to know which parts. A Response.Write happens completely outside of this, and there's no real way of knowing where to render it. At best, the ScriptManager will perform a Response.Clear to wipe out your Response.Writes, at worst you'll break the UpdatePanel protocol body and you'll get a JavaScript error.

要顶部东西了,任何文字&LT;脚本&GT; 当你执行部分页面更新标签将被忽略,因为浏览器的 innerHTML的使用功能,填补了服务器发送不执行HTML片段&LT;脚本方式&gt; 标签

To top things off, any literal <script> tag will be ignored when you're performing a partial page update, as the browser's innerHTML feature used to fill in the HTML fragments sent by the server does not execute <script> tags.

现在,这一切理论的出路 - 是有没有办法通过一个UpdatePanel执行一段JavaScript代码code的?原来有,而且它不仅仅是执行一个清洁的Response.Write很多: ScriptManager.RegisterClientScriptBlock ScriptManager.RegisterStartupScript 。例如:

Now, with all this theory out of the way -- is there no way to execute a piece of JavaScript code through an UpdatePanel? It turns out there is, and it's a lot cleaner than just executing a Response.Write: ScriptManager.RegisterClientScriptBlock and ScriptManager.RegisterStartupScript. For example:

ScriptManager.RegisterClientScriptBlock(
    theButton, // control or UpdatePanel that will be rendered
    typeof(YourPage), "UniqueKey", // makes your script uniquely identifiable
    "alert('Testing!');", true);

的重要组成部分,是第一个参数:现在的ScriptManager会知道什么时候执行脚本。如果在未更新的部分页面刷新控件注册,你的脚本将不会执行。但是,如果包含控件的UpdatePanel中的的刷新,你的脚本是迷上了它也将执行。而这通常正是你想要的。

The important part is the first argument: now the ScriptManager will know when to execute your script. If you register it on a control that is not updated on a partial page refresh, your script will not execute. But if the UpdatePanel containing the control is refreshed, your script that is hooked up to it will also execute. And that's usually exactly what you want.

如果你总是要执行你的脚本,无论哪个面板的更新,你会打电话

If you always want to execute your script, regardless of which panel updates, you'd call

ScriptManager.RegisterClientScriptBlock(Page, ... );

这篇关于SP2010 - httpcontext.response.write()不工作的LinkBut​​ton的的onClick事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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