每次页面刷新时插入查询执行 [英] insert query executing when every time page got refreshing
本文介绍了每次页面刷新时插入查询执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用系统;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web;
使用 System.Web.UI;
使用 System.Web.UI.WebControls;
使用 System.Data.SqlClient;
使用 System.IO;
public partial class ImageUpLoader:System.Web.UI.Page
{
string sqlstr;
static string 路径;
string strFileName1;
受保护 void Page_Load( object sender,EventArgs e)
{
if (!IsPostBack)
{
}
}
public void uploadDocument()
{
if ((FileUpload1.PostedFile!= null ))
{
HttpPostedFile file1 = FileUpload1.PostedFile;
path = / * Documents /+ * / file1.FileName;
strFileName1 = Server.MapPath( 〜/ Documents /)+ file1.FileName;
file1.SaveAs(strFileName1);
string ext = Path.GetExtension(path);
string contenttype = String .Empty;
switch (ext)
{
case 。jpg:
contenttype = image / jpg;
break ;
case 。png:
contenttype = image / png;
break ;
case 。gif:
contenttype = image / gif;
break ;
case 。pdf:
contenttype = image / pdf;
break ;
// case.doc:
// contenttype =Documents / vnd.ms-word;
// break;
// case.docx:
// contenttype =Documents / vnd.ms -word;
// break;
// case.xls:
// contenttype =Documents / vnd.ms-excel;
// 休息;
// case.xlsx:
// contenttype =Documents / vnd.ms-excel;
< span class =code-comment> // break;
}
}
}
受保护 void btnUpload_Click( object sender,EventArgs e)
{
uploadDocument();
string date = DateTime.Now.ToString( DD-MM-YYYY跨度>);
string imageid = DAL.getRunningID( 4\" 跨度>);
sqlstr = 插入图像(ImageID,URL,DateTime)值( + imageid + ,' + path + ',' + date + ') 跨度>;
string sqlUpdate = update Fms_Autonumber set RNo =' + imageid + '其中RCode ='4';
List< string> ids = new List< string>();
ids.Add(sqlstr);
ids.Add(sqlUpdate);
int j = DAL.sqlTransaction(ids);
if (j > 0 )
{
ScriptManager.RegisterStartupScript( this , this .GetType( ), 已成功插入, alert('Image Uploaded Successfully ..!'), true );
}
mltImageurl.Text = strFileName1;
}
}
解决方案
引用:插入数据后,如果我刷新页面而不按下上传按钮,那么数据也会插入..
试试这个:< br $> b $ b
http://www.aspdotnet-suresh.com/2010/04/detect-browser-refresh-to-avoid-events.html [ ^ ]
< pre lang =cs> protected void Page_Load( object sender,EventArgs e)
{
if (!IsPostBack)
{
}
}
将一个插入查询放入page_load或m在page_load事件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.IO;
public partial class ImageUpLoader : System.Web.UI.Page
{
string sqlstr;
static string path;
string strFileName1;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
}
}
public void uploadDocument()
{
if ((FileUpload1.PostedFile != null))
{
HttpPostedFile file1 = FileUpload1.PostedFile;
path = /* "Documents/" + */ file1.FileName;
strFileName1 = Server.MapPath("~/Documents/") + file1.FileName;
file1.SaveAs(strFileName1);
string ext = Path.GetExtension(path);
string contenttype = String.Empty;
switch (ext)
{
case ".jpg":
contenttype = "image/jpg";
break;
case ".png":
contenttype = "image/png";
break;
case ".gif":
contenttype = "image/gif";
break;
case ".pdf":
contenttype = "image/pdf";
break;
//case ".doc":
// contenttype = "Documents/vnd.ms-word";
// break;
//case ".docx":
// contenttype = "Documents/vnd.ms-word";
// break;
//case ".xls":
// contenttype = "Documents/vnd.ms-excel";
// break;
//case ".xlsx":
// contenttype = "Documents/vnd.ms-excel";
// break;
}
}
}
protected void btnUpload_Click(object sender, EventArgs e)
{
uploadDocument();
string date = DateTime.Now.ToString("dd-MM-yyyy");
string imageid = DAL.getRunningID("4");
sqlstr = "insert into Images(ImageID,URL,DateTime)values(" + imageid + ",'" + path + "','" + date + "')";
string sqlUpdate = "update Fms_Autonumber set RNo='" + imageid + "' where RCode='4'";
List<string> ids = new List<string>();
ids.Add(sqlstr);
ids.Add(sqlUpdate);
int j = DAL.sqlTransaction(ids);
if (j > 0)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "Successfully Inserted", "alert('Image Uploaded Successfully..!')", true);
}
mltImageurl.Text = strFileName1;
}
}
解决方案
Quote:After data inserted, if i refresh the page without pressing upload button then also data is inserting..
try this:
http://www.aspdotnet-suresh.com/2010/04/detect-browser-refresh-to-avoid-events.html[^]
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { } }
put a insert query into a page_load or make a mathod for query insertion of data nad call in page_load event
这篇关于每次页面刷新时插入查询执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文