gpt4 book ai didi

c# - 在 ASP.NET 中使用类似 FileUpload 的按钮

转载 作者:太空宇宙 更新时间:2023-11-03 21:14:19 25 4
gpt4 key购买 nike

我有这种情况:

enter image description here

两个普通的 ASP.NET 控件。但我想隐藏 FileUpload 控件,并保留唯一的 Button。

一些如此微不足道的事情给我带来了比预期更多的问题。这是我的实现:

ASPX:

<asp:FileUpload ID="FileUpload1" runat="server" />

<asp:Button ID="UploadButton" runat="server" Text="Carica Documento" OnClientClick="showBrowseDialog();" />

<asp:Button runat="server" ID="hideButton" Text="" style="display:none;" OnClick="UploadButton_Click" />

<script type="text/javascript" language="javascript">
function showBrowseDialog() {
var fileuploadctrl = document.getElementById('<%= FileUpload1.ClientID %>');
fileuploadctrl.click();

var btn = document.getElementById('<%= hideButton.ClientID %>');
btn.click();
}
</script>

C#:

protected void UploadButton_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
try
{
string address = Server.MapPath("") + "\\" + FileUpload1.FileName;
FileUpload1.SaveAs(address);
//...
}
catch (Exception) { }
}

最佳答案

试试这个布局

<asp:FileUpload ID="FileUpload1" Style="display: none" runat="server" onchange="upload()" />
<input type="button" value="Carica Documento" onclick="showBrowseDialog()"/>

<asp:Button runat="server" ID="hideButton" Text="" Style="display: none;" OnClick="UploadButton_Click" />

<script type="text/javascript" language="javascript">
function showBrowseDialog() {
var fileuploadctrl = document.getElementById('<%= FileUpload1.ClientID %>');
fileuploadctrl.click();
}

function upload() {
var btn = document.getElementById('<%= hideButton.ClientID %>');
btn.click();
}
</script>

关于c# - 在 ASP.NET 中使用类似 FileUpload 的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35484197/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com