I have a database with 2 records Id and Description.
What I want to do is try to bind this to a table so for example
<tr>
<drop down select list with ids available> <textbox>
</tr> <add button>
So the user can select an id from the drop down list, enter a description then click an add button next to this which will duplicate that block dynamically so they can enter as many as they like. What is the best way to go about this in webforms? Detailsview? I'm not sure how to make it dynamically add html blocks though? Any help would be appreciated.
pseudocode :
foreach description
create new tablerow
create a table cell in the row with the description as a label
create a table cell in the row with the id dropdown as a combobox
add the tablerow to the table.rows collection
the tricky part is accessing the data in your dynamically created table, but it shouldn't be hard to do as long as you set an identifier on each control that can be tied back to your data.
I'd stay away from any of the built in controls (i.e. DetailsView, FormView) for anything other than simple CRUD forms, as it's not much more effort to manually create your own data-entry forms.
Dynamically adding controls to an ASP.NET Webform (and have them work across postbacks) is quite a tricky one to get right, but in short you'll need to do something like:
Add a table server control which will store your dynamic rows.
Create a property (stored in ViewState or ControlState) to store a count of rows available.
Handle the appropriate 'add' button click event, to increment the count property and add a new table row, and child controls.
Inside an OnInit event, create the number of table rows stored in the count property.
Dynamically created controls aren't persisted across postbacks, so you'll need to create them on every postback during the pages OnInit method. As long as the controls are recreated in the right order and with the same ID's, then they will be repopulated successfully on postback.
Because you're dynamically adding controls, you may need to set the pages EnableEventValidation property to false.
Related
I have a aspx page consisting of a calendar button and a textbox.When we click calendar button then a calendar appears and the date which i select on calendar appears in textbox. Now what i want is to load the data from database according to the date selected and allow the user to edit it.And also i want to enable user to add rows at runtime. Do i need to create grid view at runtime?? Can somebody point me in a direction??
You don't need to create the gridview at runtime. You can define the gridview in the markup without having any data initially. You can set the calendar to autopostback and on the server side, capture the new date selected, grab the data based on that date and bind it to your gridview.
As far as editing the data, Gridviews already provide support for this and the amount of code that you need to write largely depends on how the datasource for the gridview is set up. If you use a SQLdatasource with the appropriate settings you practically don't have to write a single line of code besides the markup (Google: Gridview SQLdatasurce editing ASP.net).
Which control should I use to show post records from my database in ASP.NET?
On my project page I would like to show recently featured projects. The number of projects can vary. I'm looking for a control like GridView or something that alter the properties of accordingly to get data from my database.
Below is an image of what I would like my page to look like:
I have been told to use asp:ListView but I don't know how to use it. Is this the right solution for my problem?
You could use a GridView Class
Displays the values of a data source in a table where each column
represents a field and each row represents a record. The GridView
control enables you to select, sort, and edit these items.
Or a Repeater Class
A data-bound list control that allows custom layout by repeating a
specified template for each item displayed in the list.
I have created a simple web form on ASP.NET which includes a DropDownList with the names of all the records of a table (h. PhotoAlbums) in my database. When the user selects an album, all the items of this album (photos with tootlips) appear on the page in an order. I have carried out this by the aid of ListView control assigning a LINQ source as data source.
I would like by the selection of an album to grant the user the possibility to insert new items in the selected album at the same time and on the same page besides the appearance of the already existed items in the album. I think that I should merely use a DetailsView control and adjust this, so as to insert items. Is it though possible the DetailsView to appear after the selection of an album; not when the page is loaded initially? How?
If I understand your question,
You can Initially set the visible = "false" to the DetailsView. Then in your DropDownList OnSelectedIndexChanged method you can check the selected value and set the visible= "true". In your DropDownList you should set the property AutoPostBack="true"
Hope This Helps
I want to do something really simple, I just can't seem to find the EnableClientAddRow property, so I can set it to true. I have a standard GridView control on a web form. I want a button to appear on the web form. When the user clicks the button, an empty row is added to the GridView UI, so the user can enter data in the appropriate fields. The row will of course, have a "Save" button of some type in one of the columns.
I know this functionality must be in the GridView somewhere, I just can't find it. I did find some odd hacks that try to manually implement this. I'm not really interested in footer manipulations or binding tricks, just the standard add row method.
EDIT:
It appears the GridView does not support adding a row as a first-order operation. This appears to be a serious design flaw.
I typically add a new record to the underlying data source as a part of the "add record" button click action. I then re-bind the view in order to show the blank row.
The new record is typically a DataRow if the GridView is bound to a DataTable, or an object if the GridView is bound to a collection of a particular type. Not sure if that is what you consider a binding trick from your question, but it works well and is quite easy to implement.
Edit - more detail to describe the process:
Add the row to the data source, set the EditItemIndex to the newly added row in order for the row to enter edit mode, then bind the data source to the GridView. Your EditItemTemplate would contain a Cancel and a Save button. Cancel would re-bind the GridView to the underlying data source without the empty row and set EditItemIndex to -1, thereby removing the row from the GridView.
How to easily insert row in GridView with SqlDataSource
If you add a new row to the datasource, even if the row has empty values, and you databind the datasource to the Gridview, it should show up as an editable row just like any of the other rows.
I have a GridView that allows editing the values in every column, in every row, all the time. The user enters in all their changes, clicks Save once and all changes are commited.
The user must also be able to click the New button, have a new row appear in the GridView (yep, it has to show up in the actual GridView), enter whatever data they want, click Save and have all changes go to the database.
Now. Here's the user case that's throwing me: a user arrives at the page, makes several changes on several existing rows, and then needs to add a new row, enter data in the new row, click Save, and have all changes go to the database.
However, the only ways I've seen to add a new, empty row involve rebinding the GridView, which means all of their changes will be lost. This is obviously no good.
So, my question would be: what are some approaches to adding a new, empty, editable row to a GridView without having to rebind the GridView?
The only thing I can think of would be, on the New buttons' click event, suck all the data out of the GridView (including the user's potential edits), save it to ViewState (or whatever), add the new row, repopulate the grid. This, to me, seems a little hacky, but it should allow me to turn ViewState off on the GridView.
Any ideas?
Just off my head I can think of two options. The first is to cache the original results that you are binding into the grid and when you need to add another row you add a datarow to the datatabale that you are binding to and then bind this to the grid. If there are changes in the grid then you need to update the datatable. Once all changes have been made and the user clicks the save button you can iterate through the table and update the DB with the data.
It might look like this
Page Loads
Get DB Data and put into a table
Bind the table to the grid
Store the table in a cache
When user asks for a new row
Get the cached data object.
Update any rows that have changed
Add an empty row Bind to the grid
When the user saves the grid
Get the cached object.
Make last set of updates
Loop through the row and update the DB
The other way to do this is by creating the grid dynamically but this will involve far more effort than it's worth given what you have described.
You could dynamically add the new row via javascript, and on the save command look for newly added rows. That is fairly common.