简体   繁体   English

Javascript未在C#代码中触发

[英]Javascript Not Firing inside C# Code

I have the following code to upload the attachment in a program. 我有以下代码将附件上传到程序中。 In the process, if the client choose same named document in the upload file which is previously upload in the program then the client must be given alert message to change the name of the document. 在此过程中,如果客户端在先前在程序中上载的上载文件中选择了相同的命名文档,则必须向客户端提供警报消息以更改文档的名称。

Code Snippet: 代码段:

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;
    }

I have used the types of javascript codes in the program but they all doesn't work. 我在程序中使用了JavaScript代码类型,但它们均无法正常工作。 The code reader just reads the code and passes through it. 代码阅读器仅读取并传递代码。

As I submit the form b utton, all the forms fields are read and kept in an object and when it comes to an upload part the above code is read and the clientFileName is passed to the filename object and passed to the sqlquery to enter it into the database. 当我提交表单按钮时,所有表单字段均被读取并保存在一个对象中,当涉及上载部分时,将读取上述代码,并将clientFileName传递给filename对象并传递给sqlquery以便将其输入数据库。

It doesn't show any alert popup for the client to change the name of the file. 对于客户端更改文件名,它不会显示任何警报弹出窗口。 So the samefile name is passed to the server and conflict starts because of the same name. 因此,将相同的文件名传递给服务器,并且由于名称相同而导致冲突开始。

Thanks. 谢谢。

The alert pop up only when you re-posting the website. 仅当您重新发布网站时才会弹出警报。 It cannot work inside the C# code. 它不能在C#代码中运行。

See the code below(reference only): 请参见下面的代码(仅供参考):

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;
}

Outside the function use something like this: 在函数之外,使用类似以下的内容:

if(UploadFile())
        //run your SQL queue
    else
        return;//do all the return that will repost the website.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM