Click here to Skip to main content
16,016,669 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My problem is, the asp button's onclick is not working inside the updatepanel. Basically what my code does is, I have an asp fileupload control to upload a file. And then afterwards, the user can now click the button and the filename of the uploaded file will be displayed in the asp label control.

But when i tried it, it is not displaying the filename in the label. It does nothing.

What I have tried:

Here is the aspx:

C#
<asp:UpdatePanel runat="server" UpdateMode="Always" ID="updPnlName" 
    ChildrenAsTriggers="true">
                         <ContentTemplate>
                              <asp:FileUpload runat="server" ID="Image1"/>
                                   <asp:Button ID="Button2" runat="server" 
                                    Text="Upload" OnClick="Button2_Click"/>
                             </ContentTemplate>
                            <Triggers>
                               <asp:AsyncPostBackTrigger ControlID="Button2" 
                                    EventName="Click"/> 
                            </Triggers>
                     </asp:UpdatePanel>
                     <asp:Label ID="Label3" runat="server" Text="Label">
  </asp:Label>


Here is the code-behind:

C#
public void Button2_Click(Object sender, EventArgs e)
{
    var filename1 = Image1.FileName;
    Label3.Text = "Upload successfull - " + filename1;
}


By the way, the reason why i wanted to use updatepanel is to avoid refreshing the entire page when the user clicked the button. Please kindly help me on this one.
Posted
Updated 15-Aug-17 4:22am
Comments
P_Z 15-Aug-17 10:16am    
Hi, I think you should avoid using UpdatePanels...you can achieve the same functionality using JavaScript/jQuery and Ajax calls.

Might be interested to read: https://stackoverflow.com/questions/1151821/advantages-and-disadvantages-of-using-ajax-update-panels-in-asp-net-application and similar articles

1 solution

Do you have a script manager in your form?
A tag like this:
ASP.NET
<asp:ScriptManager ID="ScriptManager1" runat="server" />


It would help everyone here if you post your entire asp page here.
I can see that you are trying to use fileupload control.You can test this code:
ASP.NET
 <asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
 <asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
    <div style="margin: 30px">
        <asp:FileUpload ID="FileUpload1" runat="server" />
        <asp:RequiredFieldValidator ErrorMessage="Required" ControlToValidate="FileUpload1"
            runat="server" ForeColor="Red"></asp:RequiredFieldValidator>
        <asp:Button ID="btnUpload" Text="Submit" runat="server" OnClick="Upload" />
    </div>
</ContentTemplate>
<Triggers>
    <asp:PostBackTrigger ControlID="btnUpload" />
</Triggers>
</asp:UpdatePanel>


And then in code behind:
C#
protected void Upload(object sender, EventArgs e)
{
    string fileName = System.IO.Path.GetFileName(FileUpload1.FileName);
    FileUpload1.SaveAs(Server.MapPath("~/Whateverfolderyouwanttouploadto/") + fileName);
}
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900