通过asp.net中的“保存"按钮将数据绑定到gridview之后,一旦刷新页面,就会触发“保存"按钮单击事件 [英] save button click event getting fired up on refreshing the page once done with binding the data to a gridview through save button in asp.net

查看:48
本文介绍了通过asp.net中的“保存"按钮将数据绑定到gridview之后,一旦刷新页面,就会触发“保存"按钮单击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ASP.NET和C#.NET开发Web应用程序.通过同一页面中的文本框将数据绑定到Gridview,然后在保存"按钮单击事件中编写了绑定方法.现在,这真的很奇怪,因为一旦通过保存" button_click事件将数据从文本框中保存到gridveiw上刷新页面,网格视图就会再次被重复的行绑定.我曾尝试在firefox,chrome和IE 8上加载页面.但是结果是负面的....

谁能让我知道它为什么会发生,并指导我解决它....

以下是我的C#代码:

I am working on a web application using asp.net and c#.net. am binding the data to the gridview through the textboxes in the same page and I wrote the binding method in my "Save" button click event. Now, Its really strange thing to find that the grid view is getting binded again with duplicate rows once I refresh the page up on saving the data to the gridveiw from textboxes through "save" button_click event. I have tried loading the page on firefox, chrome and IE 8....but the result is negative....

could anyone let me know why is it happening and guide me to resolve it....

the following is my c# code:

string con = ConfigurationSettings.AppSettings["ConnectionStrings"];
protected void Page_Load(object sender, EventArgs e)
    {
        
        tbladdasset.Visible = false;
        btnsaveasset.Enabled = false;
        lblErrMsg.Text = "";
        
        if (!IsPostBack)
        {
            bindassets("", "");
            ViewState["sortOrder"] = "";
           
        }

    }


    private void bindassets(string sortExp, string sortDir)
    {
        try
        {
            SqlConnection con1 = new SqlConnection(con);
            con1.Open();
            SqlCommand cmd = new SqlCommand("select Description,CONVERT(VARCHAR(10), RecievedDate, 101) as DateRecieved,cost,Modelno,Quantity from Asset", con1);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            con1.Close();
            if (dt.Rows.Count > 0)
            {
                DataView dv = dt.DefaultView;
                if (sortExp != string.Empty)
                {
                    dv.Sort = string.Format("{0} {1}", sortExp, sortDir);
                }
                grdvAssets.DataSource = dv;
                grdvAssets.DataBind();
                
            }
            else
            {
                lblErrMsg.Text = "No data found...";
            }
            btnsaveasset.Enabled = false;
            tbladdasset.Visible = false;
        }
        catch
        {
            lblErrMsg.Text = "Failed to connect server...";
        }

    }

 protected void btnaddnew_Click(object sender, EventArgs e)
    {
        tbladdasset.Visible = true;
        btnsaveasset.Enabled = true;
        lblErrMsg.Text = "";
        txtdescription.Text = "";
        txtdtrecieved.Text = "";
        txtcost.Text = "";
        txtmodelno.Text = "";
        txtquantity.Text = "";
      
    }

    protected void btnsaveasset_Click(object sender, EventArgs e)
    {
            if (txtdescription.Text != "" && txtdtrecieved.Text != "" && txtcost.Text != "" && txtmodelno.Text != "" && txtquantity.Text != "")
            {
                try
                {
                    string desc= txtdescription.Text;
                    DateTime dtrecd = Convert.ToDateTime(txtdtrecieved.Text);
                    string cost = txtcost.Text;
                    string modelno = txtmodelno.Text;
                    double quantity = Convert.ToDouble(txtquantity.Text);
                    SqlConnection sqlcon = new SqlConnection(con);
                    sqlcon.Open();
                    string save = "Insert into Asset(Description,Recieveddate,cost,Modelno,Quantity)values(@desc,@date,@cost,@modelno,@quantity)";
                    SqlCommand cmd = new SqlCommand(save, sqlcon);
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.Add("@desc", desc);
                    cmd.Parameters.Add("@date", dtrecd);
                    cmd.Parameters.Add("@cost", cost);
                    cmd.Parameters.Add("@modelno", modelno);
                    cmd.Parameters.Add("@quantity", quantity);
                    cmd.ExecuteNonQuery();
                    sqlcon.Close();
                    bindassets("", "");
                    btnsaveasset.Enabled = false;
                    txtdescription.Text = "";
                    txtdtrecieved.Text = "";
                    txtcost.Text = "";
                    txtmodelno.Text = "";
                    txtquantity.Text = "";
                    lblErrMsg.Text = "data inserted successfully..";
                }
                catch
                {
                    lblErrMsg.Text = "Please enter valid data and try again...";
                }
            }
            else
            {
                lblErrMsg.Text = "Please enter valid data and try again...";
            }
       
    }

    protected void grdvAssets_Sorting(object sender, GridViewSortEventArgs e)
    {
        bindassets(e.SortExpression, sortOrder);
    }

    public string sortOrder
    {
        get
        {
            if (ViewState["sortOrder"].ToString() == "desc")
            {
                ViewState["sortOrder"] = "asc";
            }
            else
            {
                ViewState["sortOrder"] = "desc";
            }

            return ViewState["sortOrder"].ToString();
        }
        set
        {
            ViewState["sortOrder"] = value;
        }
    }




任何人都可以帮助我...谢谢您.




anyone please help me...thanks in advance.

推荐答案

您将gridview绑定在哪里?
如果它在PageLoad中,请从那里将其删除.
如有可能,请发布一些代码段,以便我们为您提供确切的解决方案.
Where you are binding your gridview?
If it is in PageLoad, remove it from there.
If possible please post some code snippet so that we can give you exact solution.


您好,

您遇到的问题的实际过程是浏览器要照顾以前的提交数据,即回发请求或正常请求.

您可以使用以下链接查找天气回发请求,该请求是由客户端单击事件或在刷新按钮上生成的.

http://www.codeproject.com/KB/aspnet/DetectingRefresh_.aspx

一切顺利.
Hi,

Actual process in your problem is browser is takecare of previous submission data i mean postback request or normal request.

you can find by using below link weather postback request is generated by client click event or on refresh button.

http://www.codeproject.com/KB/aspnet/DetectingRefresh_.aspx

All the Best.


这篇关于通过asp.net中的“保存"按钮将数据绑定到gridview之后,一旦刷新页面,就会触发“保存"按钮单击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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