Getting value from child control of formview

Discussion in 'ASP .Net Web Controls' started by timpera2501, Jan 19, 2006.

  1. timpera2501

    timpera2501 Guest

    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?
    timpera2501, Jan 19, 2006
    #1
    1. Advertising

  2. 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?
    >
    Phillip Williams, Jan 19, 2006
    #2
    1. Advertising

  3. 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?
    > >
    Phillip Williams, Jan 19, 2006
    #3
  4. timpera2501

    timpera2501 Guest

    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?
    > > >
    timpera2501, Jan 19, 2006
    #4
  5. 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?
    > > > >
    Phillip Williams, Jan 19, 2006
    #5
  6. timpera2501

    timpera2501 Guest

    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?
    > > > > >
    timpera2501, Jan 20, 2006
    #6
  7. My pleasure.
    --
    HTH,
    Phillip Williams
    http://www.societopia.net
    http://www.webswapp.com


    "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?
    > > > > > >
    Phillip Williams, Jan 20, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. leon
    Replies:
    0
    Views:
    426
  2. Jeff Rodriguez
    Replies:
    23
    Views:
    1,111
    David Schwartz
    Dec 9, 2003
  3. jobs
    Replies:
    0
    Views:
    1,032
  4. Geek

    Formview child control data retrieval

    Geek, Jul 18, 2006, in forum: ASP .Net Web Controls
    Replies:
    3
    Views:
    146
    Alessandro Zifiglio
    Jul 25, 2006
  5. Noel Dolan
    Replies:
    0
    Views:
    227
    Noel Dolan
    Jul 18, 2004
Loading...

Share This Page