C# Javascript未在C代码中触发

C# Javascript未在C代码中触发,c#,javascript,C#,Javascript,我有以下代码在程序中上传附件。在此过程中,如果客户端在上载文件中选择了先前在程序中上载的同名文档,则必须向客户端发送警报消息以更改文档名称 代码段: private string UploadFile() { string pathToSaveFile = Server.MapPath("~/Data/"); string clientFileName = System.IO.Path.GetFileName(FileUpload1.PostedFile.

我有以下代码在程序中上传附件。在此过程中,如果客户端在上载文件中选择了先前在程序中上载的同名文档,则必须向客户端发送警报消息以更改文档名称

代码段:

private string UploadFile()
    {
        string pathToSaveFile = Server.MapPath("~/Data/");
        string clientFileName = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);

        string upload_data = pathToSaveFile + clientFileName;

        if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.ContentLength > 0)
        {

            if (System.IO.File.Exists(upload_data))
            {
                //using Response.write
                Response.Write(@"<script type='text/javascript'>alert('Rename it please.');</script>");

                //ClientScriptManager
                var clientScript = Page.ClientScript;
                clientScript.RegisterClientScriptBlock(this.GetType(), "AlertScript", "alert('Rename it please.')'", true);

                //ScriptManger
                ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('Member Registered Sucessfully');", true);

            }
            else
            {
                FileUpload1.PostedFile.SaveAs(System.IO.Path.Combine(pathToSaveFile, clientFileName));
            }
        }
        else
        {
            Response.Write("Not Available");
        }

        return clientFileName;
    }
我在程序中使用了各种类型的javascript代码,但它们都不起作用。代码读取器只是读取代码并通过它

当我提交表单按钮时,所有表单字段都会被读取并保存在一个对象中,当涉及到上传部分时,会读取上面的代码,clientFileName会传递给filename对象并传递给sqlquery以将其输入数据库

它不会显示任何提示弹出窗口,以便客户端更改文件名。因此,samefile名称被传递到服务器,并且由于相同的名称而引发冲突


谢谢。

只有在您发布网站时,警报才会弹出。 它不能在C代码中工作

请参见下面的代码仅供参考:

private bool UploadFile()
{
    string pathToSaveFile = Server.MapPath("~/Data/");
    string clientFileName = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);

    string upload_data = pathToSaveFile + clientFileName;

    if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.ContentLength > 0)
    {

        if (System.IO.File.Exists(upload_data))
        {
            //using Response.write
            Response.Write(@"<script type='text/javascript'>alert('Rename it please.');</script>");

            //ClientScriptManager
            var clientScript = Page.ClientScript;
            clientScript.RegisterClientScriptBlock(this.GetType(), "AlertScript", "alert('Rename it please.')'", true);

            //ScriptManger
            ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('Member Registered Sucessfully');", true);
    return false;
        }
        else
        {
            FileUpload1.PostedFile.SaveAs(System.IO.Path.Combine(pathToSaveFile, clientFileName));
        }
    }
    else
    {
        Response.Write("Not Available");
    }

ViewState["FileName"] = clientFileName;
    return true;
}

您从何处调用此私有方法?您的页面是否在POST/Submit上进行完全刷新?当我们单击表单提交按钮时,请单击。它读取所有表单文本框,最后到达上传选项,然后我将上述过程保存在名为UploadFile的函数中;UploadFile返回上载文件名,该名称保存在对象中,并传递到sql查询以获取数据库条目。问题是警报没有弹出。
if(UploadFile())
        //run your SQL queue
    else
        return;//do all the return that will repost the website.