ASP.NET-FileUpload上傳後自動觸發button click(FileUpload upload auto trigger button click)
在ASP.NET中FileUpload很常都需要配一個button,上傳檔案一個動作,上傳後還需點擊按鈕才能觸發後續的儲存等等功能。
這邊要實作上傳檔案後自動觸發後續功能,不用再多一步點擊按鈕。
前端:
當fileUpload的值不為空自動找到button做click動作
<script type="text/javascript">
function UploadFile(fileUpload) {
if (fileUpload.value != '') {
document.getElementById("<%=btn_upload.ClientID %>").click();
}
}
</script>
新增FileUpload跟button並把button隱藏
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btn_upload" runat="server" OnClick="btn_upload_Click" Style="display: none" />
後端:
using System.IO;
//新增屬性當onchange時觸發javascript function
protected void Page_Load(object sender, EventArgs e)
{
FileUpload1.Attributes["onchange"] = "UploadFile(this)";
}
//完成自動觸發button click
protected void btn_upload_Click(object sender, EventArgs e)
{
//儲存等等動作
}
這邊要實作上傳檔案後自動觸發後續功能,不用再多一步點擊按鈕。
前端:
當fileUpload的值不為空自動找到button做click動作
<script type="text/javascript">
function UploadFile(fileUpload) {
if (fileUpload.value != '') {
document.getElementById("<%=btn_upload.ClientID %>").click();
}
}
</script>
新增FileUpload跟button並把button隱藏
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btn_upload" runat="server" OnClick="btn_upload_Click" Style="display: none" />
後端:
using System.IO;
//新增屬性當onchange時觸發javascript function
protected void Page_Load(object sender, EventArgs e)
{
FileUpload1.Attributes["onchange"] = "UploadFile(this)";
}
//完成自動觸發button click
protected void btn_upload_Click(object sender, EventArgs e)
{
//儲存等等動作
}
留言
張貼留言