I need a table that's 3 columns and 10 rows. How do I put a textbox in each of the cells? I know I can do it with 10 textboxes named something like textbox1, textbox2,...,textbox10. However I want to get the data in a loop from the three text boxes commit the data to a database. In other words make it more efficient than just hard coding for each text box and calling the update command 10 times?
1 Answers
For straight HTML, a start of a solution at end of post..
Note, you mention asp, C# etc etc.. I have never touched ASP.NET, but the following is how this would be accomplished in C# using Windows Forms. I'm not sure exactly where you are trying to implement the text box grid, but this example should be transferrable to a number of platforms (ASP.Net has a datagridview, but I think implementation is a bit different) given a bit of elbow grease. There are numerous tutorials online for setting up a datagridview in asp, so I will leave that part to you and the google.
I would use a datagrid view. There's a bit of setup but in a nutshell, presuming the table should be completely blank at the beginning of the routine:
datagridview1.DataSource = null;
datagridview1.Columns.Clear(); //Just make sure things are blank.
datagridview1.Columns.Add("Column1","Column1");
datagridview1.Columns.Add("Column2","Column2");
datagridview1.Columns.Add("Column3","Column3");
datagridview1.Rows.Clear();
for(int i = 0;i<10;i++)
{
datagridview1.Rows.Add()
}
datagridview1.EditMode = DataGridViewEditMode.EditOnKeystroke;
When you insert the DataGridView object, you need to make sure to disable user ability to add rows so that an 11th row doesn't appear.
I would probably pre-fill each cell with a "" just to make sure you don't have to deal with null values. If this is done, then you can reference each cell by the following:
for(int i = 0;i<datagridview1.Rows.Count;i++)
{
string val = datagridview1.Rows[rowIndex].Cells[<Column Index, or Name>].Value.ToString();
}
Stack Overflow discussion of making a table editable
I tried the following and am getting a table of textboxes which are editable
<body>
<table>
<tr>
<th>Month</th>
<th>Savings</th>
<th>SomethingElse</th>
</tr>
<tr>
<td><input type="text"></td>
<td><input type="text"></td>
<td><input type="text"></td>
</tr>
</tr>
<tr>
<td><input type="text"></td>
<td><input type="text"></td>
<td><input type="text"></td>
</tr>
</tr>
</table>
</body>
Next step would be to determine how to access that table. I presume there's a method for naming html elements that you could use to grab the data. I'm not an HTML guy, so.. Just trying to home in on a way forward for you.
Additional links referencing reading HTML tables from C# and or ASP.NET.. it appears that there is also an ASP:Table that could be used..
-
Thanks for your answer. However I'm working with a html and unless I'm missing something I can't use datagridviews. I've used them quite a bit with windows forms but I didn't think they were accessible in my current context. – coffy43 Mar 03 '16 at 18:35
-
you may be right. I saw the original question by filtering on c# tag, so was thinking it was a C# / .NET question. I added the first paragraph and left the post thinking it may give you a direction to run. If using C# / ASP.NET it appears that there are data grid views. If this is being written in straight HTML, not so much. – Nate M. Mar 03 '16 at 18:38
-
@coffy43 for other prospective posters, you may want to add some detail to the question specifically a block of code / html that you currently have for implementing the solution, or a start to that solution. – Nate M. Mar 03 '16 at 18:40
-
Well that's my problem other than what I have described (which will work but is a mess). I have nothing to go off of. I've tried a data grid. Thinking it would be the same as a data grid view. However they are not. – coffy43 Mar 03 '16 at 18:44
-
I forgot to keep checking back here and I did what you did. Then using Request.Form["textbox"+i] in a loop I was able to get it working. Thank you. – coffy43 Mar 03 '16 at 20:39