SP2010 - httpcontext.response.write () not working for LinkButton onClick event

Probably a simple mistake I missed (although I vaguely recall some obscure blog post about the inner workings of Response.Write doesn't work as expected in some situations, but I don't remember if this is one of them):

The situation is that I have a Link button on a control running in SP2010 and if I don't use HttpContext.Response.Write () everything works as expected (i.e. I can change the .Text value for the label). However, if I call Context.Response.Write () while I can debug and step through the code, nothing else happens (nothing is written and changes to other controls are not displayed). It runs on the application page in _layouts, appearing in a modal dialog.

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

Here's some code in case you're wondering:

.aspx:

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

      

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

      

Does anyone come across something similar?

EDIT: Some more interesting pieces that might help,

  • The button itself is inside the UpdatePanel
  • I have an assigned AsyncPostbackTrigger
+2


source to share


3 answers


Using Response.Write from Web Forms code is problematic at best. As a general rule of thumb: never use Response.Write from a web form or custom control.

The reason for Response.Write is problematic because it is not part of the page management and infrastructure rendering tree. This means that when used inside events, it will render text outside the normal page flow and usually outside the proper structure of an HTML page.

It also leads to errors when you use them in combination with UpdatePanels. Since UpdatePanels are specifically designed to replace parts from a page, the framework needs to know which parts. A Response.Write happens completely outside of this and there is no real way of knowing where to display it. At best the ScriptManager will execute Response.Clear to destroy the Response.Writes, at worst you will rip the body of the UpdatePanel protocol and you will get a JavaScript error.

First, any literal <script>

tag will be ignored when doing a partial page refresh, because the innerHTML

browser function used to populate the HTML snippets sent by the server does not execute the <script>

tags.

Now with all this theory in mind, isn't there a way to execute a piece of JavaScript through an UpdatePanel? It turns out that this is much easier than just doing 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);

      

The important part is the first argument: the ScriptManager will now know when to execute the script. If you register it with a control that doesn't update when the page is partially refreshed, your script won't execute. But if the UpdatePanel containing the control is updated, your script connected to it will be executed as well. And that is usually exactly what you want.

If you always want to execute your script, no matter what panel updates you call

ScriptManager.RegisterClientScriptBlock(Page, ... );

      

+3


source


@Ruben gave a very good answer, but I felt I could add useful content that doesn't match the comments.

There are several cases in SharePoint where you use Response.Write - namely when you are dealing with web pages that show up in the SharePoint Modal popup and you want to do something nice with a callback when used window.frameElement.commitPopup()

.



The fact that you are using Response.Write in the refresh panel is actually part of your problem. When the postback generated by the refresh bar is returned, the response is formatted with UpdatePanelId|Response Content

, where UpdatePanelId

is the div associated with the refresh bar and Response Content

is the new inner HTML div file. When you use response.write this format is lost, so the ScriptManager has no idea what to do with the response and should ignore it as an error. @Ruben gave you a way to register scripts with UpdatePanel.

+3


source


Context.Response ... must have lower case context

t

context.Response.Flush()

      

etc.

or will I miss the point?

+2


source







All Articles