c# - How to pass values - in a loop - to a user control -
the code page used test calling user control dynamically.
<%@ page language="c#" autoeventwireup="true" codefile="testuc.aspx.cs" inherits="testuc" %> <%@ register tagprefix="uc" tagname="testuc" src="nccsbyrole.ascx" %> <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:placeholder runat="server" id="placeholder1"></asp:placeholder> </div> </form> </body> </html>
the code behind page:
using system; using system.collections.generic; using system.linq; using system.web; using system.web.ui; using system.web.ui.webcontrols; public partial class testuc : system.web.ui.page { protected void page_load(object sender, eventargs e) { (int = 0; < 5; i++) { usercontrol myusercontrol = (usercontrol)loadcontrol("teamsbyrole.ascx"); //myusercontrol.userid = i; ******************** not working placeholder1.controls.add(myusercontrol); } } }
the code user control.
<%@ control language="c#" autoeventwireup="true" codefile="teamsbyrole.ascx.cs" inherits="teamsbyrole" %> <asp:literal id="ltlname" runat="server"></asp:literal>
and code behind user control.
using system; using system.collections.generic; using system.linq; using system.web; using system.web.ui; using system.web.ui.webcontrols; public partial class teamsbyrole : system.web.ui.usercontrol { private int _userid; public int userid { { return _userid; } set { _userid = value; } } protected void page_load(object sender, eventargs e) { string myname = "angela"; ltlname.text = "<p>" + myname + "</p>"; } }
so, have page reference user control. want call user control dynamically , need pass userid page user control loop through data. in example code above, looping 0 4 , user control being 'called' 5 times - name 'angela' being written screen 5 times.
but, how pass userid (in loop) usercontrol? have public property of userid in user control but, in page 'calling' user control - if comment in line ...
myusercontrol.userid = i;
an error reported ... 'system.web.ui.usercontrol' not contain definition 'userid' , no extension method 'userid' accepting first argument of type 'system.web.ui.usercontrol' found (are missing using directive or assembly reference?)
how can pass userid user control - within loop have?
you casting control usercontrol
userid
not declared on usercontrol
. rather, member of teamsbyrole
, should cast to.
protected void page_load(object sender, eventargs e) { (int = 0; < 5; i++) { teamsbyrole myusercontrol = (teamsbyrole)loadcontrol("teamsbyrole.ascx"); myusercontrol.userid = i; placeholder1.controls.add(myusercontrol); } }
Comments
Post a Comment