|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Getting value from child control of formviewI'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? 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? > 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? > > 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? > > > 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? > > > > 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? > > > > > 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? > > > > > >
ITemplate - Dynamic ImageButton in BindLabelColumn for DataList
Embedding CSS with inline code "Protect" or Disable Editing of A Webcontrol SmartNavigation Problem/Bug cache and detailsview, all data controls problem Custom TreeNode for asp.net 2.0 treeview control Multiple onclick Events What is the code for file upload? Menu Control - Templates 2.0 DatagridView and Buttons |
|||||||||||||||||||||||