Home All Groups Group Topic Archive Search About

Getting value from child control of formview

Author
19 Jan 2006 9:05 PM
timpera2501
Hello,

I'm a bit of a newb, so please forgive me if this is a stupid question.

I have a simple formview that contains only one control (a label control
called "COUNTLabel").  The formview is bound to an ObjectDataSournce that
returns one value named "COUNT" which is assigned to this label control.

In the code behind, I've been trying to programmatically set the "Visible"
property of the formview control based on the value in the "COUNTLabel" label
control that is within the formview.

In the DataBinding event for the formview I have the following code:

if (EmergencyTTs.Row.FindControl("COUNTLabel").Equals(0))
        {
            EmergencyTTs.Visible = false;
        }
        else
        {
            EmergencyTTs.Visible = true;
        }

But the value for "EmergencyTTs.Row.FindControl("COUNTLabel")" is always
null.  What am I doing wrong?

Is there a better/easier way to go about going this?

Author
19 Jan 2006 9:36 PM
Phillip Williams
You could have done it declaratively like this:

<asp:Label Id="COUNTLabel" runat="server" Text='<%# Bind ("COUNTFieldName")
%>'  Visible='<%# Eval("COUNTFieldName").ToString().Equals("0") %>' ></Label>

You could have still done it the way you tried except that the syntax should
be corrected to look like this:
Label lbl=(Label)FormView1.Row.FindControl("COUNTLabel");
//if control is found
if (lbl !=null) lbl.Visible = lbl.Text.Equals("0");

Show quoteHide quote
"timpera2501" wrote:

> Hello,
>
> I'm a bit of a newb, so please forgive me if this is a stupid question.
>
> I have a simple formview that contains only one control (a label control
> called "COUNTLabel").  The formview is bound to an ObjectDataSournce that
> returns one value named "COUNT" which is assigned to this label control.
>
> In the code behind, I've been trying to programmatically set the "Visible"
> property of the formview control based on the value in the "COUNTLabel" label
> control that is within the formview.
>
> In the DataBinding event for the formview I have the following code:
>
> if (EmergencyTTs.Row.FindControl("COUNTLabel").Equals(0))
>         {
>             EmergencyTTs.Visible = false;
>         }
>         else
>         {
>             EmergencyTTs.Visible = true;
>         }
>
> But the value for "EmergencyTTs.Row.FindControl("COUNTLabel")" is always
> null.  What am I doing wrong?
>
> Is there a better/easier way to go about going this?
>
Author
19 Jan 2006 9:47 PM
Phillip Williams
The second alternative below should be used during the FomView.DataBinding or
FromView.DataBound or any later event, but not during handling the page.load,
as I explained, because the Page.Load event fires before the FormView is
populated with the data.

Show quoteHide quote
"Phillip Williams" wrote:

> You could have done it declaratively like this:
>
> <asp:Label Id="COUNTLabel" runat="server" Text='<%# Bind ("COUNTFieldName")
> %>'  Visible='<%# Eval("COUNTFieldName").ToString().Equals("0") %>' ></Label>
>
> You could have still done it the way you tried except that the syntax should
> be corrected to look like this:
> Label lbl=(Label)FormView1.Row.FindControl("COUNTLabel");
> //if control is found
> if (lbl !=null) lbl.Visible = lbl.Text.Equals("0");
>
> --
> HTH,
> Phillip Williams
> http://www.societopia.net
> http://www.webswapp.com
>
>
> "timpera2501" wrote:
>
> > Hello,
> >
> > I'm a bit of a newb, so please forgive me if this is a stupid question.
> >
> > I have a simple formview that contains only one control (a label control
> > called "COUNTLabel").  The formview is bound to an ObjectDataSournce that
> > returns one value named "COUNT" which is assigned to this label control.
> >
> > In the code behind, I've been trying to programmatically set the "Visible"
> > property of the formview control based on the value in the "COUNTLabel" label
> > control that is within the formview.
> >
> > In the DataBinding event for the formview I have the following code:
> >
> > if (EmergencyTTs.Row.FindControl("COUNTLabel").Equals(0))
> >         {
> >             EmergencyTTs.Visible = false;
> >         }
> >         else
> >         {
> >             EmergencyTTs.Visible = true;
> >         }
> >
> > But the value for "EmergencyTTs.Row.FindControl("COUNTLabel")" is always
> > null.  What am I doing wrong?
> >
> > Is there a better/easier way to go about going this?
> >
Author
19 Jan 2006 10:01 PM
timpera2501
Phillip,

Thank you very much for taking the time to respond. =)

I made a method for the Formview Databinding and inserted the following code:

******************
protected void EmergencyTTs_DataBinding(object sender, EventArgs e)
    {
        Label lbl = (Label)EmergencyTTs.FindControl("COUNTLabel");
        //if control is found
        if (lbl != null) EmergencyTTs.Visible = lbl.Text.Equals("0");
    }
******************

However, it still does not hide the formview when the value of the label is
0.  Ideas?


Show quoteHide quote
"Phillip Williams" wrote:

> The second alternative below should be used during the FomView.DataBinding or
> FromView.DataBound or any later event, but not during handling the page.load,
> as I explained, because the Page.Load event fires before the FormView is
> populated with the data.
>
> "Phillip Williams" wrote:
>
> > You could have done it declaratively like this:
> >
> > <asp:Label Id="COUNTLabel" runat="server" Text='<%# Bind ("COUNTFieldName")
> > %>'  Visible='<%# Eval("COUNTFieldName").ToString().Equals("0") %>' ></Label>
> >
> > You could have still done it the way you tried except that the syntax should
> > be corrected to look like this:
> > Label lbl=(Label)FormView1.Row.FindControl("COUNTLabel");
> > //if control is found
> > if (lbl !=null) lbl.Visible = lbl.Text.Equals("0");
> >
> > --
> > HTH,
> > Phillip Williams
> > http://www.societopia.net
> > http://www.webswapp.com
> >
> >
> > "timpera2501" wrote:
> >
> > > Hello,
> > >
> > > I'm a bit of a newb, so please forgive me if this is a stupid question.
> > >
> > > I have a simple formview that contains only one control (a label control
> > > called "COUNTLabel").  The formview is bound to an ObjectDataSournce that
> > > returns one value named "COUNT" which is assigned to this label control.
> > >
> > > In the code behind, I've been trying to programmatically set the "Visible"
> > > property of the formview control based on the value in the "COUNTLabel" label
> > > control that is within the formview.
> > >
> > > In the DataBinding event for the formview I have the following code:
> > >
> > > if (EmergencyTTs.Row.FindControl("COUNTLabel").Equals(0))
> > >         {
> > >             EmergencyTTs.Visible = false;
> > >         }
> > >         else
> > >         {
> > >             EmergencyTTs.Visible = true;
> > >         }
> > >
> > > But the value for "EmergencyTTs.Row.FindControl("COUNTLabel")" is always
> > > null.  What am I doing wrong?
> > >
> > > Is there a better/easier way to go about going this?
> > >
Author
19 Jan 2006 10:33 PM
Phillip Williams
Have you tried the declarative approach? (I tried it on my desktop and the
syntax works find)

Regarding the FormView.DataBinding approach you can set up break points on
the code and step through it to verify that the method is called, then look
at the values of the lbl.Text (maybe it is padded with spaces and therefore
you need to adjust the criteria for the if condition)
Show quoteHide quote
"timpera2501" wrote:

> Phillip,
>
> Thank you very much for taking the time to respond. =)
>
> I made a method for the Formview Databinding and inserted the following code:
>
> ******************
> protected void EmergencyTTs_DataBinding(object sender, EventArgs e)
>     {
>         Label lbl = (Label)EmergencyTTs.FindControl("COUNTLabel");
>         //if control is found
>         if (lbl != null) EmergencyTTs.Visible = lbl.Text.Equals("0");
>     }
> ******************
>
> However, it still does not hide the formview when the value of the label is
> 0.  Ideas?
>
>
> "Phillip Williams" wrote:
>
> > The second alternative below should be used during the FomView.DataBinding or
> > FromView.DataBound or any later event, but not during handling the page.load,
> > as I explained, because the Page.Load event fires before the FormView is
> > populated with the data.
> >
> > "Phillip Williams" wrote:
> >
> > > You could have done it declaratively like this:
> > >
> > > <asp:Label Id="COUNTLabel" runat="server" Text='<%# Bind ("COUNTFieldName")
> > > %>'  Visible='<%# Eval("COUNTFieldName").ToString().Equals("0") %>' ></Label>
> > >
> > > You could have still done it the way you tried except that the syntax should
> > > be corrected to look like this:
> > > Label lbl=(Label)FormView1.Row.FindControl("COUNTLabel");
> > > //if control is found
> > > if (lbl !=null) lbl.Visible = lbl.Text.Equals("0");
> > >
> > > --
> > > HTH,
> > > Phillip Williams
> > > http://www.societopia.net
> > > http://www.webswapp.com
> > >
> > >
> > > "timpera2501" wrote:
> > >
> > > > Hello,
> > > >
> > > > I'm a bit of a newb, so please forgive me if this is a stupid question.
> > > >
> > > > I have a simple formview that contains only one control (a label control
> > > > called "COUNTLabel").  The formview is bound to an ObjectDataSournce that
> > > > returns one value named "COUNT" which is assigned to this label control.
> > > >
> > > > In the code behind, I've been trying to programmatically set the "Visible"
> > > > property of the formview control based on the value in the "COUNTLabel" label
> > > > control that is within the formview.
> > > >
> > > > In the DataBinding event for the formview I have the following code:
> > > >
> > > > if (EmergencyTTs.Row.FindControl("COUNTLabel").Equals(0))
> > > >         {
> > > >             EmergencyTTs.Visible = false;
> > > >         }
> > > >         else
> > > >         {
> > > >             EmergencyTTs.Visible = true;
> > > >         }
> > > >
> > > > But the value for "EmergencyTTs.Row.FindControl("COUNTLabel")" is always
> > > > null.  What am I doing wrong?
> > > >
> > > > Is there a better/easier way to go about going this?
> > > >
Author
20 Jan 2006 1:14 PM
timpera2501
Ah ha!!

Figured it out.  The code works fine in the method, just instead of:

if (lbl != null) EmergencyTTs.Visible = lbl.Text.Equals("0");

Needed this:

if (lbl != null) EmergencyTTs.Visible = !(lbl.Text.Equals("0"));

=)  Thank you sooo much for your help Phillip.  Now I can get on with my life!

-Amanda


Show quoteHide quote
"Phillip Williams" wrote:

> Have you tried the declarative approach? (I tried it on my desktop and the
> syntax works find)
>
> Regarding the FormView.DataBinding approach you can set up break points on
> the code and step through it to verify that the method is called, then look
> at the values of the lbl.Text (maybe it is padded with spaces and therefore
> you need to adjust the criteria for the if condition)
> --
> HTH,
> Phillip Williams
> http://www.societopia.net
> http://www.webswapp.com
>
>
> "timpera2501" wrote:
>
> > Phillip,
> >
> > Thank you very much for taking the time to respond. =)
> >
> > I made a method for the Formview Databinding and inserted the following code:
> >
> > ******************
> > protected void EmergencyTTs_DataBinding(object sender, EventArgs e)
> >     {
> >         Label lbl = (Label)EmergencyTTs.FindControl("COUNTLabel");
> >         //if control is found
> >         if (lbl != null) EmergencyTTs.Visible = lbl.Text.Equals("0");
> >     }
> > ******************
> >
> > However, it still does not hide the formview when the value of the label is
> > 0.  Ideas?
> >
> >
> > "Phillip Williams" wrote:
> >
> > > The second alternative below should be used during the FomView.DataBinding or
> > > FromView.DataBound or any later event, but not during handling the page.load,
> > > as I explained, because the Page.Load event fires before the FormView is
> > > populated with the data.
> > >
> > > "Phillip Williams" wrote:
> > >
> > > > You could have done it declaratively like this:
> > > >
> > > > <asp:Label Id="COUNTLabel" runat="server" Text='<%# Bind ("COUNTFieldName")
> > > > %>'  Visible='<%# Eval("COUNTFieldName").ToString().Equals("0") %>' ></Label>
> > > >
> > > > You could have still done it the way you tried except that the syntax should
> > > > be corrected to look like this:
> > > > Label lbl=(Label)FormView1.Row.FindControl("COUNTLabel");
> > > > //if control is found
> > > > if (lbl !=null) lbl.Visible = lbl.Text.Equals("0");
> > > >
> > > > --
> > > > HTH,
> > > > Phillip Williams
> > > > http://www.societopia.net
> > > > http://www.webswapp.com
> > > >
> > > >
> > > > "timpera2501" wrote:
> > > >
> > > > > Hello,
> > > > >
> > > > > I'm a bit of a newb, so please forgive me if this is a stupid question.
> > > > >
> > > > > I have a simple formview that contains only one control (a label control
> > > > > called "COUNTLabel").  The formview is bound to an ObjectDataSournce that
> > > > > returns one value named "COUNT" which is assigned to this label control.
> > > > >
> > > > > In the code behind, I've been trying to programmatically set the "Visible"
> > > > > property of the formview control based on the value in the "COUNTLabel" label
> > > > > control that is within the formview.
> > > > >
> > > > > In the DataBinding event for the formview I have the following code:
> > > > >
> > > > > if (EmergencyTTs.Row.FindControl("COUNTLabel").Equals(0))
> > > > >         {
> > > > >             EmergencyTTs.Visible = false;
> > > > >         }
> > > > >         else
> > > > >         {
> > > > >             EmergencyTTs.Visible = true;
> > > > >         }
> > > > >
> > > > > But the value for "EmergencyTTs.Row.FindControl("COUNTLabel")" is always
> > > > > null.  What am I doing wrong?
> > > > >
> > > > > Is there a better/easier way to go about going this?
> > > > >
Author
20 Jan 2006 4:08 PM
Phillip Williams
My pleasure.
Show quoteHide quote
"timpera2501" wrote:

> Ah ha!!
>
> Figured it out.  The code works fine in the method, just instead of:
>
> if (lbl != null) EmergencyTTs.Visible = lbl.Text.Equals("0");
>
> Needed this:
>
> if (lbl != null) EmergencyTTs.Visible = !(lbl.Text.Equals("0"));
>
> =)  Thank you sooo much for your help Phillip.  Now I can get on with my life!
>
> -Amanda
>
>
> "Phillip Williams" wrote:
>
> > Have you tried the declarative approach? (I tried it on my desktop and the
> > syntax works find)
> >
> > Regarding the FormView.DataBinding approach you can set up break points on
> > the code and step through it to verify that the method is called, then look
> > at the values of the lbl.Text (maybe it is padded with spaces and therefore
> > you need to adjust the criteria for the if condition)
> > --
> > HTH,
> > Phillip Williams
> > http://www.societopia.net
> > http://www.webswapp.com
> >
> >
> > "timpera2501" wrote:
> >
> > > Phillip,
> > >
> > > Thank you very much for taking the time to respond. =)
> > >
> > > I made a method for the Formview Databinding and inserted the following code:
> > >
> > > ******************
> > > protected void EmergencyTTs_DataBinding(object sender, EventArgs e)
> > >     {
> > >         Label lbl = (Label)EmergencyTTs.FindControl("COUNTLabel");
> > >         //if control is found
> > >         if (lbl != null) EmergencyTTs.Visible = lbl.Text.Equals("0");
> > >     }
> > > ******************
> > >
> > > However, it still does not hide the formview when the value of the label is
> > > 0.  Ideas?
> > >
> > >
> > > "Phillip Williams" wrote:
> > >
> > > > The second alternative below should be used during the FomView.DataBinding or
> > > > FromView.DataBound or any later event, but not during handling the page.load,
> > > > as I explained, because the Page.Load event fires before the FormView is
> > > > populated with the data.
> > > >
> > > > "Phillip Williams" wrote:
> > > >
> > > > > You could have done it declaratively like this:
> > > > >
> > > > > <asp:Label Id="COUNTLabel" runat="server" Text='<%# Bind ("COUNTFieldName")
> > > > > %>'  Visible='<%# Eval("COUNTFieldName").ToString().Equals("0") %>' ></Label>
> > > > >
> > > > > You could have still done it the way you tried except that the syntax should
> > > > > be corrected to look like this:
> > > > > Label lbl=(Label)FormView1.Row.FindControl("COUNTLabel");
> > > > > //if control is found
> > > > > if (lbl !=null) lbl.Visible = lbl.Text.Equals("0");
> > > > >
> > > > > --
> > > > > HTH,
> > > > > Phillip Williams
> > > > > http://www.societopia.net
> > > > > http://www.webswapp.com
> > > > >
> > > > >
> > > > > "timpera2501" wrote:
> > > > >
> > > > > > Hello,
> > > > > >
> > > > > > I'm a bit of a newb, so please forgive me if this is a stupid question.
> > > > > >
> > > > > > I have a simple formview that contains only one control (a label control
> > > > > > called "COUNTLabel").  The formview is bound to an ObjectDataSournce that
> > > > > > returns one value named "COUNT" which is assigned to this label control.
> > > > > >
> > > > > > In the code behind, I've been trying to programmatically set the "Visible"
> > > > > > property of the formview control based on the value in the "COUNTLabel" label
> > > > > > control that is within the formview.
> > > > > >
> > > > > > In the DataBinding event for the formview I have the following code:
> > > > > >
> > > > > > if (EmergencyTTs.Row.FindControl("COUNTLabel").Equals(0))
> > > > > >         {
> > > > > >             EmergencyTTs.Visible = false;
> > > > > >         }
> > > > > >         else
> > > > > >         {
> > > > > >             EmergencyTTs.Visible = true;
> > > > > >         }
> > > > > >
> > > > > > But the value for "EmergencyTTs.Row.FindControl("COUNTLabel")" is always
> > > > > > null.  What am I doing wrong?
> > > > > >
> > > > > > Is there a better/easier way to go about going this?
> > > > > >