Asp.net按钮onclick在updatepanel中不起作用

问题描述:

我的问题是,asp按钮的onclick在updatepanel内部不起作用。基本上我的代码是什么,我有一个asp文件上传控件来上传文件。然后,用户现在可以单击按钮,上传文件的文件名将显示在asp标签控件中。



但是当我试用它时,它没有在标签中显示文件名。它什么都不做。



我尝试过:



这是aspx:



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:

<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:

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





顺便说一下,我之所以想使用updatepanel,是为了避免在用户刷新整个页面点击按钮。请帮助我。



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.

您的表单中是否有脚本管理器?

这样的标签:

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





如果你发布你的帖子,这将有助于每个人整个asp页面在这里。

我可以看到你正在尝试使用fileupload控件。你可以测试这段代码:



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: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:

protected void Upload(object sender, EventArgs e)
{
    string fileName = System.IO.Path.GetFileName(FileUpload1.FileName);
    FileUpload1.SaveAs(Server.MapPath("~/Whateverfolderyouwanttouploadto/") + fileName);
}