I created an asp.net application that fills a table with information from a database, choose a value in a drop down list and update the database. For some reason now when I go to the next date and all of the information displays correctly, but the value in the drop down list does not. Because of this, say the first page has 3 rows and the second page has 10. The first 3 rows on the second page will have the same value in the drop down list as the previous page, everything else is changed though. Here is the code used for creating the drop down list on every row:
tc = new TableCell();
tc.BorderWidth = 1;
String rteNum = reader.GetValue(fCount - 1).ToString().TrimStart('R');
//this output displays the correct value
System.Diagnostics.Trace.WriteLine(rteNum.Trim());
ddl = new DropDownList();
ddl.ID = "route" + index;
ddl.Attributes["runat"] = "server";
ddl.Attributes["onblur"] = "refresh()";
ListItem item;
for (int i = 1; i <= 32; i++)
{
item = new ListItem();
item.Text = i.ToString();
item.Value = i.ToString();
if (i.ToString().Equals(rteNum.Trim()))
{
item.Selected = true;
}
ddl.Items.Add(item);
}
list.Add(ddl);
tc.Controls.Add(ddl);
tc.ID = "tblr" + index;
tr.Cells.Add(tc);
tbl1.Rows.Add(tr);
EDIT:
The problem is I need to use the changed values if the user presses a button, however when the data is changed the information has to be dropped. So basically I need to display the information with a drop down list then update the database.
One way is to do it in mark-up like this:
So when you want to set the selected value based on a database value (in response to a button-click, etc., you might use a sub like this in your code-behind:
As an alternative to using a datasoruce control in mark-up, you can use the sub above, but instead…
Here’s an example: http://support.microsoft.com/kb/306574 (CTRL+F for “databind” — it’s down in item 4)