开发者

[asp.net]problem with refreshing the updatepanel from server code

In my app I have a simple upload with jquery and custom http handler binded to button and an update panel to show the uploaded file name:

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:Button ID="btnUploadFile" runat="server" Text="Button" />
    <%= System.DateTime.Now %>
    <input id="File1" type="file" />
    <asp:UpdatePanel ID="UpdatePanel1" runat="server" ChildrenAsTriggers="true" UpdateMode="Conditional">
        <ContentTemplate>
            <br />
            <br />
            <%= System.DateTime.Now %>
            <asp:Label ID="Label1" runat="server" Text="" ViewStateMode="Disabled"></asp:Label>
        </ContentTemplate>
    </asp:UpdatePanel>
</asp:Content>

$(document).ready(function ()
{
    $(function ()
    {
        $('#file_upload').fileUpload({
            url: '../UploadHandler.ashx',
            method: 'POST',
            onLoadAll: function (list)
            {
               // __doPostBack('MainContent_UpdatePanel1', '');
            }
        });
    });

    $('#File1').hide();

    $('#MainContent_btnUploadFile').click(function ()
    {
        $('#File1').click();

        return false开发者_StackOverflow中文版;
    });
});

The handler looks that:

public void ProcessRequest(HttpContext context)
        {
            HttpPostedFile uploadedfile = context.Request.Files[0];

            string fileName = uploadedfile.FileName;

            IMainFormViewPresenter presenter = Bootstrapper.ServiceLocator.GetService<IMainFormViewPresenter>();
            //do sth..
            presenter.SetFileInfo(fileName);
            presenter.RefreshUpdatePanel();
        }

and in the view

public void RefreshUpdatePanel()
        {
           // UpdatePanel1.Update();
        }
public void SetFileInfo(string fileName)
        {
            Label1.Text = fileName;
        }

The problem is that, the UpdatePanel1.Update() in the server code doesn't work. Sometimes I get exception: The Update method can only be called on UpdatePanel with ID 'UpdatePanel1' before Render.

I can't find out what is going on. The update panel refreshed from javascript work ok, but I'm curious why on the server is problem. Full solution I put here https://dl-web.dropbox.com/get/Public/WebApplication1.rar?w=ea9959b6


Since the button is outside of your update panel you will need to add a trigger to your update panel that has the controlID of your button and the event of click so in your update panel add the following:

<asp:UpdatePanel ID="UpdatePanel1" runat="server" ChildrenAsTriggers="true" UpdateMode="Conditional">
    <ContentTemplate>
        <br />
        <br />
        <%= System.DateTime.Now %>
        <asp:Label ID="Label1" runat="server" Text="" ViewStateMode="Disabled"></asp:Label>
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="btnUploadFile" EventName="Click" />
    </Triggers>
</asp:UpdatePanel>

This will tell your update panel to treat the button as if it were inside of the update panel and to participate in partial postbacks as if it were inside of the update panel.

HTH.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜