如何避免在Postback上从asp.net重复输入? [英] How to avoid duplicate entry from asp.net on Postback?

查看:76
本文介绍了如何避免在Postback上从asp.net重复输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个下拉列表,用于从模板表中提取数据.我有一个添加按钮来插入新模板.添加按钮将弹出jQuery弹出窗口以插入新值.将有一个保存按钮来保存新数据. On_Save_Click我输入新数据并关闭弹出窗口.

I have a dropdown list that pulls data from template table. I have an Add button to insert new template. Add button will brings up jQuery popup to insert new values. There will be a save button to save the new data. On_Save_Click I enter the new data and close the popup.

这是问题所在: 当我刷新页面时,页面会再次输入值.所以,我得到重复的条目!

Here is the proplem: When I refresh the page, the page entering the values again. So, I get duplicate entries!

问题: 如何避免这个问题?我查看了Satckoverflow和Google,他们都建议重定向到另一个页面.我不想将用户重定向到另一个页面.如何使用相同的表格来避免此问题?请帮忙.

Question: How can I avoid this issue? I check out Satckoverflow and Google, both they suggest to redirect to another page. I don't want to redirect the user to another page. How can I use the same form to avoid this issue? Please help.

推荐答案

您可以使用viewstate或session来指示是否已插入数据(按下按钮).

You can use viewstate or session to indicate if data already inserted (button pressed).

类似这样的东西:

private void OnbuttonAdd_click()
{
   if(ViewState["DataInserted"] != "1")
   {
      ...
      // Add new entry...
      ...
      if(data inserted successfully)
      {
         ViewState["DataInserted"] = "1";
      }
   }
}

public bool DataInserted
{
    get
    {
        if (HttpContext.Current.Session["DataInserted"] == null)
        {
            HttpContext.Current.Session["DataInserted"] = false;
        }
        bool? dataInserted = HttpContext.Current.Session["DataInserted"] as bool?;

        return dataInserted.Value;
    }
    set
    {
        HttpContext.Current.Session["DataInserted"] = value;
    }
}

...

private void OnbuttonAdd_click()
{
   if(!DataInserted)
   {
      ...
      // Add new entry...
      ...
      if(data inserted successfully)
      {
         DataInserted = true;
      }
   }
}

这篇关于如何避免在Postback上从asp.net重复输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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