我正在使用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;
}
}
答案 0 :(得分:0)
由于Postback,ASP.net页面重新加载正在发生。为了避免这种情况,您需要使用AJAX。 AJAX请求将避免完整页面重新加载。您可以使用JQuery或APS.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;
}