页面重新加载时自动触发按钮事件 [英] Button event automatically fired when page reload

查看:80
本文介绍了页面重新加载时自动触发按钮事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨.....



这里我的问题是当我点击保存按钮数据将保存...当我重新加载我的页面时它会再次保存< br $> b $ b

我的代码是....





hi....

here my problem is when i click save button data will save... and when i reload my page it will save again

my code is....


try
        {
            string confirmValue = Request.Form["confirm_value"];
            if (confirmValue == "Yes")
            {
                //this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('You clicked YES!')", true);
                if (txtcusid.Text == "")
                {
                    ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Choose Customer ID')</script>");

                }
                else if (txtcusname.Text == "")
                {
                    ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Choose Customer Name')</script>");

                }
                else if (txtcusnumber.Text == "")
                {
                    ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Choose Customer Number ')</script>");
                }
                else if (txtservicetype.Text == "")
                {
                    ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Choose Service Type')</script>");
                }
                else if (DDlProductname.Text == "")
                {
                    ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Choose Product Name')</script>");
                }
                else if (txtCompliant.Text == "")
                {
                    ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Enter Compliant ID')</script>");
                }
                else if (txtcompdate.Text == "")
                {
                    ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Choose Date')</script>");
                }
                else if (txtsolution.Text == "")
                {
                    ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Enter Solution')</script>");
                }
                else if (DDlAssign.Text == "")
                {
                    ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Choose Assign')</script>");
                }
                else if (txtdatetocom.Text == "")
                {
                    ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Choose Compliant date')</script>");
                }
                else if (DDlPriorty.Text == "")
                {
                    ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Choose Priorty')</script>");
                }
                else if (DDlStatus.Text == "")
                {
                    ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Choose Status')</script>");
                }
                else
                {

                    connection();
                    query = "UpIn_SP";
                    cmd = new SqlCommand(query, con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@compliantID", txtcompid.Text).ToString();
                    cmd.Parameters.AddWithValue("@customerid", txtcusid.Text).ToString();
                    cmd.Parameters.AddWithValue("@customername", txtcusname.Text).ToString();
                    cmd.Parameters.AddWithValue("@customernumber", txtcusnumber.Text).ToString();
                    cmd.Parameters.AddWithValue("@contactperson", txtcompname.Text).ToString();
                    cmd.Parameters.AddWithValue("@productname", DDlProductname.Text).ToString();
                    cmd.Parameters.AddWithValue("@servicetype", txtservicetype.Text).ToString();
                    cmd.Parameters.AddWithValue("@compliant", txtCompliant.Text).ToString();
                    cmd.Parameters.AddWithValue("@compdate", Convert.ToDateTime(txtcompdate.Text));
                    cmd.Parameters.AddWithValue("@solution", txtsolution.Text).ToString();
                    cmd.Parameters.AddWithValue("@assign", DDlAssign.Text).ToString();
                    cmd.Parameters.AddWithValue("@datetocomp", Convert.ToDateTime(txtdatetocom.Text));
                    cmd.Parameters.AddWithValue("@priorty", DDlPriorty.Text).ToString();
                    cmd.Parameters.AddWithValue("@status", DDlStatus.Text).ToString();
                    int result = cmd.ExecuteNonQuery();
                    //con.Close();
                    if (result > 0)
                    {

                        //ViewState["VScompID"] = txtcompid.Text;
                        ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Data Updated SuccessFully')</script>");

                    }
                    else
                    {
                        ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Data Not Saved')</script>");
                    }
                }
            }
            else
            {
                this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('You clicked NO!')", true);
            }
        }
        catch (Exception ex)
        {

        }
        finally
        {
            con.Close();
        }

推荐答案

是的,您可以这样做。也就是说,当您保存内容时将页面重定向到单独的页面,它显示您保存的内容或其他内容的详细信息(即使用GET请求的请求页面)。



此模式称为 PRG模式。您可以将此应用于您的ASP.net应用程序。请查看以下链接以获取更多信息。



样本



Yes,You can do something like this.That is ,when you save the content redirect your page into separate page which it shows the details of what you saved or something else (i.e. The requested Page which use GET request).

This pattern is called PRG pattern.You can apply this with your ASP.net app.Please check below link for more info.

Sample

protected void SaveClicked(object sender, EventArgs e)
{
    //save changes to database here

    //Redirect if all went well
    Response.Redirect("http://www.mysite.com/customer.aspx?CustomerId="
        + idOfSavedCustomer.ToString());
}





重定向后 - 使用ASP.NET获取


这篇关于页面重新加载时自动触发按钮事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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