<div>I have a C# (C# 2010 with SQL 2008) web application and I building an edit screen but I dont know how many records are in the table, so I need to dynamically build the text box and populate it with data... below is the sample loop that I use to retrieve
the data...I'm just doing a response.write for now, but I need to load the values into a text box, then if the user submits this, I need to update the table.</div> <div>I haven't really ever created input boxes on the fly before...</div> <div>
foreach (DataRow dr1 in ds.Tables[0].Rows)
{
Response.Write(dr1["description"].ToString());
Response.Write(dr1["opens_at"].ToString());
Response.Write(dr1["closes_at"].ToString());
}
My examaple above is only writing the results to a screen, but I need them loaded into a text box, then allow the user to review and update the values and then submit back to the database.
The response.write was just to help explain the 3 values (for one record), but I need to have the user update the values as well.
So I need to load the values in a text box, then on postback, submit the values/changes back to the database.
That looks to potentially do what I am needing, but I dont want paging....my example has 3 records, I want all 3 records shown at the same time, I do not want paging...is this possible?
Set an element to run at server so you can access it from your codebehind
<div id="content" runat="server"></div>
In your codebehind, add the html controls like so
for (int i = 0; i < 15; i++) //for loop to demonstrate your data table rows
content.Controls.Add(new HtmlInputText()
{
Value = string.Format("Description {0}", i),
Name = string.Format("Value{0}", i), // Make sure Name and ID both match for the form to pick it up
ID = string.Format("Value{0}", i)
});
To read your submitted values, on a callback function read the Request.Form NameValueCollection
var newValue = Request.Form["Value1"];
and this is what your html source looks like for that code
That looks to potentially do what I am needing, but I dont want paging....my example has 3 records, I want all 3 records shown at the same time, I do not want paging...is this possible?
Paging is an option that you turn on, just set the AllowPaging="true" to false:
carloslobos
0 Points
34 Posts
Creating textboxes/dropdowns dynamically based on records in table
Nov 20, 2012 01:15 PM|LINK
foreach (DataRow dr1 in ds.Tables[0].Rows)
{
Response.Write(dr1["description"].ToString());
Response.Write(dr1["opens_at"].ToString());
Response.Write(dr1["closes_at"].ToString());
}
My examaple above is only writing the results to a screen, but I need them loaded into a text box, then allow the user to review and update the values and then submit back to the database.
The response.write was just to help explain the 3 values (for one record), but I need to have the user update the values as well.
So I need to load the values in a text box, then on postback, submit the values/changes back to the database.
</div>rtpHarry
All-Star
56620 Points
8958 Posts
Re: Creating textboxes/dropdowns dynamically based on records in table
Nov 20, 2012 01:30 PM|LINK
Hi
It sounds like you need to use a DataBound control such as a DetailsView:
You shouldn't ever use Response.Write, there is always a better way to do things.
carloslobos
0 Points
34 Posts
Re: Creating textboxes/dropdowns dynamically based on records in table
Nov 20, 2012 03:12 PM|LINK
That looks to potentially do what I am needing, but I dont want paging....my example has 3 records, I want all 3 records shown at the same time, I do not want paging...is this possible?
medelbrock
Member
627 Points
133 Posts
Re: Creating textboxes/dropdowns dynamically based on records in table
Nov 20, 2012 08:52 PM|LINK
You can accomplish it by adding System.Web.UI.HtmlControls
Set an element to run at server so you can access it from your codebehind
In your codebehind, add the html controls like so
for (int i = 0; i < 15; i++) //for loop to demonstrate your data table rows content.Controls.Add(new HtmlInputText() { Value = string.Format("Description {0}", i), Name = string.Format("Value{0}", i), // Make sure Name and ID both match for the form to pick it up ID = string.Format("Value{0}", i) });To read your submitted values, on a callback function read the Request.Form NameValueCollection
and this is what your html source looks like for that code
oned_gk
All-Star
30991 Points
6344 Posts
Re: Creating textboxes/dropdowns dynamically based on records in table
Nov 21, 2012 01:02 AM|LINK
try using gridview and place the textboxes inside templatefield. Bind the tb text to db fields.
rtpHarry
All-Star
56620 Points
8958 Posts
Re: Creating textboxes/dropdowns dynamically based on records in table
Nov 21, 2012 09:09 AM|LINK
Paging is an option that you turn on, just set the AllowPaging="true" to false:
These controls are very flexible.
carloslobos
0 Points
34 Posts
Re: Creating textboxes/dropdowns dynamically based on records in table
Nov 21, 2012 01:10 PM|LINK
If the paging is turned off, I still only see the first record and the other 2 records are still not shown or accessible.
If paging is off, i was expecting to see all records shown on load of the page?
rtpHarry
All-Star
56620 Points
8958 Posts
Re: Creating textboxes/dropdowns dynamically based on records in table
Nov 22, 2012 11:15 AM|LINK
Sorry I didn't read that right the first time through. DetailsView is only for showing on record at a time.
There is GridView and ListView for showing multiple records at a time but it doesn't support editing multiple records out of the box.
There is a pretty handy tutorial that I have used before in my projects that shows you how to work around this though:
carloslobos
0 Points
34 Posts
Re: Creating textboxes/dropdowns dynamically based on records in table
Nov 22, 2012 08:16 PM|LINK
Thanks for the post and I am glad you are understanding the complexity is a bit larger...
As for the link you sent me, I can't get the source code and I am getting 'virus' type errrs when I load that site...
Any suggestiongs
rtpHarry
All-Star
56620 Points
8958 Posts
Re: Creating textboxes/dropdowns dynamically based on records in table
Nov 22, 2012 09:48 PM|LINK
You are right, it seems this site has crumbled. The second domain has not been renewed and the downloads are not available.
I couldn't find a copy of it in my archives either.
Guess you will have to find a different solutions, sorry.
(I have emailed Matt to see if he can help so I'll update here if he gets back to me)