ASP.NET Can't access user control's property in code-behind file -
visual studio 2008, c#, asp.net
need make multiple copies (or instances) of web user control programmatically.
i have usercontrolfile.ascx contains asp:label id="vhlabel" in it. in file set label's text property through public string vhtext variable, usual:
public string vhtext { set { vhlabel.text = value; } }
there divs , other html data in file cannot created in c# programmatically, have use ascx file.
now, in code-behind file need to:
- clone user control many times;
- set unique value label's property "text" of each clone through "vhtext" variable.
please, share suggestions on these issues. if need, can show code here. i've been looking answer long, still unsuccessful.
you can find walkthrough of how programmatically add user controls page here: http://msdn.microsoft.com/en-us/library/c0az2h86(v=vs.100).aspx
in case, need in usercontrolfile.ascx
control (the classname
attribute important bit):
<%@ control language="c#" autoeventwireup="true" codefile="usercontrolfile.ascx.cs" inherits="usercontrolfile" classname="myusercontrol" %> <asp:label id="vhlabel" runat="server"></asp:label>
then, @ top of page include control (change file path appropriate structure):
<%@ reference control="~/controls/usercontrolfile.ascx" %>
finally, code behind of page contain code programmatically add instances of control via loadcontrol
method , use of myusercontrol
type - defined in classname
attribute in @control directive above:
protected void page_load(object sender, eventargs e) { var control = (asp.myusercontrol)loadcontrol("~/controls/usercontrolfile.ascx"); control.vhtext = "1"; page.controls.add(control); control = (asp.myusercontrol)loadcontrol("~/controls/usercontrolfile.ascx"); control.vhtext = "2"; page.controls.add(control); /* etc... */ }
that should point in right direction...
Comments
Post a Comment