C# gridview nested in repeater update - c#

My current situation is to display an unknown number of Plantypes and within those Plantypes display a list of Participants(also unknown number), the participants have a textbox and a dropdown that is editable (you can't edit the individual rows, there is one update that does a bit of validation then updates all rows.)
I currently have a gridview nested withing a repeater, the repeater displays the Plan in a label and OnItemDataBound I call a method to populate the gridviews. It looks great, but I can't figure out how to save all the data at once. I'm not opposed to handling this a different way, as in loose the gridview and or repeater, if someone has a better idea.
This is C# and framework 2.0...there is no sorting or paging on the gridviews...just some links and the fields to update.
thanks in advance,
Padawan

HTTP is stateless. Since a user physically can only update one record at a time you should implement some way to save as the user goes. This can be a simple as having a row focus and row blur events and do the saves using AJAX on blur(unfocus). Maybe make is modal as they edit so they cannot leave the page without saving.

Related

Manage state in MVC4

I have a page with around 20+ controls. Within this page I have a partial View which I am using to create an editable grid. This grid has Add New Row/Remove Row buttons on click of which data entered in the textboxes in grid row gets added to the grid.
Whenever I click these 2 buttons, my page is getting refreshed due to which whatever data is being entered by the user in the 20+ controls of main page is lost.
I have tried hopelessly searching for solutions quite some time, but still trying my luck out here. Any ideas are appreciated.
Well I recommend you to use full jquery mechanisms to add or remove rows. You can easily add or remove HTML elements. All you have to care about is to maintain the numbering system. I suggest you to use a view model that contains a list of objects that you need to add or remove. At first render it normally by providing list of objects (from database or other source, if required). After this define your add and remove buttons to type button and play with jquery to add remove table rows or even divs. After adding or removing required element, reorganize your numbering. At submit you can use either full postback or ajax postback. If Any confusion, let me know.

Add usercontrols dynamically inside a GridView

Here's my problem. I am trying to add usercontrols dynamically to different rows of a GridView.
Let me explain the scenario further. Let's say, I have a page where the user can place orders for different kinds of items. The order form for each item is different in layout and code, and has its own usercontrol. There is an "Add More" button, which lets the user add an extra row to the GridView, while the kind of form that gets appended is selected by the dropdown list (see picture below).
What I am trying to do is - have a separate UserControl for each one of these forms, and then have the UserControl added (or maybe I should say "bound") to the GridView. Every time a new row is added, I plan to re-bind the GridView, including rows from above, to maintain all the rows.
The problem is - the main page cannot know what fields there will be inside each individual UserControl (they can be anything, really, and down the road, newer UCs could be added). So my main question is - how do I bind the data to the GridView, when I do not know what to bind? Is there any way to bind the data from inside each UserControl itself?
I hope my problem and question are clear enough. Thanks for helping.

Navigation grid inside of a FormView

We have a list of elements we use to drive a form view. Typically the resultset is between 5-15 records. Right now in the form view we have the typical first/prior/next/last for navigation. The problem is that sometimes the user needs to go directly to a specific record to edit it. The records here are job tasks that have some pretty short descriptions.
The idea was proposed to put a gridview inside of the formview that lists all of the records for that form, and a user can just select view or edit on that record and it will navigate directly to that record and put it into the proper mode. We could put this outside the form view as well, that doesn't really matter.
The question is, regardless of the dirving force, how to I tell the formview to go to record X driven from something like an external grid.
I know that the formview has the DataKeyNames field but is there a way to say "Go to record who's PK is 17" given that it's in the current dataset for the formview?
If so, does anyone have any sample C# code for it? I know we could just populate the existing formview with a single record, but we also want to keep the normal navigation buttons in place as well, in the event as well (sometimes there's cases where there's hundreds of job tasks, in which case we'd supress the grid view -- sounds wrong but there's more to the business case).
This short solution was to embed the grid in the FormView ItemTemplate and reference the same data source for both.
To made the row editable, you need a simple callback for RowDataBound on the grid in which you will retrive the link button (or whatever control you're using to trigger the edit) and then set the command argument to the rowindex. After that, you need to have the callback for the link button (or trigger) that wil retrieve the command argument (again the row index) and then set the FormView PageIndex to that value, and then set the ChangeMode for the FormView to FormViewMode.Edit.
So to recap, for read only view it's a grid with all of the items, but when handle the edit or insert, you get the traditional form view.

Checkbox control in gridview

I have a gridview with checkbox control as a template field and one bounded field..
The grid is binded on every postback through a function and the grid contents remain the same on every post back..now when i check one of the checkboxes and then click the button at the end of the page, I need to store that particular row information..but I'm not able to retreive that information because when I check and then click button..the page loads and then the grid again populates and then checkboxes become uncheck and no CheckedChanged event fires..Help me with this
I need to persist the state of checkbox on every postback even when it is checked..how to do this??
In the page_load event function, please use the following code for your persistent data
if (!IsPostBack)
{
//your static data
}
This particular problem is fairly common. I haven't seen any "simple" solution yet, but here are 3 separate methods I have used. Each was used because of a limitation in the system.
Solution 1
Use AJAX. By putting your controls within an update panel, you can persist the changes by making them "real-time" in the database. This is not really a "simple" solution, but in my opinion it is one of the easiest to implement. Since the change is psuedo-immediate, there is no real need to worry about post-backs and persistence.
Solution 2
Use a "change management" control of sorts. You can apply a hidden control whose value is used to keep track of any changes made in relevant controls. You would need to devise a coherent data structure that provide at least a control ID and the new value (possibly the old value if you need some kind of "roll-back" feature). This would need to be coded in JavaScript so that any changes to the hidden control's value were structured and not duplicated. Then on your postback you would need to read this control's value, make any pertinent changes, and then rebind your data as appropriate. This can be fairly cumbersome, and it would need to be well-documented in the event that you pass this application on to a successor.
Solution 3
Use the PostBack for CheckChanged events and keep all data managed in the view state. During the RowItemCreated event of the GridView you can find the checkbox control in the relevant cell and manually add the delegate handler to that control to handle the postback in the event of a CheckChanged event firing. You can then have the change immediate. The drawback to this is that PostBack events become frequent and heavy. If you're storing large amounts of data in the ViewState this also causes page load to be slow and unresponsive, so whatever structure you choose for the ViewState you'll want to keep it small.
This is possible if you are using asp.net 4.0 using
<asp:GridView id="GridView2" runat="server" EnablePersistedSelection="true">
</asp:GridView>
If you are using 3.5, you will have to retain checkbox info in viewstate. I hope this will be helpful.
http://www.codeproject.com/Articles/202938/How-to-select-multiple-records-from-the-GridView-a
Another Option:
This is how msdn have described a hotmail type gridview.. may be this can help.. this will require you to extend existing GridView Control.
http://msdn.microsoft.com/en-us/magazine/cc163612.aspx
Regards.

Inserting , updating and deleting of Data

I want to make a control that show list of bounded data like in grid view but I want to be able to insert new record from the same control
what is the best asp.net control I can use to do that (GridView , FormView or DetailsView)? and why?
Thanks in Advance
You can do that with all three. If you want to know how, there are some good tutorials on the official ASP.NET website, here.
The choice between them is a formatting/layout decision. A GridView produces a table, showing multiple records, while a DetailsView shows one at a time. A FormView lets you display the data however you want, but required much more effort, since you have to provide the HTML.

Categories

Resources