开发者

How to hide/show a control using AJAX (AjaxControlToolkit) and C#

I know this must sound really basic but I'm really stumped here. What I'm trying to do is to show a Hyperlink once a process has completed. And this process is the AsyncFileUpload. In the ASPX page, I want to create an but have it hidden on the initial page load. If I set the Style="display: none;" seems to work but after the file upload, nothing I do, will make the control visible again. When the file is uploaded, it calls a function called FileUploadComplete. It's in here that no matter what I do, the Hyperlink won't display.

Any help is greatly appreciated :)

Thank you, dave

Here is the ASPX Code (with recently added javascript)

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="OptionsPlaceHolder" runat="server">
    <script language="javascript" type="text/javascript">
        function ShowLink() {
            $(开发者_运维百科"#openFile").show();
        }
    </script>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolderBody" runat="server">
    <asp:UpdatePanel ID="updImportFile" runat="server" UpdateMode="Conditional">
        <ContentTemplate>
            <div class="pageHeader">           
                <asp:Literal runat="server" ID="pageTitle" Text="<%$ Resources:Resources, ImportFile %>" />
            </div>
            <ajaxToolkit:AsyncFileUpload ID="FileUpload1" runat="server" Width="600px" 
UploaderStyle="Traditional" OnUploadedComplete="FileUploadComplete" ThrobberID="throbber" 
               CompleteBackColor="#E9F2FD" OnClientUploadComplete="ShowLink" />
            <asp:Image runat="server" ID="throbber" ImageUrl="images/loading.gif" />                    
            <br />
            <asp:Hyperlink runat="server" ID="openFile" NavigateUrl="~/OpenFile.aspx" Text="Open" 
style="display:none;"/>
        </ContentTemplate>
    </asp:UpdatePanel>        
</asp:Content>

And here is the code behind:

 protected void FileUploadComplete(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            string importName = Server.MapPath(@"Uploads\") + FileUpload1.FileName;
            FileUpload1.SaveAs(importName);

            // Import the JSA
            JSA jsa = new JSA();
            jsa.Import(importName);

            // Show the Hyperlink
            ShowLink();
        }
    }

    private void ShowLink()
    {
        openFile.Attributes["Style"] = string.Empty;
    }

I didn't include the master page code. It has the ToolkitScriptManager in it.


Are you trying to show on client-side or server-side? Is the link a client-side, or server-side object? Javascript would be the standard way.

If the control is a client-side object:

document.getElementById("hyperlink_name").style.display = "block"; 

Or if it is a server-side object:

document.getElementById("<%= hyperlink_name.ClientID %>").style.display = "block"; 

I would recommend getting jQuery and using the following though:

$('#hyperlink_name').show(); 

Or you can use an ASP.Net Link Button and do it server-side:

linkButton.Visible = true; 


It would be more helpful if you would post some of the code you have tried already so that we can get a better idea of where you are.

{first answer deleted}

[EDIT :I didn't catch that you are using AsyncFileUpload when I first read the question]

Using AsyncFileUpload inside an update panel the server is being accessed via a partial postback, as a result other controls (the hyperlink) cannot be affected on the server. This will require that you make use of javascript (or preferably jquery) to make the change on the client.


You can do it on OnClientUploadComplete function, but you have to reference the hyperlink like this: <%= hyperLink.ClientID %>.style.display = 'block';

Another more asp.net way is to use update panels. Put the hyperlink into an UpdatePanel and set a trigger on the UpdatePanel when the file is uploaded. then change the visibility on the server at UploadedComplete event.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜