一键保存和更新 [英] Save and Update using one button

查看:78
本文介绍了一键保存和更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

namespace RERMS.HR
{
    public partial class LateAttendance : System.Web.UI.Page
    {
        Button btnSave;
        protected void Page_Load(object sender, EventArgs e)
        {
            (this.Master as SiteMaster).SaveClicked += new EventHandler(Save_Click);
            (this.Master as SiteMaster).DeleteClicked += new EventHandler(Delete_Click);
            if (!IsPostBack)
            {  LoadGridView();
            }
            if (Request.QueryString["LateAttendanceApprovalID"] == null || Request.QueryString["LateAttendanceApprovalID"].ToString() == "")
            {}
            else if (Request.QueryString["LateAttendanceApprovalID"].ToString() != null || Request.QueryString["LateAttendanceApprovalID"].ToString() != "")
            {
                LateAttendanceApprovalForEdit();
            }
 }
 private void LateAttendanceApprovalForEdit()
        {
            btnSave = this.Master.FindControl("btnSave") as Button;
            if (btnSave.Text != "Update")
            {
                var LateRepository = new LateAttendanceRepository();
                var LateApprovalDto = new LateAttendanceApprovalDto();
                LateApprovalDto.LateAttendanceApprovalID = Convert.ToString(Request.QueryString["LateAttendanceApprovalID"]);
                LateApprovalDto = LateRepository.LoadLateAttendanceForEdit(LateApprovalDto).AttendanceApproval;
                txtApproveDate.Text = Convert.ToString(LateApprovalDto.ApprovalDate);
                txtArrivingTime.Text = Convert.ToString(LateApprovalDto.ArrivingTime);
                txtRemarks.Text = Convert.ToString(LateApprovalDto.Remarks);
                ddlEmployee.SelectedValue = LateApprovalDto.Employee.EmployeeID;
                btnSave.Text = "Update";
            }
        }

        private void LoadGridView()
        {
            LateAttendanceRepository LateRepository = new LateAttendanceRepository();
            LateRepository.LoadGridView(grdLateAttendance);
          
        }
void Save_Click(object sender, EventArgs e)
        {
            if (btnSave.Text == "Update")
            {
                UpdateLateAttendance();
            }
            else if (btnSave.Text == "Save")
            {
                SaveLateAttendance();
            }
             LoadGridView();
        }
        private void SaveLateAttendance()
        {
            var LateRepository = new LateAttendanceRepository();
            var LateApprovalDto = new LateAttendanceApprovalDto();
            var Company = new CompanyDto();
            var employeeDto = new EmployeeDto();
            var departmentDto = new DepartmentDto();
            LateApprovalDto.LateAttendanceApprovalID = "LT-10";
            LateApprovalDto.ApprovalDate = Convert.ToDateTime(txtApproveDate.Text);
            LateApprovalDto.ArrivingTime = Convert.ToDateTime(txtArrivingTime.Text); LateRepository.AddLateAttendance(LateApprovalDto);
        }
When i insert data then create this exception"Object reference not set to an instance of an object." Error line 81. because btnSave cant find value Update or Save.But i cant understand that.Sorry my poor English.
Source Error:

Line 79:         void Save_Click(object sender, EventArgs e)
Line 80:         {
Line 81:             if (btnSave.Text == "Update")
Line 82:             {
Line 83:                 UpdateAttendance();


请解决问题


pls solute the problem

推荐答案

请查看问题的所有注释-它们都是合理的.

首先,即使您从未同时使用它们,对这两个不同的操作使用相同的按钮也是一个坏主意.用户应该始终看到那里还有什么"的视觉提示.因此,更好的UI样式将具有两个单独的按钮,但每个按钮都应根据当前状态启用或禁用.并且此启用状态应在每次更改状态时进行更新.

现在,看看您在做什么!使用立即常量(例如保存"和更新")完全是不可接受的.更糟糕的是,您在代码的不同部分中使用了这样的常量值.您怎么能支持它?这几乎是不可能的.假设您要修改按钮名称之一.您要在几个地方进行更改并记住它们之间的关系吗?而且您的if (btnSave.Text == "Update")之类的检查会很好地解决问题.这甚至不是糟糕的编码-这根本不是编程.完全重写代码.这样的常数属于资源.至少要使它们成为显式常量,但永远不要对其进行硬编码.

最后,您没有显示引发异常的消息对象引用未设置为对象的实例",但这是最容易检测和修复的情况之一.它仅表示使用和实例(非静态)成员对某个引用类型的某个成员/变量进行了取消引用,这要求该成员/变量为非null,但实际上它似乎为null.只需在调试器下执行它,它将在引发异常的位置停止执行.在该行上放置一个断点,重新启动应用程序,然后再次到达该点.评估下一行中涉及的所有引用,看看哪一个为空,而不必为空.在弄清楚这一点之后,请修复代码:在确保成员/变量正确初始化为非null引用的情况下,或者检查它是否为null,如果为null,请执行其他操作.

结案.

-SA
Please see all the comments to the questions — they all are reasonable.

First of all, using the same button for those two different actions is a bad idea even if you never use them at the same time. The user should always see the visual clue on "what else is there". So, much better UI style would be having two separate buttons, but each should be enabled or disabled depending on current state; and this enable status should be updated each time the state is changed.

Now, look what you are doing! The use of immediate constants like "Save" and "Update" it totally unacceptable. Worse, you use such constant values in different parts of the code. How can you ever support it? This is nearly impossible. Imagine you want to modify one of the button names. Are you going to change it in several places and remember how they are related? And your check like if (btnSave.Text == "Update") will screw up things very well. This is not even bad coding — this is not programming at all. Rewrite the code completely. Such constants belong to resources; at least, make them explicit constants, but never ever hard-code them.

And finally, you did not show where the exception with the message "Object reference not set to an instance of an object" is thrown, but this is one of the very easiest cases to detect and fix. It simply means that some member/variable of some reference type is dereferenced by using and of its instance (non-static) members, which requires this member/variable to be non-null, but in fact it appears to be null. Simply execute it under debugger, it will stop the execution where the exception is thrown. Put a break point on that line, restart the application and come to this point again. Evaluate all references involved in next line and see which one is null while it needs to be not null. After you figure this out, fix the code: wither make sure the member/variable is properly initialized to a non-null reference, or check it for null and, in case of null, do something else.

Case closed.

—SA


void Save_Click(object sender, EventArgs e)
        {
            btnSave = this.Master.FindControl("btnSave") as Button;
            if (btnSave.Text == "Update")
            {
                UpdateLateAttendance();
            }
            else if (btnSave.Text == "Save")
            {
                SaveLateAttendance();
            }
             LoadGridView();
        }


这篇关于一键保存和更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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