Answered by:
Controls within <Div> Inherit their property and i Need a Code Template.

Question
-
User1557998713 posted
I have a <div> that contains multiple lines of information and each line contains a title control label and another label control with the value. Elsewhere on the same .aspx page I have had to create the same <div> with the same controls but adding one more label.
Since I just want lines containing value to be displayed, in codebehind the 2 <div> and their labels become visible = false and later if any of the Value Label controls contain information that control and its title label it becomes visible = true and its <div> too.
Problems:
1.- The first <div> works correctly and only the labels with information are shown. In the second <div> when i activate visible = true, at the moment all the labels it contains are automatically visible = true and I don't want labels to be displayed without information. Cuestion:The <div> are in turn within different controls. Is it possible that they inherit the visible property?
2.- I would like to do something like a template to show the same <div> from that template and not have to duplicate code and in the second <div> add the additional field.The First <Div>: <div ID="MyDiv1" runat="server" class="Options_Container"> <asp:Label ID="Author_Label" runat="server" class="Options_text" Text="Author:"/> <asp:Label ID="Author_Value" runat="server" class="Options_text" Text=""><%=ViewState["Author_Value"]%></asp:Label><br/> </div> The Second <Div>:
<div ID="MyDiv2" runat="server" class="Options_Container"> <asp:Label ID="Author_Label" runat="server" class="Options_text" Text="Author:"/> <asp:Label ID="Author_Value" runat="server" class="Options_text" Text=""><%=ViewState["Author_Value"]%></asp:Label><br/> <asp:Label ID="Edition_Label" runat="server" class="Options_text" Text="Edition:"/> <asp:Label ID="Edition_Value" runat="server" class="Options_text" Text=""><%=ViewState["Edition_Value"]%></asp:Label><br/> </div>Css: .Options_Container { font-family:Century Gothic; font-size:10px; margin-top:5px; line-height:15px; padding:2px; border:10px solid White; } .Options_text { font-family:Century Gothic; font-size:11px; color:#666; line-height:15px; font-weight:bold; }
Thursday, April 30, 2020 12:39 PM
Answers
-
User1535942433 posted
Hi zequion,
zequion
1.- The first <div> works correctly and only the labels with information are shown. In the second <div> when i activate visible = true, at the moment all the labels it contains are automatically visible = true and I don't want labels to be displayed without information. Cuestion:The <div> are in turn within different controls. Is it possible that they inherit the visible property?Accroding to your description,I don't understand your requirment clearly.I'm guessing that you want that you want to have a template ,and then when Author_Value 's value is null,Author_Label and Author_Value of div1 and div2 cann't be displayed.
zequion
2.- I would like to do something like a template to show the same <div> from that template and not have to duplicate code and in the second <div> add the additional field.I suggest you could use UserControl to implement template.
More details,you could refer to below codes:
Asp.net:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="2166472.aspx.cs" Inherits="Demo._2166472" %> <%@ Register TagPrefix="UC" TagName="Author" Src="WebUserControl2.ascx" %> <%@ Register TagPrefix="UC" TagName="cc" Src="WebUserControl3.ascx" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <UC:Author runat="server" ID="author" /> </div> <div> <UC:Author runat="server" ID="author1" /> <UC:cc runat="server" ID="author2" /> </div> </form> </body> </html>
WebControl2.ascx:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl2.ascx.cs" Inherits="Demo.WebUserControl2" %> <asp:Label ID="Author_Label" runat="server" Text="Author:"></asp:Label> <asp:Label ID="Author_Value" runat="server" Text=""></asp:Label>
WebControl2.ascx code-behind:
protected void Page_Load(object sender, EventArgs e) { if (Author_Value.Text == "") { Author_Label.Visible = false; Author_Value.Visible = false; } }
WebControl3.ascx:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl3.ascx.cs" Inherits="Demo.WebUserControl3" %> <asp:Label ID="Edition_Label" runat="server" class="Options_text" Text="Edition:"/> <asp:Label ID="Edition_Value" runat="server" class="Options_text" Text="">111</asp:Label><br/>
Best regards,
Yijing Sun
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, May 1, 2020 6:02 AM
All replies
-
User-474980206 posted
This has nothing to with html, css or JavaScript. The question is about building web form controls. The question is not totally clear, but yes, one control can pass properties to its children.
Thursday, April 30, 2020 2:56 PM -
User1557998713 posted
What happens is that in the second <div> I don't want the children to inherit the "Visible" property because then I have to manually deactivate it in the labels that are not going to have a value. Property can be inherited, but can it be prevented from inheriting?
Thursday, April 30, 2020 3:13 PM -
User-474980206 posted
in webform controls, the Visible property controls whether the control is rendered. the default is true, its not inherited. if the parent does not render, then none of its children will.
you could build a smart label control that only rendered if it had content.
Thursday, April 30, 2020 3:29 PM -
User1535942433 posted
Hi zequion,
zequion
1.- The first <div> works correctly and only the labels with information are shown. In the second <div> when i activate visible = true, at the moment all the labels it contains are automatically visible = true and I don't want labels to be displayed without information. Cuestion:The <div> are in turn within different controls. Is it possible that they inherit the visible property?Accroding to your description,I don't understand your requirment clearly.I'm guessing that you want that you want to have a template ,and then when Author_Value 's value is null,Author_Label and Author_Value of div1 and div2 cann't be displayed.
zequion
2.- I would like to do something like a template to show the same <div> from that template and not have to duplicate code and in the second <div> add the additional field.I suggest you could use UserControl to implement template.
More details,you could refer to below codes:
Asp.net:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="2166472.aspx.cs" Inherits="Demo._2166472" %> <%@ Register TagPrefix="UC" TagName="Author" Src="WebUserControl2.ascx" %> <%@ Register TagPrefix="UC" TagName="cc" Src="WebUserControl3.ascx" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <UC:Author runat="server" ID="author" /> </div> <div> <UC:Author runat="server" ID="author1" /> <UC:cc runat="server" ID="author2" /> </div> </form> </body> </html>
WebControl2.ascx:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl2.ascx.cs" Inherits="Demo.WebUserControl2" %> <asp:Label ID="Author_Label" runat="server" Text="Author:"></asp:Label> <asp:Label ID="Author_Value" runat="server" Text=""></asp:Label>
WebControl2.ascx code-behind:
protected void Page_Load(object sender, EventArgs e) { if (Author_Value.Text == "") { Author_Label.Visible = false; Author_Value.Visible = false; } }
WebControl3.ascx:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl3.ascx.cs" Inherits="Demo.WebUserControl3" %> <asp:Label ID="Edition_Label" runat="server" class="Options_text" Text="Edition:"/> <asp:Label ID="Edition_Value" runat="server" class="Options_text" Text="">111</asp:Label><br/>
Best regards,
Yijing Sun
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, May 1, 2020 6:02 AM -
User1557998713 posted
Very good user control. Thank you
Friday, May 1, 2020 6:15 AM