在buttonclick检查是否存在文件,然后提示信息,并回复 [英] on buttonclick check if file exist then prompt message and revert

查看:116
本文介绍了在buttonclick检查是否存在文件,然后提示信息,并回复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我想要的检查,如果用户用同日同一区域再次增加了数据的情况下,它应该给他一个提示警告。

I have a scenario where I want to check if user adds data with same date and same region again, it should give him a prompt alert.

我code试图在这背后,象下面这样: -

I tried this with code behind,like below:-

 protected void btnSave_Click(object sender, EventArgs e)
{

    DataTable dtExcel = new DataTable();
    dtExcel.Clear();
    string StrCount = String.Empty;
    string connString = "";
    HttpPostedFile File = FileUpload1.PostedFile;
    string strFileType = Path.GetExtension(FileUpload1.FileName).ToLower();
    string path = FileUpload1.PostedFile.FileName;
    string Filename = path.Substring(path.LastIndexOf("\\") + 1, path.Length - path.LastIndexOf("\\") - 1);
    path = Server.MapPath(@"~/Excels/" + "/" + Filename.ToString());

    File.SaveAs(path);
    if (strFileType.Trim() == ".xls")
    {
        connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
    }
    else if (strFileType.Trim() == ".xlsx")
    {
        connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
    }
    string query = "SELECT * FROM [Sheet 1$]";
    OleDbConnection conn = new OleDbConnection(connString);
    conn.Close();
    if (conn.State == ConnectionState.Closed)
        conn.Open();
    OleDbCommand cmd = new OleDbCommand(query, conn);
    OleDbDataAdapter daExcel = new OleDbDataAdapter(cmd);

    daExcel.Fill(dtExcel);
    conn.Close();


    DataTable DtMain = CF.ExecuteDT("select Tran_type, Order_Date, Region_Mkey from WMS_Future_Del_Order_Hdr where Tran_type = '" + CmbTypeOfAsn.SelectedValue + "' and Order_Date = convert(datetime,'" + TxtEdate.Value + "',103) and Region_Mkey = '" + ddlRegion.SelectedValue + "'"); // checks the duplicate records here


    if (DtMain.Rows.Count > 0)
    {
        ClientScript.RegisterStartupScript(this.GetType(), "SuccessScript", "myTestFunction()", true);  
    }

另请参阅我的函数用于提示警报重复的消息

Also see my function for prompting the alert for duplicate message

function myTestFunction() {
         if (confirm('Are you sure you want to override the file ?')) {
             return true;
         }
         else {
             return false;
         }
     }

但这里所发生甚至取消点击后,Excel文件上传和会保存。我不知道为什么。

but what happens here is even after cancel click, the excel file is uploaded and gets saved. I dont know why

推荐答案

虽然的阿巴斯Kapasi的的基于AJAX的解决方案是很好的一个(你不想/不能使用基于复选框的解决方案正如我在其他职位建议<一href=\"http://stackoverflow.com/a/33477274/5104101\">$c$cbhind-javascript-alert-for-yes-and-no-not-working-exactly),如果你不熟悉AJAX或不想使用AJAX,那么你可以根据你的要求使用以下方法实现它这似乎有点奇怪,但你可以修改它。

Although Abbas Kapasi's solution based on AJAX is good one (and you do not want to/can not use the solution based on CheckBox as I suggested in my other post codebhind-javascript-alert-for-yes-and-no-not-working-exactly), if you are not familiar with AJAX or don't want to use AJAX then you may achieve it using following approach. It may seem a bit odd but you may modify it according to your requirements:


  1. 我分裂code上传并找到一个过程现有的文件/记录和code写/覆盖另一个子过程的记录。

  2. 然后我用两个LinkBut​​tons, LinkBut​​tonLoad LinkBut​​tonOverwrite 。结果
    LinkBut​​tonLoad 附会进行通话的上传和发现现有的文件/记录的主要步骤。 LinkBut​​tonOverwrite 将调用写入/改写纪录的过程。的 LinkBut​​tonOverwrite仍将隐藏从用户

  3. 在第一个过程中,如果系统发现现有的文件/记录它将显示一个客户端提示给用户来覆盖或没有。如果文件/记录不存在,它会调用第二个子程序写的记录。

  4. 在客户端,如果显示为已存在的文件/记录与用户选择的提示取消/否的过程将不会进行。如果用户选择 OK /是然后我会打电话联系到LinkBut​​ton2程序改写的记录。

  1. I am splitting the code to upload and find existing file/record in one procedure and the code to write/overwrite the records in another sub-procedure.
  2. Then I am using two LinkButtons, LinkButtonLoad and LinkButtonOverwrite.
    LinkButtonLoad will be attached to the call the main procedure which uploads and finds existing file/record. LinkButtonOverwrite will call the procedure to write/overwrite the records. LinkButtonOverwrite will remain hidden from the user.
  3. In the first procedure if the system finds existing file/record it will show a client side prompt to the user to overwrite or not. If the file/record does not exist it will call second sub-procedure to write the records.
  4. On client side, if the prompt is displayed for existing file/record and the user selects Cancel/No the process will not proceed. If the user selects OK/Yes then I'll be calling the procedure linked to the LinkButton2 to overwrite the records.

现在把他们放在一起:

在ASPX /前端

<asp:LinkButton ID="LinkButtonLoad" runat="server" Text="Load Records" CssClass="button" ... />
<asp:LinkButton ID="LinkButtonOverwrite" runat="server" Text="ow" CssClass="button hidden" ... />

我用隐藏 CSS类隐藏按钮 .hidden {显​​示:无;} 。不要使用可见=假属性,它会无法使用在客户端。

I'm using hidden CSS class to hide the button .hidden {display: none;}. Do NOT use visible="false" attribute, it'll not be available on the client side.

在客户端的JavaScript:

JavaScript on client side:

function myTestFunction() {
    if (confirm('Are you sure you want to override the file ?')) {
        // get the action attribute of the hidden LinkButtonOverwrite here and call it on OK
        // as an alternative you may also invoke the click event of this button
        var defaultAction = $('#<%=LinkButtonOverwrite.ClientID %>').prop("href");
        window.location.href = defaultAction;
    }
    else {
        return false;
    }
}

在LinkBut​​tonLoad code背后:

LinkButtonLoad in code behind:

protected void LinkButtonLoad_Click(object sender, EventArgs e)
{
    //==============================================================
    // YOUR CODE TO UPLOAD THE FILE, SAVE IT SO WE MAY USE IT AGAIN
    //==============================================================
    if (DtMain.Rows.Count > 0)
    {
        ClientScript.RegisterStartupScript(this.GetType(), "SuccessScript", "myTestFunction()", true);
    }
    else
    {
        MySubToOverwrite();
    }
}

在LinkBut​​tonOverwrite code背后:

LinkButtonOverwrite in code behind:

protected void LinkButtonOverwrite_Click(object sender, EventArgs e)
{
    //===========================================================
    // AS THE FILE IS ALREADY UPLOADED IN LINKBUTTONUPLOAD_CLICK
    // YOU MAY ACCESS IT AND PERFORM REQUIRED OPERATIONS
    //===========================================================

    MySubToOverwrite();
}

MySubToOverwrite()code背后:

MySubToOverwrite() code in behind:

private void MySubToOverwrite()
{
    //==========================================================
    // OVERWRITE YOUR RECORDS HERE
    // FROM THE FILE ALREADY UPLOADED IN LINKBUTTONUPLOAD_CLICK
    //==========================================================
}

这篇关于在buttonclick检查是否存在文件,然后提示信息,并回复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆