在将新记录插入数据库时​​,它正在网格视图中更新旧记录

时间:2019-04-29 11:44:48

标签: asp.net sql-server gridview

当我尝试添加新记录时,网格视图会更新旧记录。

在网格视图中,我有一个编辑选项。如果单击“编辑”,则该特定行将进入编辑屏幕并填充文本框并按预期进行更新。在网格视图中单击“添加新记录”按钮时,它将按预期添加带有空文本框的屏幕,但是如果我添加新记录,它将更新先前已编辑的记录。

public class NestedScrollViewHome  extends NestedScrollView {
    @SuppressWarnings("unused")
    private int slop;
    @SuppressWarnings("unused")
    private float mInitialMotionX;
    @SuppressWarnings("unused")
    private float mInitialMotionY;
    public NestedScrollViewHome(Context context) {
        super(context);
        init(context);
    }
    private void init(Context context) {
        ViewConfiguration config = ViewConfiguration.get(context);
        slop = config.getScaledEdgeSlop();
    }
    public NestedScrollViewHome(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }
    public NestedScrollViewHome(Context context, AttributeSet attrs,
                              int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }
    private float xDistance, yDistance, lastX, lastY;
    @SuppressWarnings("unused")
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        final float x = ev.getX();
        final float y = ev.getY();
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                xDistance = yDistance = 0f;
                lastX = ev.getX();
                lastY = ev.getY();
                // This is very important line that fixes
                computeScroll();
                break;
            case MotionEvent.ACTION_MOVE:
                final float curX = ev.getX();
                final float curY = ev.getY();
                xDistance += Math.abs(curX - lastX);
                yDistance += Math.abs(curY - lastY);
                lastX = curX;
                lastY = curY;
                if (xDistance > yDistance) {
                    return false;
                }
        }
        return super.onInterceptTouchEvent(ev);
    }
    public interface OnScrollChangedListener {
        void onScrollChanged(NestedScrollView who, int l, int t, int oldl,
                             int oldt);
    }
    private OnScrollChangedListener mOnScrollChangedListener;
    public void setOnScrollChangedListener(OnScrollChangedListener listener) {
        mOnScrollChangedListener = listener;
    }
    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        if (mOnScrollChangedListener != null) {
            mOnScrollChangedListener.onScrollChanged(this, l, t, oldl, oldt);
        }
    }
}

如果我单击“添加新记录”按钮,则在填充文本框后,它应该添加一个新记录,但是它正在更新刚添加新记录之前已编辑的旧记录。

protected void GV_PRIOR_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
    int pid = Convert.ToInt32(GV_PRIOR.DataKeys[e.NewSelectedIndex].Value);
    DataTable dt = bll.editprior(pid);
    foreach (DataRow dr in dt.Rows)
    {
        HiddenField1.Value = dr["P_ID"].ToString();
        tb_prioname.Text = dr["P_NAME"].ToString();
        chk_actprior.Checked = dr["P_ACTIVE"].ToString() == "Y";
    }
    btn_savprior.Text = "UPDATE";                
}

protected void btn_savprior_Click(object sender, EventArgs e)
{
    if (HiddenField1.Value == "")
    {
        bll.savprior(0, tb_prioname.Text, Convert.ToBoolean(chk_actprior.Checked));
        Response.Write("<script>alert('Priority Saved Successfully')</script>");

    }
    else
    {
        bll.savprior(Convert.ToInt16(HiddenField1.Value), tb_prioname.Text, Convert.ToBoolean(chk_actprior.Checked));
        Response.Write("<script>alert('Priority Updated Successfully')</script>");
    }
    bindgrid();              
}

1 个答案:

答案 0 :(得分:-1)

尝试清除HiddenField1.Value =“”; 在bindgrid()之前;