单击刷新按钮时,数据会再次保存 [英] data gets saved again when clicking the refresh button

查看:72
本文介绍了单击刷新按钮时,数据会再次保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个asp.net Web表单,它有一个文本框控件和一个按钮控件.buton控件有一个事件处理程序。当按钮被单击时,文本框控件中的文本被检索,然后被保存到sql server数据库。我在这里面临的问题是当我点击按钮控件时数据被保存到正常的数据库但是当我点击f5键在网络浏览器上刷新数据时会再次保存。

这是什么解决方案。

i have a asp.net web form that has a textbox control and a button control.the buuton control has got a event handler.when the button gets clicked text from the textbox control is retrieved and then gets saved to the sql server database.the problem that i am facing here is when i click the button control the data gets saved to the database which is normal but when i hit the f5 key to refresh on the web browser the data gets saved again.
what''s the solution to this.

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;

namespace WebApplication3
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        string data;
                
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            data = TextBox1.Text;
            using (SqlConnection con = new SqlConnection("initial catalog=users;data source=.; integrated security=sspi"))
            {
                if (TextBox1.Text != "")
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand("insert into dbo.names values(@data)", con);
                    SqlParameter dataparam = cmd.Parameters.Add("@data", System.Data.SqlDbType.VarChar);
                    dataparam.Value = data;
                    cmd.ExecuteNonQuery();
                    TextBox1.Text = "";           
                }           
            }
        }
    }
}

推荐答案

你必须保持相同的功能,即

You must have kept same function i.e
protected void Button1_Click(object sender, EventArgs e)



保存和刷新按钮,转到酒店刷新按钮的功能将羊肉点击事件的功能更改为其他名称再次定义它。


for both save and refresh buttons, go to the property of the refresh button change the function for mutton click event to some other name again define it.


你可以简单地添加:



You can simpley add:

if(!Page.IsPostBack)
{
}



围绕您想要在按钮运行时运行的代码点击。


around the code you want to run when the button is clicked.


使用System;

使用System.Collections.Generic;

使用System.Linq;

使用System.Web;

使用System.Web.UI;

使用System.Web.UI.WebControls;

使用System。 Data.SqlClient;

命名空间WebApplication4

{

公共部分类WebForm1:System.Web.UI.Page

{

protected void Page_Load(object sender,EventArgs e)

{



if(!IsPostBack)

{



会话[时间] = DateTime.Now.ToString();

}

}

protected void Page_PreRender(object sender,EventArgs e)

{

ViewState [Time] = Session [Time];

}





protected void Button1_Click(object sender,EventArgs e)

{

if(Session [Time]。ToString()== ViewState [Time]。ToString())

{

//提交数据的代码....





TextBox1.Text = null;



Session [Time] = DateTime.Now.ToString();



}

其他

{

//页面刷新代码....

TextBox1。 Text = null;

Response.Write(Page Refreshed!);

}

}

}

}
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;
namespace WebApplication4
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

if (!IsPostBack)
{

Session["Time"] = DateTime.Now.ToString();
}
}
protected void Page_PreRender(object sender, EventArgs e)
{
ViewState["Time"] = Session["Time"];
}


protected void Button1_Click(object sender, EventArgs e)
{
if (Session["Time"].ToString() == ViewState["Time"].ToString())
{
// Code for submitting data....


TextBox1.Text = null;

Session["Time"] = DateTime.Now.ToString();

}
else
{
// Code for page refresh....
TextBox1.Text = null;
Response.Write("Page Refreshed!");
}
}
}
}


这篇关于单击刷新按钮时,数据会再次保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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