This article scope is limited to Updating and Deleting records using Grid View and I am not using any readymade Data controls for that but manually writing all event methods.
I will be using Sql objects directly into methods to keep the example simple and straight forward.
Now let’s start with putting the Grid View on page. Data Key Names="Auto ID" Auto Generate Edit Button="True" On Row Editing="Edit Record" On Row Canceling Edit="Cancel Record" On Row Updating="Update Record" Cell Padding="4" Header Style-Horizontal Align="left" On Row Deleting="Delete Record" Row Style-Vertical Align="Top" Fore Color="#333333" Grid Lines="None" In the above code, I have kept Auto Generate Columns as false so that Grid View will not render all the fields of the data source automatically.
I have specified Data Key Names as Auto ID, the primary key of the database table.
Image button in popup header is used as Cancel Control ID but we also have a cancel button in our modal popup and here is the code to close the modal popup by cancel button without postback: If you can see we used if not is postback because we don’t want to load record on every postback but only for the first time, on page load.
We will try to create a basic user page where you can see all the users in a grid with paging and buttons to add new user, update an existing user and delete any existing user with confirmation box.
Add New User button click event handler, which clear all the control in modal popup and show the it #region[Add New User record into database] public bool Add New User(User Entity user) #region[Update User record into database] public bool Update User(User Entity user) #endregion #region[Delete User record from database] public bool Delete User By ID(Int32 user Id) #endregion #region[Get User to show in popup for edit] public User Entity Get User By ID(Int32 user ID) #endregion Save Button click event handler to save record into database, nothing is complicated, as we already seen at the time of adding new user we are setting userid to 0 means need to create a user otherwise update user.
Use the same modal popup extender which we use for adding new users and same html will be used, when we go for updating the record we will keep Id in hidden field and pass to update the rocord.
After it has been clicked the postback places that row in edit mode. At this point in time the above Edit Item Template is displayed with two Image Buttons within it.
One has it's Command Name = "Update" and the other "Cancel".