Steps to remove a row from a gridview by selecting the delete option on a form

I have a webpage in aspx format that features a gridview containing three fields and an "Update" button. Upon clicking the Update button, I am redirected to another aspx page with a form displaying more details about the selected entry from the grid view. This form includes additional fields and a "Delete" button. Upon clicking the Delete button, my objective is to close the current form and return to the gridview to delete the corresponding entry. In implementing this functionality, I have utilized TemplateField within my gridview.

<asp:GridView ID="GridView1" runat="server">
   <Columns>
     <asp:TemplateField ShowHeader="False" HeaderText=" ">
          <ItemTemplate>
              <asp:Button ID="Btn_Update" Text="Update" runat="server" ButtonType="Button" CommandName="update" />
          </ItemTemplate>
      </asp:TemplateField>
        <asp:BoundField DataField="ID" HeaderText="ID" />
        <asp:BoundField DataField="FirstName" HeaderText="First Name" />
        <asp:BoundField DataField="LastName" HeaderText="Last Name"  />
    </Columns>
</asp:GridView>

Following the closure of the form upon selecting the "Delete" button, the following code snippet details how I handle returning back to the gridview:

 protected void btn_Delete_Click(object sender, EventArgs e)
{

    #region Redirect to Page
    Page.ClientScript.RegisterStartupScript(this.GetType(), "RefreshParent", "<script language='javascript'>RefreshParent()</script>");
    Response.Write("<script>window.close();</" + "script>");
    #endregion

    ClearData();
}

I would greatly appreciate assistance on how to effectively remove the row from the gridview after executing the "Delete" action in the form. Thank you for your support!

Answer №1

Here is a code snippet to illustrate the concept. Although it is in WPF + C# and not web-based, the principle remains the same.

Main Program:

public delegate void DeleteRow(bool doDelete);

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    int selectedRow = 0;
    public DeleteRow deleteRowDelegate;

    public void ReportDelete(bool delete)
    {
        // Implement row deletion here.
    }

    public MainWindow()
    {
        InitializeComponent();
        deleteRowDelegate += new DeleteRow(ReportDelete);
    }

    private void btnOK_Click(object sender, RoutedEventArgs e)
    {            
        // Obtain the selected row number and assign it to selectedRow.

        SecondaryWin win = new SecondaryWin(deleteRowDelegate);
        win.ShowDialog();

        // If DELETE was clicked in the secondary window, ReportDelete() method would have been executed at this point.
    }
}

This is the code for your secondary window:

public partial class SecondaryWin : Window
{
    DeleteRow callbackDel;

    public SecondaryWin(DeleteRow callback)
    {
        InitializeComponent();
        callbackDel = callback;
    }

    private void btnDel_Click(object sender, RoutedEventArgs e)
    {
        callbackDel.Invoke(true);
        // Close the window
    }
}

In the main program, you register the ReportDelete() method with the DeleteRow delegate and then pass it to the secondary window. I passed it through the constructor, but you can use a different approach if desired.

In the secondary window, you can invoke the delegate by clicking the DELETE button and exiting the window.

Whenever DELETE is clicked in the secondary window while back in Main(), the code within the ReportDelete() method will be executed, allowing you to delete the specified row using selectedRow.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

HTML5 Boilerplate and optimizing the critical rendering path by deferring scripts and styles

Previously, I constructed my website layouts using the HTML5 Boilerplate method: incorporating styles and Modernizr in the head section, jQuery (either from Google CDN or as a hosted file) along with scripts just before the closing body tag. This is an exa ...

The authentication callback function fails to execute within Auth0 Lock

I'm having an issue with logging into my application using Auth0. I have integrated Auth0 Lock version 10.3.0 through a CDN link in my project and am utilizing it as shown below: let options = { disableSignupAction: true, rememberLastLogin: f ...

How can I verify the presence of items in a list box and determine if the list box contains any duplicates in C# programming language?

private void button1_Click(object sender, EventArgs e) { listBox1.Items.Add(textBox1.Text); } private void button2_Click(object sender, EventArgs e) { string val = listBox1.Text.Trim(); if (list ...

Delivering static files on Asp.net 5 for Angular single-page applications

In my latest project, I developed a Single Page Application using Angular and Asp.net 5. The routing paths in Angular are configured to return index.html by default, allowing Angular to handle further routes. Everything works smoothly when I access simple ...

What is the best way to submit form data along with an image using Angular?

Recently, I built an application where users can submit form data along with an uploaded image. Since I am new to Angular, I am facing some challenges in merging the user-submitted data model and the image upload using the FormData method. Can anyone guide ...

What are the differences between a Django/React app API when in production versus during development?

Recently, I created a React app that relies on Axios for fetching APIs. During the development phase, I noticed an issue with the API URL configuration. Since my ReactApp was hosted on localhost:3000 but making API calls to 127.0.0.1 - I encountered some e ...

What is the best way to display the answer on a webpage?

... index.html <body> <h1>Upload Test</h1> <!--<form method="post" enctype="multipart/form-data" action="/picture"> --> <form id="pictureForm" method=& ...

Tracking ajax calls with piwik: A step-by-step guide

I'm curious about how to enable piwik to track ajax requests. I know there is an API available, but I'm unsure about the exact steps I need to take in order to view ajax loaded pages in the dashboard. Could it be something like this: _paq.push( ...

The Model Viewer module is unable to load the three-dimensional model

Attempting to incorporate a 3D model into my website using the modelviewer library, but encountering difficulties in loading the file as shown below: Just to note, I am utilizing github pages with a custom domain from godaddy which may be contributing to ...

Determining if a URL links to an image when the file extension is not informative

I am currently working on building an AJAX call to retrieve data from an API with a messy data structure. The challenge I'm facing is that the array returned by each AJAX call can contain up to 30 elements, some of which have image URLs without a file ...

Challenges with customized controls in Google Maps version 3

I recently integrated a Google map on my website and added some custom tooltips using custom overlays. You can check it out here: However, I've noticed that sometimes the map seems to freeze - I can't drag it or interact with any of the buttons. ...

Getting props value in parent component using Vue JS

In my development project, I am working with three key components: component-1, component-2, and an App component. Initially, I pass a Boolean prop from component-1 to component-2. Through the use of a @click event, I am able to toggle this prop value betw ...

The export named 'PDFDataRangeTransport' could not be located when utilizing react-pdf in a Next.js application

Hey there! I'm currently working on rendering a PDF using the react-pdf library in my next app, but I've run into a problem: SyntaxError: The named export 'PDFDataRangeTransport' cannot be found in the module 'pdfjs-dist'. Th ...

Creating a stunning image carousel in Vue by integrating a photo API: step-by-step guide

Trying to figure out how to create an image carousel in Vue using photos from an API. Currently able to display the photos using: <section class="images"> <img v-for="image in images" :key="image.id":src="image.assets.large.url"> &l ...

Displaying duration of time through JQuery along with present day and time

I'm working on a web application where I display the date and time of a posted request in the user interface. Now, I want to enhance this by showing how long ago that request was posted relative to the current Date and time. Is there a simple way to ...

A more concise method to verify if something is undefined in JavaScript

Anyone have suggestions for a more concise idiom to use? const x = module || window; // Reference Error fallback Is there a shorter method to verify the presence of module? ...

The .value property on the form group displays numeric values as either null or an empty string

I'm encountering an issue with extracting data from a form group. Within my code, there is a formGroup named lineitemForm, and I am attempting to structure this form group as follows: private formatTransferData() { const depositDates = this.get ...

What is the method to generate an ApnSetting entity?

My attempts to create an ApnSetting object have been unsuccessful as the result is consistently null. using var apn = new ApnSetting.Builder(); apn.SetEntryName("APN name"); apn.SetApnName("some.apn"); var test = apn.Build(); The test ...

Encountering the error "Uncaught reference error: $ is not defined" despite ensuring that scripts are loading in the correct order. However, the scripts work from the

Encountering an Issue: "Uncaught ReferenceError: $ is not defined" Whenever I try to utilize $('#someid') in my custom JavaScript code within an Electron app. All scripts are properly arranged in my HTML file: <script type="text/javascri ...

Struggling to convert XML into an object

I am facing a challenge with deserializing an XML response from a web service. The structure of the response is as follows: <CreateSubscribersResultCollection xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> <CreatedSubscriberIds xmlns:a= ...