Trouble creating a multiple Row Table from Server Side Code
I am trying to create a table dynamically in my code behind. My problem is I have a count of how many controls I want added to each TableRow after which I want to add that TableRow to the table and then start a new row. Here is the code I have no far but it just adds one row and does not move any of the controls to a seperate new row.
public void FillTable(string DB, int? appID, string function, int? rID, string UserId, int ControlsperRow)
{
OneEvaDataContext datacontext = new OneEvaDataContext();
var results = datacontext.sp_Build_Menu_Field_Names(DB, appID, function, rID);
int controlCount = 0;
TableRow tr = new TableRow();
foreach (sp_Build_Menu_Field_NamesResult result in results)
{
TableCell tcLabel = new TableCell();
TableCell TcControl = new TableCell();
Control control = new Control();
control = DynamicControlReturn.ReturnControl(result.Param_Name, result.Build_Options_Type_NAME, UserId);
control.ID = "control" + controlCount.ToString();
_controlClientID开发者_如何学GoList.Add(control.ClientID);
tcLabel.Text = result.Param_Label;
TcControl.Controls.Add(control);
tr.Cells.Add(tcLabel);
tr.Cells.Add(TcControl);
controlCount = controlCount + Convert.ToInt32(result.TD_Len);
if (controlCount == ControlsperRow)
{
base.Controls.Add(tr);
controlCount = 0;
}
}
}
I would have thought that doing the .Add only when I am finished building that row would work but I guess since I'm using the same TableRow "tr" It is only putting in just one row.
EDIT: in the is context base is my class which looks like this:
public class Dynamic_Search_Table : System.Web.UI.WebControls.Table
{
Move
TableRow tr = new TableRow();
into the foreach, and add
this.Add(tr);
at the end of the foreach.
Edit: Change
if (controlCount == ControlsperRow)
{
base.Controls.Add(tr);
controlCount = 0;
}
To
if (controlCount == ControlsperRow)
{
base.Controls.Add(tr);
controlCount = 0;
tr = new TableRow();
}
精彩评论