Answered by:
set property values to user control before loading it.

Question
-
User-1231945312 posted
Hi,
I created a user control(not custom control) and before calling loadcontrol method to display this user control, i would like to set values to this usercontrol such as the code below
userControl.Property = "20"
Page.LoadControl("userControl.aspx");But obviously,the code above wont work.Is there any way that i can do this?
Many Thanks
Friday, June 20, 2008 12:29 PM
Answers
-
User-1407570774 posted
Hi there,
MyUserControl.ascx:
1 using System; 2 3 namespace WebApplication1 4 { 5 6 public partial class MyUserControl : System.Web.UI.UserControl 7 { 8 9 public MyUserControl() { } 10 11 public MyUserControl(string text) 12 { 13 this.Text = text; 14 } 15 16 public string Text 17 { 18 get 19 { 20 object text = ViewState["Text"]; 21 return text != null ? text.ToString() : string.Empty; 22 } 23 set { ViewState["Text"] = value; } 24 } 25 26 protected void Page_Load(object sender, EventArgs e) 27 { 28 Response.Write(this.Text); 29 } 30 31 } 32 33 }
Default.aspx:
1 using System; 2 using System.Web.UI; 3 4 namespace WebApplication1 5 { 6 public partial class _Default : System.Web.UI.Page 7 { 8 protected void Page_Load(object sender, EventArgs e) 9 { 10 MyUserControl myuc = (MyUserControl)Page.LoadControl(typeof(MyUserControl), new object[] { "This is test..." }); 11 this.Controls.Add(myuc); 12 } 13 } 14 }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, June 21, 2008 11:20 AM
All replies
-
User-667042492 posted
hi,
in your usercontrol class you must define properties.. for example;
private string name;
public string Name {
get {return name;}
set {name = value;}}
and in your page,
userControl.Name = "myname";
i hope this help you..
Friday, June 20, 2008 1:07 PM -
User-1231945312 posted
I already tried this code but it doesnt work.
I need to set value to the property before the user control is loaded by Page.LoadControl() method. And i dont know how i can do that.
Friday, June 20, 2008 1:25 PM -
User-442214108 posted
First off, it needs to be an ASCX file, not an ASPX, if it's a user control
Then try something like this:
Dim myControl as Control=CType(Page.LoadControl(FullPath with .ascx extension), Control)
myControl.Property="20"
Yourcontainer.Controls.Add(myControl)
Friday, June 20, 2008 1:48 PM -
User-16411453 posted
myControl.Property=[Text]<Bindable(True), Category("Appearance"), DefaultValue(""), Localizable(True)> Property Text() As String
Get
Dim s As String = CStr(ViewState("Text"))
If s Is Nothing Then
Return String.Empty
Else
Return s
End If
End Get
Set(ByVal Value As String)
ViewState("Text") = Value
End Set
End Property
Friday, June 20, 2008 6:06 PM -
User-1231945312 posted
Ok,Thanks very much. i will try this one.
Friday, June 20, 2008 9:24 PM -
User-1231945312 posted
i just tried your solution.but it doesnt solve my problem. Is there a way to set this property inside Page.LoadControl(FullPath with .ascx extension)?
Because if this property is not initialized, the class will be not able to be loaded or instantiated by Page.LoadControl(FullPath with .ascx extension)
Friday, June 20, 2008 9:32 PM -
User-1407570774 posted
Hi there,
MyUserControl.ascx:
1 using System; 2 3 namespace WebApplication1 4 { 5 6 public partial class MyUserControl : System.Web.UI.UserControl 7 { 8 9 public MyUserControl() { } 10 11 public MyUserControl(string text) 12 { 13 this.Text = text; 14 } 15 16 public string Text 17 { 18 get 19 { 20 object text = ViewState["Text"]; 21 return text != null ? text.ToString() : string.Empty; 22 } 23 set { ViewState["Text"] = value; } 24 } 25 26 protected void Page_Load(object sender, EventArgs e) 27 { 28 Response.Write(this.Text); 29 } 30 31 } 32 33 }
Default.aspx:
1 using System; 2 using System.Web.UI; 3 4 namespace WebApplication1 5 { 6 public partial class _Default : System.Web.UI.Page 7 { 8 protected void Page_Load(object sender, EventArgs e) 9 { 10 MyUserControl myuc = (MyUserControl)Page.LoadControl(typeof(MyUserControl), new object[] { "This is test..." }); 11 this.Controls.Add(myuc); 12 } 13 } 14 }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, June 21, 2008 11:20 AM -
User-16411453 posted
Sorry about that. I gave you a server control solution. Did not really notice your working on a user control.
You should try the user control forum for better info. I've never made one, so I have no clue.
Saturday, June 21, 2008 7:01 PM -
User1075616431 posted
Can you do this:
Public Property something() As String
Get
If ViewState("something") Is Nothing Then
Return ""
End If
Return CType(ViewState("something"), String)
End Get
Set(ByVal Value As String)
ViewState("something") = Value
doSomething(Value)
End Set
End Property
Protected Sub doSomething(ByVal something As String)
End SubFriday, June 19, 2009 4:32 AM