gpt4 book ai didi

javascript - 在按钮上单击检查文件是否存在然后提示消息并还原

转载 作者:行者123 更新时间:2023-11-30 12:12:28 24 4
gpt4 key购买 nike

我有一个场景,我想检查用户是否再次添加具有相同日期相同区域的数据,它应该给他一个提示。

我在后面的代码中尝试了这个,如下所示:-

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

另请参阅我的提示重复消息警报的功能

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

但这里发生的情况是,即使在取消点击后,excel 文件也会上传并保存。不知道为什么

最佳答案

尽管 Abbas Kapasi 的 基于 AJAX 的解决方案是一个很好的解决方案(并且您不想/不能使用我在另一篇文章 codebhind-javascript-alert-for-yes-and-no-not-working-exactly 中建议的基于 CheckBox 的解决方案),如果您不熟悉 AJAX 或不想使用 AJAX,那么您可以使用以下方法实现它。这可能看起来有点奇怪,但您可以根据您的要求进行修改:

  1. 我正在拆分代码以在一个过程中上传和查找现有文件/记录,以及在另一个子过程中编写/覆盖记录的代码。
  2. 然后我使用两个 LinkBut​​tons,LinkBut​​tonLoadLinkBut​​tonOverwrite
    LinkBut​​tonLoad 将附加到调用上传和查找现有文件/记录的主程序。 LinkBut​​tonOverwrite 将调用程序来写入/覆盖记录。 LinkBut​​tonOverwrite 将对用户保持隐藏
  3. 在第一个过程中,如果系统找到现有文件/记录,它将向用户显示一个客户端提示,提示用户是否覆盖。如果文件/记录不存在,它将调用第二个子过程来写入记录。
  4. 在客户端,如果显示现有文件/记录的提示并且用户选择Cancel/No,则该过程将不会继续。如果用户选择 OK/Yes,那么我将调用链接到 LinkBut​​ton2 的过程来覆盖记录。

现在把它们放在一起:

在aspx/前端

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

我正在使用 hidden CSS 类来隐藏按钮 .hidden {display: none;}。不要使用 visible="false" 属性,它在客户端不可用。

客户端的 JavaScript:

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 在后面的代码中:

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 代码隐藏:

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() 后面的代码:

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

关于javascript - 在按钮上单击检查文件是否存在然后提示消息并还原,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33391254/

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