asp.net 防止刷新页面时保存数据

pftdvrlh  于 2023-06-25  发布在  .NET
关注(0)|答案(1)|浏览(208)

问题是:我点击Button 1将数据保存到我的sqldatabase。在此之后,我的ClearAllData运行,所有字段为空。问题是当我刷新页面时,即使我所有的字段都是空的,而且我没有点击Button 1,新的数据被输入到我的sql数据库中。
我有!IsPostback的页面加载,但我猜有其他东西,我需要防止这个问题。

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
        }
    }
    public void ClearAllData()
    {
        DropDownList1.SelectedValue = DropDownList1.Items[0].ToString();
        DropDownList2.SelectedValue = DropDownList2.Items[0].ToString();
        DropDownList3.SelectedValue = DropDownList3.Items[0].ToString();
        DropDownList4.SelectedValue = DropDownList4.Items[0].ToString();
        DropDownList5.SelectedValue = DropDownList5.Items[0].ToString();
        TextBox1.Text = "";
        Label1.Text = "";

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(DropDownList5.SelectedValue))
        {
            SqlConnection con = new SqlConnection("Data Source=ucpdapps2;Initial Catalog=OnCallWeb;Integrated Security=True");
            con.Open();
            SqlCommand comm = new SqlCommand("Update Dispatcher_Roles set Name = '" + DropDownList1.SelectedValue + "',Position = '" + DropDownList2.SelectedValue + "',Roles = '" + TextBox1.Text + "',Status = '" + DropDownList3.SelectedValue + "',DispatcherCovering = '" + DropDownList4.SelectedValue + "' where Name='" + DropDownList1.SelectedValue + "'", con);
            comm.ExecuteNonQuery();
            con.Close();
            Label1.Text = "Update Saved";
            ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Successfully Updated');", true);
            ClearAllData();
        }
        if (!string.IsNullOrEmpty(DropDownList1.SelectedValue))
        {
            SqlConnection con = new SqlConnection("Data Source=ucpdapps2;Initial Catalog=OnCallWeb;Integrated Security=True");
            con.Open();
            SqlCommand comm = new SqlCommand("Insert into Dispatcher_Roles values ('" + DropDownList1.SelectedValue + "','" + DropDownList2.SelectedValue + "','" + TextBox1.Text + "','" + DropDownList3.SelectedValue + "','" + DropDownList4.SelectedValue + "')", con);
            comm.ExecuteNonQuery();
            con.Close();
            ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Successfully Added');", true);
            Label1.Text = "Entry Saved";
            ClearAllData();
        }
        if (DropDownList1.SelectedValue == "" || DropDownList2.SelectedValue == "" || TextBox1.Text == "")
        {
            Label1.Text = "Fill In All Fields";
        }

    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("Data Source=ucpdapps2;Initial Catalog=OnCallWeb;Integrated Security=True");
        con.Open();
        SqlCommand comm = new SqlCommand("Update Dispatcher_Roles set Name = '" + DropDownList1.SelectedValue + "',Position = '" + DropDownList2.SelectedValue + "',Roles = '" + TextBox1.Text + "',Status = '" + DropDownList3.SelectedValue + "',DispatcherCovering = '" + DropDownList4.SelectedValue + "' where Name='" + DropDownList1.SelectedValue + "'", con);
        comm.ExecuteNonQuery();
        con.Close();
        ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Successfully Updated');", true);
        ClearAllData();
    }

    protected void Button3_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("Data Source=ucpdapps2;Initial Catalog=OnCallWeb;Integrated Security=True");
        con.Open();
        SqlCommand comm = new SqlCommand("Delete Dispatcher_Roles Where Name='" + DropDownList1.SelectedValue + "'", con);
        comm.ExecuteNonQuery();
        con.Close();
        ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Successfully Deleted');", true);
        ClearAllData();
    }

    protected void Button5_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("Data Source=ucpdapps2;Initial Catalog=OnCallWeb;Integrated Security=True");
        con.Open();
        SqlCommand comm = new SqlCommand("select * from Dispatcher_Roles where Name= '" + DropDownList5.SelectedValue + "'", con);
        SqlDataReader r = comm.ExecuteReader();
        while (r.Read())
        {
            DropDownList1.SelectedValue = r.GetValue(1).ToString();
            DropDownList2.SelectedValue = r.GetValue(2).ToString();
            TextBox1.Text = r.GetValue(3).ToString();
            DropDownList3.SelectedValue = r.GetValue(4).ToString();
            DropDownList4.SelectedValue = r.GetValue(5).ToString();
        }
    }

    protected void Button6_Click(object sender, EventArgs e)
    {
        ClearAllData();
    }

    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {

    }
}
voj3qocg

voj3qocg1#

解决这些问题的最简单方法是使用所谓的“人类工程学”。换句话说,您不试图修复软件问题,但实际上改变了工作流以及您向最终用户展示数据编辑的方式。
因此,例如,假设您的用户在数据输入期间经常再次输入客户(重复数据输入)。
您可以通过在保存时间编写一堆代码来解决这个问题,在用户完成输入新客户的所有工作后,却发现客户已经存在?
好吧,因为用户不能编辑客户,或发票或做任何有用的工作没有首先做搜索?然后,“人类工程”的解决方案是总是启动用户在一个非常好的搜索页面。
所以,然后在一个真正好/好/容易/快速的搜索页面上创建和启动用户。
然后,他们将简单地键入几个字符,并首先找到客户,(您显示一些搜索结果)。然后,他们点击客户,编辑并点击保存,现在就回来与下一个客户/任务进行战斗。
事实证明,上述内容也方便了您的刷新问题,因为当他们保存时,您只需返回搜索页面,这样他们就可以在下一个客户上工作/查找。
在某些情况下,上述操作可能会花费额外的击键或额外的鼠标点击,但这解决了查找现有数据的搜索,并且它随后也倾向于解决刷新问题的问题,因为在数据输入期间,如果他们点击刷新而没有保存,则数据无论如何都不会被保存。
我喜欢这个UI:

所以,在上面,我没有刷新的问题,从来没有担心刷新,因为我弹出一个对话框来编辑一个记录,因此不必“清空”记录。
因此,通常,解决这些类型问题的更好的解决方案是“人工”设计界面,这样刷新就不会成为问题。
但是,你能做什么?当用户点击保存按钮时?对于保存代码的最后一行,执行响应。重定向(“到同一页”)强制重新加载该页。(反正你有一个后回来+往返旅行!)。结果将是一个空白的新记录,并且如果用户再次点击刷新按钮,则不会导致新添加的记录。
换句话说,在任何软件中,用户不能做任何有用的工作,除非他们首先找到并搜索并提出该记录。

相关问题