I've created a template control what I am running into difficults is during the Page_Load event on my web page I cannot access the child controls in the two possible templates.
What I would like to be able to do is set the value or make some changes to the control at runtime but I have not been successful in gaining code access to the control, for example the TEXTBOX1 control in the EditTemplate template.Does anyone know a good way to preform this task, let alone if I am even using the correct event
Example of HTML Render
<
aspSample:TextLabelControl ID="CustTest" runat="server">
<EditTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</EditTemplate>
<ViewTemplate>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</ViewTemplate>
</aspSample:TextLabelControl> My Test Control Code:1 using System; 2 using System.ComponentModel; 3 using System.Web.UI; 4 using System.Web.UI.WebControls; 5 using System.Web.UI.Design; 6 7 8 9 namespace Samples.AspNet.CS.Controls.TextLabelControl 10 { 11 [Designer(typeof(TextLabelControlDesigner)), 12 ToolboxData("<{0}:TextLabelControl runat=server></{0}:TextLabelControl>"), 13 ParseChildren(true)] 14 public sealed class TextLabelControl : WebControl, INamingContainer 15 { 16 17 // Field for the templates 18 19 private ITemplate editTemplate; 20 private ITemplate viewTemplate; 21 private Boolean actionControl; 22 private string valueControl; 23 24 25 public TextLabelControl() 26 { 27 } 28 29 [PersistenceMode(PersistenceMode.InnerProperty), 30 TemplateContainer(typeof(TemplateControl)), 31 TemplateInstance(TemplateInstance.Single) ] 32 33 public ITemplate EditTemplate 34 { 35 36 get { return editTemplate; } 37 set { editTemplate = value; } 38 39 } 40 41 [PersistenceMode(PersistenceMode.InnerProperty), 42 TemplateContainer(typeof(TemplateControl)), 43 TemplateInstance(TemplateInstance.Single) ] 44 45 public ITemplate ViewTemplate 46 { 47 48 get { return viewTemplate; } 49 set { viewTemplate = value; } 50 51 } 52 53 public Boolean Edit 54 { 55 get { return actionControl; } 56 set { actionControl = value; } 57 } 58 59 public string Value 60 { 61 get { return valueControl; } 62 set { valueControl = value; } 63 } 64 65 protected override void CreateChildControls() 66 { 67 68 this.Controls.Clear(); 69 70 //EnsureChildControls(); 71 72 ITemplate template; 73 74 if (actionControl == true) 75 { 76 template = editTemplate; 77 } 78 else 79 { 80 template = viewTemplate; 81 } 82 83 PlaceHolder pan = new PlaceHolder(); 84 template.InstantiateIn(pan); 85 this.Controls.Add(pan); 86 87 //base.CreateChildControls(); 88 } 89 90 } 91 92 93 public class TextLabelControlDesigner : ControlDesigner 94 { 95 TemplateGroupCollection col = null; 96 97 public override void Initialize(IComponent component) 98 { 99 // Initialize the base 100 base.Initialize(component); 101 // Turn on template editing 102 SetViewFlags(ViewFlags.TemplateEditing, true); 103 } 104 105 // Add instructions to the placeholder view of the control 106 public override string GetDesignTimeHtml() 107 { 108 return CreatePlaceHolderDesignTimeHtml("Click here and use " + 109 "the task menu to edit the templates."); 110 } 111 112 113 public override TemplateGroupCollection TemplateGroups 114 { 115 get 116 { 117 118 if (col == null) 119 { 120 // Get the base collection 121 col = base.TemplateGroups; 122 123 // Create variables 124 TemplateGroup tempGroup; 125 TemplateDefinition tempDef; 126 TextLabelControl ctl; 127 128 // Get reference to the component as TemplateGroupsSample 129 ctl = (TextLabelControl)Component; 130 131 // Create a TemplateGroup 132 tempGroup = new TemplateGroup("Template Set A"); 133 134 // Create a TemplateDefinition 135 tempDef = new TemplateDefinition(this, "Template Edit", 136 ctl, "EditTemplate", true); 137 138 // Add the TemplateDefinition to the TemplateGroup 139 tempGroup.AddTemplateDefinition(tempDef); 140 141 // Create another TemplateDefinition 142 tempDef = new TemplateDefinition(this, "Template View", 143 ctl, "ViewTemplate", true); 144 145 // Add the TemplateDefinition to the TemplateGroup 146 tempGroup.AddTemplateDefinition(tempDef); 147 148 // Add the TemplateGroup to the TemplateGroupCollection 149 col.Add(tempGroup); 150 151 } 152 153 return col; 154 } 155 } 156 157 // Do not allow direct resizing unless in TemplateMode 158 public override bool AllowResize 159 { 160 get 161 { 162 if (this.InTemplateMode) 163 return true; 164 else 165 return false; 166 } 167 } 168 } 169 170 } 171
![]() |
0 |
![]() |
Hi,
From the code you provided, the code works well. Actually, if you take a look at the custom server control you provided, you can find there’s a active template designed in that control, only if you set the Edit property to true, the EditTemplate will be the active template, otherwise, ViewTemplate is the active one.
So based on the control you provided, you can use it in two ways:
Suppose you have declared your custom server control on aspx page in the following way:<cc1:TextLabelControl ID="TextLabelControl1" runat="server"> <EditTemplate> <asp:TextBox ID="textbox1" Text="abc" runat="server"></asp:TextBox> </EditTemplate> <ViewTemplate> <asp:Label ID="Label" runat="server" Text="Hello"></asp:Label> </ViewTemplate> </cc1:TextLabelControl>Scenario 1: Set Edit property to false (or don’t set the Edit property), the ViewTemplate will actually take effect.
Label lb = this.TextLabelControl1.FindControl("Label") as Label; TextBox tb = this.TextLabelControl1.FindControl("textbox1") as TextBox; Response.Write(tb.Text); // Null exception occurs. Response.Write(lb.Text); // Output: HelloScenario 2: Set Edit property to true, the EditTemplate will actually take effect.
this.TextLabelControl1.Edit = true; Label lb = this.TextLabelControl1.FindControl("Label") as Label; TextBox tb = this.TextLabelControl1.FindControl("textbox1") as TextBox; Response.Write(tb.Text); // Output: abc Response.Write(lb.Text); // Null exception occurs.Thanks.
Michael Jin.
Microsoft Online Community Support
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
![]() |
0 |
![]() |