asp.net按钮onclick事件未触发。而是使用链接按钮,onclick事件正在触发,但页面重新加载自身

时间:2018-02-01 09:04:48

标签: c# asp.net

我正在使用asp.net c#。我遇到一个问题,按钮onclick事件绑定但没有触发。我不知道为什么。相反,我使用了链接按钮。在链接按钮onclick事件正在触发,但页面自行重新加载。我不知道为什么。我对这个问题有什么想法吗?

.aspx文件
  

  asp:FileUpload ID="FileUpload1_brand_image" onchange="ShowPreview(this)" runat="server"
asp:LinkButton ID="btnUpload" runat="server"  CssStyle="text-decoration:none;" OnClick="btnUpload_Click" CausesValidation="False"

.cs文件:

protected void btnUpload_Click(object sender, EventArgs e) //Profile Image  
        {  
            if (IsPostBack)  
            {  
                String product_path = Server.MapPath("~/assets/images/" + shopname.Value + "/");  
                if (!Directory.Exists(product_path))  
                {  
                    Directory.CreateDirectory(product_path);  
                }  
                String extension = Path.GetExtension(FileUpload1_brand_image.FileName);  
                FileUpload1_brand_image.SaveAs(product_path + shopname.Value + "_profileimage" + extension);  
                Session["path"] = product_path + shopname.Value + "_profileimage" + extension;  

            }  
}  

enter image description here

1 个答案:

答案 0 :(得分:0)

由于Postback,ASP.net页面重新加载正在发生。为了避免这种情况,您需要使用AJAX。 AJAX请求将避免完整页面重新加载。您可以使用JQueryAPS.net AJAX toolkit

<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <asp:FileUpload ID="FileUpload1" runat="server" />
        <asp:Button ID="btnAsyncUpload" runat="server"
           Text="Async_Upload" OnClick = "Async_Upload_File" />
        <asp:Button ID="btnUpload" runat="server" Text="Upload"
           OnClick = "Upload_File" />      
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID = "btnAsyncUpload"
          EventName = "Click" />
        <asp:PostBackTrigger ControlID = "btnUpload" />
    </Triggers>
</asp:UpdatePanel>



protected void Async_Upload_File(Object sender, EventArgs e)
{

}

protected void Upload_File(Object sender, EventArgs e)
{
  bool hasFile = FileUpload1.HasFIle;
}