我写了一个页面来将文件上传到服务器。
在上传文件之前,应该有一个覆盖文件的问题(如果文件已经在服务器上)
check(后面的代码/ Event IndexChange)在客户端脚本中设置了一个变量。 如果用户按下“上传”按钮,则脚本(confirm_user)将检查变量并显示一个确认对话框(覆盖?是/否),然后调用C#函数。
它是在具有母版页的IFrame中运行的ASPX控件。
我的问题:
如果我在未设置输入文件的情况下对其进行了测试,那么它将起作用(代码中的断点)。 但是,如果我选择要上传的文件,则不会调用该功能(按正常的“上传”按钮可以正常工作)。
ASCX文件
<%@ Control Language="C#" AutoEventWireup="True" Inherits="Test_Web.UploadProgress" Codebehind="UploadProgress.ascx.cs"%>
<link rel="stylesheet" href="css/alertify.css" />
<link rel="stylesheet" href="css/themes/default.css" />
<script src="alertify.js"></script>
<script type='text/javascript'>
//hold Check state
var override = -1;
//Script is working, except Click()
function confirm_user() {
if (override == 1) {
if (confirm('Overrride?')) { document.getElementById('<%=btn_upload_nocheck.ClientID%>').click();
return true;
}
else
return false;
}
else if (override == 0) { document.getElementById('<%=btn_upload_nocheck.ClientID%>').click();
return true;
}
return false;
function setoverride(ov) {
override = ov;
alertify.alert('Info','Test: '+ ov);
}
//}
</script>
<asp:ScriptManager runat="server"></asp:ScriptManager>
<asp:UpdatePanel runat="server" ID="up_message">
<ContentTemplate>
<asp:DropDownList runat="server" ID="dd_sendungstitle" OnSelectedIndexChanged="dd_sendungstitle_SelectedIndexChanged" AutoPostBack="True">
<asp:ListItem>t1</asp:ListItem>
<asp:ListItem>t2</asp:ListItem>
<asp:ListItem>t3</asp:ListItem>
<asp:ListItem>t4</asp:ListItem>
</asp:DropDownList>
<asp:Label runat="server" ID="lbl_progress"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
<br/>
<input type="file" id="upUserCtrl" runat="server"/>
<br/>
<asp:Button ID="btn_upload_nocheck" runat="server" Text="Upload" Width="101px" align="center" OnClick="btn_upload_nocheck_Click"/>
<asp:Button runat="server" ID="btnTest" OnClientClick="return confirm_user();" Text="Upload Script"/>
文件后方代码
public partial class UploadProgress: System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
...
private void ShowMessage(string msg)
{
StringBuilder sb = new StringBuilder();
sb.Append("<body><script type='text/javascript'>alert('" + msg + "');</script></body>");
HttpContext.Current.Response.Write(sb.ToString());
HttpContext.Current.Response.Flush();
}
protected void btn_upload_nocheck_Click(object sender, EventArgs e)
{
Upload(false);
}
private void Upload(bool docheck)
{
//Upload File
int t = _override;
...
ShowMessage("File(" + filename + ") uploaded");
}
protected void dd_sendungstitle_SelectedIndexChanged(object sender, EventArgs e)
{
// Do Checking STuff
if (dd_sendungstitle.SelectedIndex == 0 || dd_sendungstitle.SelectedIndex == 3)
{
_override = 0;
}
else
{
_override = 1;
}
//Set Client Script
ScriptManager.RegisterStartupScript(Page,typeof(string), "javascript", "setoverride(" + _override + ")", true);
}
}