Doug Schmidt
asked on
C# findcontrol
I am dynamically creating the row and cell structure within an asp:Table and need to access the data in the table. The dynamic creation is because there will be a variable amount of data in the table. The table creation is fine but I am not able to programmatically find the appropriate controls to access the data.
html code
row and cell creation code
access data code
Thanks for any help.
html code
<table id="tblGoals" class="GoalsTable" runat="server">
<tr>
<td colspan="4">
<h3 class="Center">Goals</h3>
</td>
</tr>
<tr>
<td colspan="4">
<asp:Table ID="tblGoalDetails" CssClass="GoalDetails" runat="server" GridLines="Horizontal">
</asp:Table>
</td>
</tr>
</table>
row and cell creation code
//********************************************************************************
// Name: LoadGoalsTable
// Abstract: Loads goals for specific exercise in workout
//********************************************************************************
private void LoadGoalsTable(int intExerciseID)
{
int intGoalCounter = 0;
string strGoalType;
//look up goals for current exercize
using (var myEntities = new dbWorkOutEntities())
{
var CurrentGoals = (from goals in myEntities.tPeopleWorkOutExerciseGoals
where goals.intPersonID == f_intPersonId
&& goals.intWorkOutID == f_intWorkOutId
&& goals.intExcersizeID == intExerciseID
orderby goals.intPeopleWorkOutExerciseGoalID
select goals);
hdnNumberOfGoals.Value = Convert.ToString(CurrentGoals.Count());
//If goals exist
if (CurrentGoals != null)
{
BuildHeader();
//build goal rows
foreach (var Goal in CurrentGoals)
{
intGoalCounter += 1;
strGoalType = LookUpGoalType(Goal.intGoalTypeID);
TableRow tblRow = new TableRow();
//build cells and add controls
//PersonWorkOutGoalID with label
TableCell celGoalID = new TableCell();
celGoalID.CssClass = "Center";
Label lblGoalID = new Label();
lblGoalID.ID = "lblGoalID" + intGoalCounter.ToString();
lblGoalID.Text = Goal.intPeopleWorkOutExerciseGoalID.ToString();
celGoalID.Controls.Add(lblGoalID);
tblRow.Cells.Add(celGoalID);
//GoalType - with hidden field fot GoalTypeID
TableCell celGoalType = new TableCell();
celGoalType.CssClass = "Center";
celGoalType.Text = strGoalType;
HiddenField hdnGoalTypeID = new HiddenField();
hdnGoalTypeID.ID = "hdnGoalTypeID" + intGoalCounter.ToString();
hdnGoalTypeID.Value = Goal.intGoalTypeID.ToString();
celGoalID.Controls.Add(hdnGoalTypeID);
tblRow.Cells.Add(celGoalType);
//Goal with TextBox
TableCell celGoal = new TableCell();
TextBox txtGoal = new TextBox();
txtGoal.CssClass = "TableBoxes";
txtGoal.Text = Goal.intExcersizeGoal.ToString();
txtGoal.ID = "txtGoal" + intGoalCounter.ToString();
celGoal.Controls.Add(txtGoal);
tblRow.Cells.Add(celGoal);
//Actual with TextBox
TableCell celActual = new TableCell();
TextBox txtActual = new TextBox();
txtActual.CssClass = "TableBoxes";
txtActual.ID = "txtActual" + intGoalCounter.ToString();
celActual.Controls.Add(txtActual);
tblRow.Cells.Add(celActual);
tblGoalDetails.Rows.Add(tblRow);
}
}
}
}
//********************************************************************************
// Name: BuildHeader
// Abstract: Builds header row for goal detail table
//********************************************************************************
private void BuildHeader()
{
//Build column headers
TableHeaderRow hdrRow = new TableHeaderRow();
//build header cells
TableHeaderCell hdrCell1 = new TableHeaderCell();
hdrCell1.Text = "Goal #";
TableHeaderCell hdrCell2 = new TableHeaderCell();
hdrCell2.Text = "Goal Type";
TableHeaderCell hdrCell3 = new TableHeaderCell();
hdrCell3.Text = "Goal";
TableHeaderCell hdrCell4 = new TableHeaderCell();
hdrCell4.Text = "Actual";
//add header row
hdrRow.Cells.Add(hdrCell1);
hdrRow.Cells.Add(hdrCell2);
hdrRow.Cells.Add(hdrCell3);
hdrRow.Cells.Add(hdrCell4);
tblGoalDetails.Rows.Add(hdrRow);
}
access data code
//********************************************************************************
// Name: SaveResults
// Abstract: Save results as work out is proceeding
//********************************************************************************
private void SaveResults(int intNumberOfGoals)
{
int intCounter = 1;
string strHiddenGoalTypeID;
int intGoalTypeID;
string strGoalID;
int intGoalID;
string strGoal;
int intGoal;
string strActualID;
int intActual;
while (intCounter <= intNumberOfGoals)
{
//find HiddenField for and get GoalId
strHiddenGoalTypeID = "hdnGoalTypeID" + intCounter.ToString(); // define control ID
HiddenField hdnGoalTypeID = (HiddenField)tblGoalDetails.FindControl(strHiddenGoalTypeID); //find control
intGoalTypeID = Convert.ToInt32(hdnGoalTypeID.Value); //get data
//find lable and get value for goalid
strGoalID = "lblGoalID" + intCounter.ToString();
Label lblGoalID = (Label)tblGoalDetails.FindControl(strGoalID);
intGoalID = Convert.ToInt32(lblGoalID.Text);
//find textbox and get value for goal
strGoal = "txtGoal" + intCounter.ToString();
TextBox txtGoal = (TextBox)tblGoalDetails.FindControl(strGoal);
intActual = Convert.ToInt32(txtGoal.Text);
//find textbox and get value for actual
strActualID = "txtActual" + intCounter.ToString();
TextBox txtActual = (TextBox)tblGoalDetails.FindControl(strActualID);
intGoal = Convert.ToInt32(txtActual.Text);
using (var myEntities = new dbWorkOutEntities())
{
//Add new role
myEntities.tPeopleWorkOutExerciseResults.Add(new tPeopleWorkOutExerciseResult()
{
intPersonID = f_intPersonId,
intWorkOutID = f_intWorkOutId,
intExcersizeID = Convert.ToInt32(hdnExerciseID.Value),
intGoalTypeID = intGoalTypeID,
intPeopleWorkOutExerciseGoalID = intGoalID,
intExerciseGoal = intGoal,
intExcersizeResult = intActual,
dtmDatePerformed = DateTime.Now
});
//make perssistent
myEntities.SaveChanges();
}
intCounter += 1;
}
}
Thanks for any help.
Also instead of this use div or some thing to align, if not you need to have multiple table loop
<table id="tblGoals" class="GoalsTable" runat="server">
<tr>
<td colspan="4">
<h3 class="Center">Goals</h3>
</td>
</tr>
<tr>
<td colspan="4">
<asp:Table ID="tblGoalDetails" CssClass="GoalDetails" runat="server" GridLines="Horizontal">
</asp:Table>
</td>
</tr>
</table>
The table creation is fine but I am not able to programmatically find the appropriate controls to access the data.
did you debug your code one line by line?
which line does not work as expected?
whats the issue?
ASKER
Thanks Dorababu.
I've removed the extraneous table. I've also updated my code to contain nested foreach loops on rows, cells, and controls. In each cell I check cor control type then get the data from the control. The problem is that the loop is skipped without even starting. Can you help with what I'm doing wrong?
html:
c# trying to access controls in table tbleGoalDetails:
Thanks,
Doug
I've removed the extraneous table. I've also updated my code to contain nested foreach loops on rows, cells, and controls. In each cell I check cor control type then get the data from the control. The problem is that the loop is skipped without even starting. Can you help with what I'm doing wrong?
html:
<h3 class="Center"><asp:Label ID="lblGoals" runat="server" Text="Goals"></asp:Label></h3>
<asp:Table ID="tblGoalDetails" CssClass="GoalsTable" runat="server" GridLines="Horizontal">
</asp:Table>
c# trying to access controls in table tbleGoalDetails:
//********************************************************************************
// Name: SaveResults
// Abstract: Save results as work out is proceeding
//********************************************************************************
private void SaveResults(int intNumberOfGoals)
{
int intGoalTypeID = 4;
int intGoalID = 1;
int intGoal = 50;
int intActual = 50;
foreach (TableRow row in tblGoalDetails.Rows)
{
foreach (TableCell cell in row.Cells)
{
foreach (Control control in cell.Controls)
{
if (control is HiddenField)
{
intGoalID = Convert.ToInt32(((HiddenField)control).Value);
}
else if (control is Label)
{
intGoalID = Convert.ToInt32(((Label)control).Text);
}
else if (control is TextBox)
{
if (control.ID.Contains("txtGoal"))
{
intGoalID = Convert.ToInt32(((TextBox)control).Text);
}
if (control.ID.Contains("txtActual"))
{
intGoalID = Convert.ToInt32(((TextBox)control).Text);
}
}
using (var myEntities = new dbWorkOutEntities())
{
//Add new role
myEntities.tPeopleWorkOutExerciseResults.Add(new tPeopleWorkOutExerciseResult()
{
intPersonID = f_intPersonId,
intWorkOutID = f_intWorkOutId,
intExcersizeID = Convert.ToInt32(hdnExerciseID.Value),
intGoalTypeID = intGoalTypeID,
intPeopleWorkOutExerciseGoalID = intGoalID,
intExerciseGoal = intGoal,
intExcersizeResult = intActual,
dtmDatePerformed = DateTime.Now
});
//make perssistent
myEntities.SaveChanges();
}
}
}
}
}
Thanks,
Doug
ASKER
Huseyin,
I just posted updated code based on what Dorababu said. The line of code where it breaks down is line 13. It doesn't even enter into the loop.
Thanks for the help.
Doug
I just posted updated code based on what Dorababu said. The line of code where it breaks down is line 13. It doesn't even enter into the loop.
Thanks for the help.
Doug
put a breakpoint on first line of this code, and check the value of
tblGoalDetails.Rows
tblGoalDetails.Rows.count
also how do you call this? It should be called after page is loaded, not before load or page init...
tblGoalDetails.Rows
tblGoalDetails.Rows.count
also how do you call this? It should be called after page is loaded, not before load or page init...
ASKER
it is called from a button click event.
The value of tblGoalDetails.Rows is 0 which is of course why it skips over the entire loop and never even gets to the tblGoalDetails.Rows.Cells. On the page that I'm looking at in the browser, during debug, there are actually 3 rows in the table.
The rows and cells are added to the table programmatically if that effects anything.
The value of tblGoalDetails.Rows is 0 which is of course why it skips over the entire loop and never even gets to the tblGoalDetails.Rows.Cells.
The rows and cells are added to the table programmatically if that effects anything.
call this code on page load to see if it does something different...
ASKER
Calling from page load does give me the correct row and cell count but I need to get that to happen after the user has entered information and clicked the next button
on button click set a variable... here is a simple logic
private saveCLicked as boolean = false
buttonSave_Click() {
saveClicked = true
}
saveData() { move your save code here }
page_load(){
if page.isPostBack {
if saveClicked saveData();
}
I am not sure whats happening, since I dont see your full code for that button and button click...
ASKER
it only gives ne the correct counts if page is not post back
did you somehow disabled viewstate?
nothing here makes sense to me :)
nothing here makes sense to me :)
ASKER
actually have the view state enabled. I think that because it's a dynamically created table the post back destroys the data before the click event code is run. I need to create a javascript script to save the data that will run before the postback.
create your table on page_load
are you creating it on preRender or something?
even dynamically creating it should preserve...
are you creating it on preRender or something?
even dynamically creating it should preserve...
ASKER
creating on page load was wondering if creating it earlier like on init might help. seems like this is a recurring issue with dynamically created tables according to my research on the web
create it on pageinit but only on not postback and see if they persist...
also, why you are doing table dynamically?
just create a repeater and create a dataview/datatable and bind it...
I never needed to use such thing in 20 years :)
just create a repeater and create a dataview/datatable and bind it...
I never needed to use such thing in 20 years :)
if you are writing too much code, you are doing something wrong :)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That looks like the solution I've been looking for. Probably won't have a chance to get to it until Sunday or possible even Monday.
Thanks
Thanks
ASKER
Worked great! I hadn't worked with a repeater in the past. You can believe that it is firmly established in my tools.
Thank you
Thank you
https://www.experts-exchange.com/questions/29033000/Check-if-Checkbox-selected.html?anchor=a42178189¬ificationFollowed=190876037&anchorAnswerId=42178189#a42178189