How to reference a ListView control using its DataSource in a C# class

Currently, I have a ListView embedded in a webpage connected to a C# class datasource called CommentsDAO. This class contains the necessary methods to retrieve or delete data from the ListView. While data retrieval is successful, deleting a row proves to be challenging as the selected row needs to be obtained. However, the ListView instance is not within the scope of its datasource. To address this issue, there is a button (imgbtn1) adjacent to each row with a "Delete" command.

<asp:ListView ID="ListView1" runat="server" DataSourceID="ObjectDataSource1">
                    <LayoutTemplate>
                        <div id="itemPlaceholderContainer" runat="server" style="">
                            <span id="itemPlaceholder" runat="server" />
                        </div>
                    </LayoutTemplate>
                    <ItemTemplate>
                        <span>
                            <asp:Label ID="Label1" Font-Size="Small" runat="server" Text='<%# Eval("User") %>' />
                            <table>
                                <tr>
                                    <td>
                                        <div style="border: 1px solid; background-color: #F0F8FF; padding: 10px; height: 50px;
                                            overflow: auto">
                                            <asp:Label ID="ComentLabel" runat="server" Text='<%# Eval("Coment") %>' />
                                        </div>
                                    </td>
                                    <td valign="top">
                                        <asp:ImageButton ID="imgbtn1" CommandName="Delete" runat="server" ImageUrl="~/trash16x16.gif" />
                                    </td>
                                </tr>
                            </table>
                            <br />
                        </span>
                    </ItemTemplate>
                </asp:ListView>

...

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" DataObjectTypeName="ModalPopupDemo.Comentario"
    SelectMethod="FindAll" TypeName="ModalPopupDemo.ComentarioDAO" DeleteMethod="Delete">
</asp:ObjectDataSource>

My requirements are as follows:
- Users can leave comments (...)
- A vertical ListView displays these comments (strings).
- Upon clicking an external button, a Jquery popup featuring a wysiwyg editor will appear.
- After closing this popup, an Ajax call must be made to the server to save the editor content into a database.
- Subsequently, the ListView should be updated with the user-entered data from the popup (via Ajax).
- If the comment author is logged in, a delete icon should be displayed next to the comment.
- Clicking on this icon triggers an Ajax call to delete the comment from the database and refresh the ListView.

Considering the above requirements, would the ListView control be the most suitable choice for this task? Alternatively, should I utilize HTML tables along with jQuery scripting?

Your patience and assistance are greatly appreciated. Regards, Toni

Answer №1

If I'm following correctly, are you attempting to make modifications to the ListView through a custom DataSource object that you've developed?

Let me take a guess here, if your aim is to remove data from the DataSource using your delete method, then you'll have to rebind the ListView so it reflects the changes you've made. It seems like you're interested in editing the ListView directly from within the DataSource, which could be seen as an unconventional approach.

One way you could potentially achieve this is by including a ListViewObject property within your DataSource class and passing the instance of the ListView to it. However, it might be simpler to just modify the DataSource and rebind the ListView when necessary.

For the delete functionality, utilize the CommandArgument to send the item index (or any other identifier) and in the OnItemCommand event, retrieve the identifier from the CommandArgument and delete the specified item from your DataSource using that identifier. Once the item has been removed from the DataSource, rebind the ListView.

Does that explanation provide some insight, or have I misinterpreted your requirements?

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

Highlighting the content within Material-UI's <Dialog> using ReactJS without affecting the entire webpage

I have an issue in my ReactJS project with the <Dialog> component from Material-UI (http://www.material-ui.com/#/components/dialog). When I open the <Dialog> and press command + a on my Mac, it highlights the entire page instead of just the con ...

Is there a way to showcase every published WordPress page in a drop-down menu?

I am having trouble displaying all the published pages within my WordPress site as a dropdown list. Here is the code I have attempted to use: <div class="header-right"> <?php $pages = get_pages(); $posts = get_pages(array( ...

Adjusting font size in dynamic containers relatively

Imagine we have a slider named .outer, which adjusts its height based on the width of the viewport. Inside this slider, there is an example slide called .inner that contains some text. However, when we shrink the window width, the content in slide .inner s ...

Mastering the art of concurrent Ajax requests using jQuery for an advanced Posting and Commenting system

In my Asp.net MVC project, I have successfully implemented a post and comment mechanism. The posts and comments are stored in different tables in the database. Additionally, using Ajax requests with jQuery, I can retrieve comments from the database and dis ...

Adjust the size of text with jQuery to make it larger or smaller

I run a news website and I'm looking to add buttons that allow users to increase or decrease the font size of the page. I have set a default text size in CSS like this: <style> * { font-size: 12px ; } </style ...

Activate the radio button with jQuery when the event occurs

I have been experimenting with using HTML and JS along with the functionality provided by bootstrap-switch.org to toggle between two groups of radio buttons within a form. However, I am encountering difficulty in setting the first radio button in the secon ...

Unable to proceed due to lint errors; after conducting research, the issue still remains

I'm still getting the hang of tslint and typescript. The error I'm encountering has me stumped. Can someone guide me on resolving it? I've searched extensively but haven't been able to find a solution. Sharing my code snippet below. (n ...

html interactive/expandable tree

I've come across this code which generates an HTML tree, but I'm facing an issue where the tree expands every time I refresh the page. What I want to achieve is to have certain branches expanded and others collapsed when the page is opened, depe ...

JQuery animations not functioning as expected

I've been attempting to create a functionality where list items can scroll up and down upon clicking a link. Unfortunately, I haven't been able to achieve the desired outcome. UPDATE: Included a JSFiddle jQuery Code: $(document).ready(function ...

Issue with jquery .click function not triggering after CSS adjustment

In the process of creating a fun game where two players take turns drawing on a grid by clicking <td> elements. Currently, I have implemented the functionality to toggle the background color of a <td> element from black to white and vice versa ...

Align the asp.net content generated towards the left side

I am looking to optimize the layout of my asp.net code by shifting the content on all pages to the left side of the screen for better efficiency. Despite my efforts, I have been unable to find a suitable CSS solution that works universally across all view ...

Experiencing difficulty retrieving form data within Google Apps Script

My attempt to send form data to google sheet via ajax is encountering an issue with the if else statement in the google script not functioning correctly. Here is the snippet of the google script: // original from: http://mashe.hawksey.info/2014/07/google- ...

Obtaining array value in JSON and incorporating it into a datatable

I have the following JSON data: { "aaData": [ [ { "displayValue": "Home Page", "link": "http://somelink.com" }, "London", "1983" ], [ { ...

Unable to save data retrieved using jQuery JSONP

My current project involves fetching photo data from Flickr using a jQuery AJAX call with JSONP. However, instead of immediately using the data, I want to store it for future use. In some cases, users will be able to perform different queries on the pre-fe ...

jQuery Bootgrid Pagination issue: Unable to successfully pass the current page element

My experience with jQuery Bootgrid has been a bit challenging as I encountered 2 problems related to Pagination. Below is the JavaScript code that I have been working with: The data method in the code below seems to be defaulting to GET, even though I ...

When an item in the accordion is clicked, the modal's left side scroll bar automatically scrolls to the top. Is there a solution to prevent this behavior and

When I first load the page and click on the Sales accordion, then proceed to click on Total reported and forecasted sales, the scrollbar jumps back up to the top The marked ng-container is specifically for the UI of Total reported and forecasted sales He ...

A guide to examining the HTTP response body, including HTML content, using Wireshark

After entering the URL in the address bar of the virtual machine's browser, a request for an HTML document from my host computer was made. The HTML document, which I had personally written, successfully appeared in the virtual machine's browser. ...

Change a comma delimited string into a JSON formatted array

Is there a way to convert a string of comma separated values into valid JSON format using either PHP or jQuery? The comma separated strings are currently stored in a database and have the following structure: Singapore,Andora,Australia The desired outpu ...

As the cursor moves, the image follows along and rotates in sync with its

Can anyone help me figure out how to create a moving image that follows the mouse cursor? I have a radial pie menu with an image in the middle, and I want it to spin and rotate as the mouse moves. Any ideas on how I can achieve this effect? I would greatl ...

The div element continuously wraps its content when the browser size is reduced

Is there a way to prevent a div from wrapping or shifting below when resizing the browser to the left? I want this specific box to remain fixed in its position, causing the user to use the horizontal scroll bar to view it while everything else on the pag ...