Adding rows to a table based on user input (ASP.NEt) - c#

I have a TextBox entry field where the user will enter a integer value. And then there is a "Create" button, which when clicked upon must generate a Table with 2 columns :
"Name" and "Email" being the column headers.
I want each row to have a textbox in each of these columns.
All of this has to happen after the button is clicked. I have discovered that if you dynamically add a control in ASP.NET(I am using C#) then the controls are lost during postback. And I don't know how to prevent that from happening.
Can somebody please give me some ideas regarding how to go about adding rows dynamically to a table (I tried using the asp.net Server side table control but ran into the "lost-during-postback" problem - can I try with something else like a gridview ? but afaik a GV will not work without data bound to it )
Point to note is that my table has textboxes for user entry and it is not for showing data ..rather it is for accepting data from the user which will be later used to persist details to the database.

Dynamic Controls
That's involved. Here's an interesting article on the issue of dynamic controls.
I think normally if you create dynamic controls then you're responsible for recreating them on postback; however the article contains a way to use the Init event if it's copacetic with your app.
Edit:
or Regular ASP.NET Controls
You can use data bound ASP.NET controls like: DataList, Repeater, GridView, etc. (You can put text boxes and all other kinds of controls into them for repeating in each row - or "item") and bind values to it. If you need to perform some processing on the user input first, then generate an internal Array, List, Dictionary, DataTable etc. (your choice) and bind that data as a data source to the ASP.NET control of choice.
aspnetControl.DataSource = myDataTable;
aspnetControl.DataBind();
or
aspnetControl.DataSourceId = "name_of_control";
There are various ways to assign a data source.
This way you won't run into the same problems as with dynamic control creation; however there is a lot to learn to facilitate this way too.

Related

Multiple row input in asp.net for editing database table

From time to time, I need to create an input control which allows multiple rows of input, the type of input and number of columns could be different in each case but typically it would be represent a database table being edited, so it should also be possible to store an id value within each row. It should also be possible for the user to add new rows to the input if they have more data to input.
I have tried a couple of approaches to this but they are all very long winded for what seems like such an obvious and common scenario. I'm thinking that there must be a simple way to do this that I have missed.
The way that I usually solve this is by using a list view, enter all of the input controls required within the item template and use a html table for formatting, with each item being a row of the table. If there is existing data to be edited, I would load the data from the database, add a blank object to the results and bind it to the list view. If there is no existing data, I would create a collection with a blank record in it and bind it to the list view. I add a button for adding a new row. When clicked, this button retrieves all of the existing data from the list view by iterating all of the listview items and populating the data into a collection, which I would then add a blank object to and rebind the listview with the results. When saving, I would retrieve the results by iterating the listview items again and using a hidden field within each row to store the record id. This would determine whether a new record was added or an existing record was edited.
This approach works for me but I'm sure there must be simpler ways to achieve this. Looking forward to seeing how other people solve this.
For web forms, use a GridView. You can set its properties to allow editing, deleting, new rows, sorting, and paging.
Check out the MSDN example here that shows how to use it. If you bind a datasource to it, it will figure out the columns and adjust dynamically then, or you can predefine the columns you want for more customability.
You are talking about bulk insert/update. You could use XML insertion/updation for this purpose. Get all the data to a DataSet ds variable. Then use ds.GetXml() method to convert the dataset to XML string. Pass this to an XML parameter into SQL server which has the datatype 'XML'
INSERT INTO YOURTABLE
SELECT
V.VOI.value('(.)[1]', 'int')
FROM
#Input.nodes('/DATASETNAME/DATATABLENAME/') V(VOI)
Use this link to learn more
You need dynamic table with Add, View,Edit and delete operations on each data.
I would suggest using DataTable Jquery component. there are tons of examples on Data operations, and you can plug this with any Server technology including ASP.net
http://editor.datatables.net/

How do I create a gridview with an empty column to allow user to enter a value which will then be used with the rest of the columns to update a table

How do I create a gridview with an empty column to allow user to enter a value which will then be used with the rest of the columns to update a table. I was hoping to allow user to go down a gridview change a number of values and upon button push have the new table displayed to review changes before submitting to a database. The gridview is dynamically bound from an ilist which makes it difficult as the page lifecycle disables me pulling the items out using .findcontrol. And Neither can I use viewstate("newtable") = ilist method as this will not account for the editable field. Help!!!
J
Firstly, your question is not clear if you put your code sample it will be more. if you work with entity framework for database op., datagridview already allow updates. yourentityobject.SaveChanges() is enough.

How to create a gridview in asp.net having features similar to the datagridview in Windows forms

I understand that the Gridview of asp.net and windows forms are different, but my requirement is that I have a registration form where in a user registers his vehicles.
A single user can have many vehicles, so I want to provide a Gridview with the column names specified and allow users to enter the values in the rows. Depending on the number of vehicles, the user keeps adding them. This task is actually easy in the Windows forms since the grid is in such a way that an empty row is provided by default and we can type in the cells. Finally we can read the DataGridview by each row and save the results to database.
I am looking for this feature in the Asp.net either with the Gridview or any other data control. This looks pretty complex with the asp.net gridview since ill need to work with the edit button, update button for doing this every time and also a add new row button every time.
Is there any other way I can do this task without complexity. I am fairly new to asp.net.
If you want to add a new row then what you can do it add a footer row to the gridview. and everytime the user enter information in the footer row you can bind it to gridview and when the page is reloaded you will always have the footer row. This way user can register as many cars they want.

Creating and accessing control values dynamically

I have a DropDownList that gets populated from SQL on page load.
A user will select an item from the list and click a button.
Based upon the item selected, I need to fetch data from SQL. The results are then used to dynamically create a table with textboxes (for user input).
After filling out some textboxes, the user will click another button. I will need to loop through the dynamically created table and do some data processing with the values entered in the textboxes.
I'm having a hard time knowing when to create the dynamic table. If on Page_Init, how do I get the DropDownList selected value to pass to SQL? If on Page_Load, how am I going to re-create the the table with the user-entered values?
This is in C# .NET Framework 4.0 using Webforms
Found the solution.
I'm using Request.Form[ddl.UniqueID] to get the DDL's selected value.

Editable, growable DataGrid that retains values on postback and updates underlying DataTable

I'm trying to create an ASP.NET/C# page that allows the user to edit a table of data, add rows to it, and save the data back to the database. For the table of data, I'm using a DataGrid whose cells contain TextBoxes or CheckBoxes. I have a button for adding rows (which works) and a button for saving the data. However, I'm quite stuck on two things:
The TextBoxes and CheckBoxes should retain their values on postback. So if the user edits a TextBox and clicks the button to add more rows, the edits should be retained when the page reloads. However, the edits should not be saved to the database at this point.
When the user clicks the save button, or anytime before, the DataTable underlying the DataGrid needs to be updated with the values of the TextBoxes and CheckBoxes so that the DataTable can be sent to the database. I have a method that does this, but I can't figure out when to call it.
Any help getting this to work, or suggestions of alternative user interfaces that would behave similarly, would be appreciated.
Take a look at the ASP.NET GridView control. It's newer than the DataGrid and has editing features, editing events, etc built in.
Check out the following website:
http://highoncoding.com/Categories/7_GridView_Control.aspx
There are tons of articles on the above link about the GridView control.

Categories

Resources