Dynamic Controls question

J

J055

Hi

I have a FormView, Panel and ObjectDataSource control. I'm trying to change
the Panel.Visible property to true based on a value in the ReturnValue
object in the ObjectDataSource.Selected event. I can't find the Panel
control from the inside the Selected event method even though the control is
available earlier in the page life cycle, e.g.

protected void odsAccount_Selected(object sender,
ObjectDataSourceStatusEventArgs e)
{
if (e.ReturnValue != null)
{
ScsDataSet.AccountsDataTable accTbl =
(ScsDataSet.AccountsDataTable)e.ReturnValue;
if ((AccountTypes)accTbl[0].AccountType == AccountTypes.Student)
{
Panel panel = (Panel)FindControl("panParents");
// Error occurs here - Object reference not set to an instance of an
object.
panel.Visible = true;
}
}
}

I can access the panParents control from the Page_Load which occurs first.
What am I doing wrong.

Thanks
Andrew
 
M

Mark Fitzpatrick

Try calling the FindControl method from the control that is the direct
container of the Panel. If the Panel is in the form view and the form view
is called formView you would do it as Panel panel =
(Panel)formView.FindControl("panParents");

Have you tried accessing panParents directly though? If it's declared
somewhere else on the page and isn't a child of a control such as the form
view or grid, you should be able to access it directly without using the
FindControl method. If the panParents is generated dynamically it isn't
necessarily created yet, hence FindControl won't work since there's
technically nothing to find yet. In this case you could create a global
variable that could be set to the panel and accessed throughout the page.
 
J

J055

Hi Mark

I tried this originally:

panel = (Panel)fvAccount.FindControl("panParents");

fvAccount is the FormView control. The Panel is inside the EditItemTemplate.

I don't normally have problems with this type usage so thought it might be
something to do with positioning the code inside the ObjectDataSource
Selected event.

I'm not sure how to debug it either since the control can be found earlier
in the page life cycle.

Thanks
Andrew
 
J

J055

Hi

I think I have a more fundamental problem - I have no idea how to find child
controls within the FormView.

this doesn't show any controls anywhere in the life cycle.

Trace.Warn(fvAccount.HasControls().ToString());

Can't find anything which helps me in the forums.

Thanks
Andrew
 
J

J055

Hi

It looks like you can only find child controls of the FormView in the
ItemCreated event so now I have this:

private ScsDataSet.AccountsDataTable accountsTbl;

protected void odsAccount_Selected(object sender,
ObjectDataSourceStatusEventArgs e)
{
if (e.ReturnValue != null)
{
accountsTbl = (ScsDataSet.AccountsDataTable)e.ReturnValue;
}
}

protected void fvAccount_ItemCreated(object sender, EventArgs e)
{
// ItemCreated fires twice on postback (before and after
ObjectDataSource Selected)
// so test for null object first
if (accountsTbl == null)
return;
else if (fvAccount.CurrentMode == FormViewMode.Edit)
{
Panel panel;

// select the areas to display for AccountType
switch ((AccountTypes)accountsTbl[0].AccountType)
{
case AccountTypes.Student:
panel = (Panel)fvAccount.FindControl("panParents");
panel.Visible = true;
panel = (Panel)fvAccount.FindControl("panGroupsSets");
panel.Visible = true;
break;
default:
break;
}
}
}

The problem is that on PostBack the ItemCreated event fires twice, before
and after the
ObjectDataSource Selected event. I have to check for null first. This all
seems rather convoluted so I wonder if I'm taking the best approach here.

I'd really like to have a good consistent way of working with web controls
and events. I often find that the code need to do what seems very straight
forward initially becomes a very large amount of code. Comments please.

Thanks
Andrew
 
L

Luke Zhang [MSFT]

Hell Andrew,

The child controls of a Form View is not the controls we put in its item
template, it is first a "ChildTable", then some "FormViewRow", and then
"TableCell". The panel control is child control of "TableCell". please
refer following code:

FormViewRow fr = (FormViewRow)FormView1.Controls[0].Controls[1];

TableCell tc = fr.Cells[0];

foreach (System.Web.UI.Control c in tc.Controls)
Response.Write(c.ToString()+"<br>" );


Sincerely,

Luke Zhang

Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
N

Ned Balzer

Hi

I don't know if this question is related to this thread, but I have a
perplexing problem. I am trying to reference a panel inside a
formview. I have code in the page load that looks like this:

If Not Page.IsPostBack Then
FormView1.ChangeMode(FormViewMode.Edit)
Dim multiDeptInt As Int32 = [some external condition that evaluates to
an int]
Dim multiDeptPnl2 as Panel =
CType(articleFormView.FindControl("pnlMultidept2"), Panel)
If multiDeptInt < 2 Then 'single
multiDeptPnl2.Visible = False
Else
multiDeptPnl2.Visible = True
End If
articleFormView.ChangeMode(FormViewMode.ReadOnly)
Dim multiDeptPnl1 as Panel = CType(tc.FindControl("pnlMultidept1"),
Panel)
If multiDeptInt < 2 Then 'single
multiDeptPnl1.Visible = False
Else
multiDeptPnl1.Visible = True
End If
End If

pnlMultidept2 is a control that exists only in the EditItemTemplate of
the formview; pnlMultiDept1 exists only in the ItemTemplate. And as I
mentioned above, the multiDeptInt flag is set externally.

The problem is this: the code has no trouble finding the pnlMultiDept2,
but it can't find the pnlMultiDept1 -- the code crashes on the
subsequent call to set the multDeptPnl1.Visible attribute.

Is there something fundamentally different between the ItemTemplate and
the EditItemTemplate where you have to control the behavior of
contained controls differently? Or maybe the ChangeMode isn't
happening?

I tried your advice of referencing the panel as contained by a table
cell instead of by the formview (although again, i don't understand why
it would be different for itemtemplate than for edititemtemplate):

If Not Page.IsPostBack Then
....
articleFormView.ChangeMode(FormViewMode.ReadOnly)
Dim fr as FormViewRow = Ctype(articleFormView.Controls(0).Controls(1),
FormViewRow)
Dim tc as TableCell = fr.Cells(0)
Dim multiDeptPnl1 as Panel = CType(tc.FindControl("pnlMultidept1"),
Panel)
If multiDeptInt < 2 Then 'single
multiDeptPnl1.Visible = False
Else
multiDeptPnl1.Visible = True
End If
End If

but it didn't make any difference.

TIA.

-- Ned Balzer
Hell Andrew,

The child controls of a Form View is not the controls we put in its item
template, it is first a "ChildTable", then some "FormViewRow", and then
"TableCell". The panel control is child control of "TableCell". please
refer following code:

FormViewRow fr = (FormViewRow)FormView1.Controls[0].Controls[1];

TableCell tc = fr.Cells[0];

foreach (System.Web.UI.Control c in tc.Controls)
Response.Write(c.ToString()+"<br>" );


Sincerely,

Luke Zhang

Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top