using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data;
using System.Data.SqlClient ;
using System.Web;

public partial class _Default : System.Web.UI.Page 
{
static   DataTable dtvalues =new DataTable();

    protected void Page_Load(object sender, EventArgs e)
        {
           // dtvalues.Columns.Clear();
           
            if (!Page.IsPostBack)
            {
              createdatatable();
            }
            else
            {

            }
    }
    protected void createdatatable()
    {
           dtvalues.Columns.Clear();
            dtvalues.Columns.Add("Name");
            dtvalues.Columns.Add("Middel Name");
            dtvalues.Columns.Add("Last Name");
            dtvalues.Columns.Add("Address");
    
        
    }    
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (TextBox1.Text == "" && TextBox2.Text == "" && TextBox3.Text == "" && TextBox4.Text == "")
        {

        }
        else
        {

            DataRow drvalues = dtvalues.NewRow();
            drvalues["Name"] = TextBox1.Text;
            drvalues["Middel Name"] = TextBox2.Text;
            drvalues["Last Name"] = TextBox3.Text;
            drvalues["Address"] = TextBox4.Text;
            dtvalues.Rows.Add(drvalues);
            GridView1.DataSource = dtvalues;
            GridView1.DataBind();
            TextBox1.Text = "";
            TextBox2.Text = "";
            TextBox3.Text = "";
            TextBox4.Text = "";
            TextBox1.Focus();
        } 
       

    }
}


this is my code after inserting values in to text box when we clicking on button 1 values from textboxes enters into grideview like this we can enters n numbers of values but the problem is while in beetween we are clicking on the refresh button of browser the last inserted record insrted again which is not wanted to do

I don't know the exact solution but I think it's ok. It should be like that. Because you click button and give some job and F5 repeat last job.

exactly is there any way to stop becouse if end user will refresh the page application will reenter the last record which couse problem

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data;
using System.Data.SqlClient ;
using System.Web;

public partial class _Default : System.Web.UI.Page 
{
static   DataTable dtvalues =new DataTable();

    protected void Page_Load(object sender, EventArgs e)
        {
           // dtvalues.Columns.Clear();
           
            if (!Page.IsPostBack)
            {
              createdatatable();
            }
            else
            {

            }
    }
    protected void createdatatable()
    {
           dtvalues.Columns.Clear();
            dtvalues.Columns.Add("Name");
            dtvalues.Columns.Add("Middel Name");
            dtvalues.Columns.Add("Last Name");
            dtvalues.Columns.Add("Address");
    
        
    }    
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (TextBox1.Text == "" && TextBox2.Text == "" && TextBox3.Text == "" && TextBox4.Text == "")
        {

        }
        else
        {

            DataRow drvalues = dtvalues.NewRow();
            drvalues["Name"] = TextBox1.Text;
            drvalues["Middel Name"] = TextBox2.Text;
            drvalues["Last Name"] = TextBox3.Text;
            drvalues["Address"] = TextBox4.Text;
            dtvalues.Rows.Add(drvalues);
            GridView1.DataSource = dtvalues;
            GridView1.DataBind();
            TextBox1.Text = "";
            TextBox2.Text = "";
            TextBox3.Text = "";
            TextBox4.Text = "";
            TextBox1.Focus();
        } 
       

    }
}


this is my code after inserting values in to text box when we clicking on button 1 values from textboxes enters into grideview like this we can enters n numbers of values but the problem is while in beetween we are clicking on the refresh button of browser the last inserted record insrted again which is not wanted to do

Hi HirenPatel

This may help you,
Just remove "createdatatable" from Page_load event and move it button click


Mark as solved if it helps you!!!

While I'm not a fan of linking info from one site to another (kinda like standing in a restaurant shouting "go eat at the one across the street") I believe that this post at the asp.net forums might be of assistance to you.

Check the replies by scottim to the question asked.

Post Link -> For convenience sake I'll post the pertinent portions below but I hate to take someone's info without due credit is all:

One simple way is to add a literal to your page. Set the literal text to be <input type="hidden" name="ticks" value="..."/>, where the "..." is replaced by something unique to each request (e.g. System.DateTime.Now.Ticks.ToString()). In the button's OnClick handler, check if Session["ticks"] is the current posted value of the "ticks" hidden field (Request["ticks"]). If so, return without doing anything. Otherwise, execute the rest of the handler logic. At the end of the handler, set Session["ticks"] to the current posted "ticks" value.

1. Save the code below to page.aspx.
2. Browse to page.aspx (see label text "GET").
3. Click the button to post (see label text "Real POST").
4. Refresh the page (see label text "Refresh POST").
etc...

<script runat=server language=cs>
public void Page_Load() {
string ticks = Request["ticks"];
string sessionTicks = (string)Session["ticks"];
if (IsPostBack) {
if (ticks == sessionTicks) {
lb.Text = "Refresh POST";
}
else {
lb.Text = "Real POST";
Session["ticks"] = ticks;
}
}
lit.Text = "<input type=\"hidden\" name=\"ticks\" value=\"" + DateTime.Now.Ticks + "\"/>";
}

</script>


<form runat=server>
<asp:literal runat=server id=lit/>
<asp:label runat=server id=lb text=GET/>
<asp:button runat=server text=click/>
</form>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.