473,394 Members | 1,828 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Dynamic Controls question

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

Oct 2 '06 #1
6 3518
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.
--
Hope this helps,
Mark Fitzpatrick
Former Microsoft FrontPage MVP 199?-2006

"J055" <j0**@newsgroups.nospamwrote in message
news:ew*************@TK2MSFTNGP02.phx.gbl...
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

Oct 2 '06 #2
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
Oct 2 '06 #3
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
Oct 2 '06 #4
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
Oct 3 '06 #5
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/subscripti...ult.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/subscripti...t/default.aspx.
==================================================

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

Oct 5 '06 #6
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

Luke Zhang [MSFT] wrote:
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/subscripti...ult.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/subscripti...t/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
Oct 23 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Kelly | last post by:
Hi Everyone I have a web application and all it basically does is ask for a user id and show a questionnaire. To create the questionnaire, I get the list of questions from the database and...
1
by: Scott Schluer | last post by:
Hello, I've got myself a small problem and I'm hoping someone can help. I have a DataList called dlProducts (displays products from a database). Within the <ItemTemplate> container of the...
3
by: Tyler Carver | last post by:
I am trying to use some dynamic controls that are built and then added to tables. The problem that I am having is the timing of when I can populate the controls and have the state remain after a...
3
by: Leo J. Hart IV | last post by:
OK, here's another question for the experts: I am building a multi-step (3 steps actually) form using a panel for each step and hiding/displaying the appropriate panel/panels depending on which...
1
by: npverni | last post by:
I have a fairly complex form that needs to load and maintain the state of several different dynamic user controls. Here is the workflow: 1) A series of editable user controls (each containing...
9
by: Tarscher | last post by:
hi all, I have this seemingly simple problem. I have lost a lot of time on it though. When a user selects a value from a dropdownlist (static control) a dynamic control is generated. I have...
0
by: Scott Roberts | last post by:
I always thought that the viewstate "keys" included the control ID. As long as the control IDs were unique, there shouldn't be any conflicts. Well, it appears that that may not be the case with...
5
by: Hans Kesting | last post by:
Hi, Is there good information about the asp.net page lifecycle in combination with dynamically loaded controls? Or on "how to build dynamic controls"? I keep hitting problems where values are...
13
by: tommymo | last post by:
Hi everyone I'm new to this site and the world of ASP.Net C# programming. I have been learning controls and integrating them with a SQL database. So far I have been able to move along and understand...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.