A simple problem with user controls
-
Hi, I am having trouble with user controls. Just for testing, I created a user control named "MyUC". Inside, I added a label called "Label1". I then went to WebForm1.aspx, and dragged and dropped the MyUC user control. What I need is to be able to set the Label1 Text property from CodeBehind of the webform (WebForm1.aspx.cs). Can you please give me the exact code for the two steps? (ie. 1-defining the property in MyUc.ascx.cs, 2-Referencing the user control I added by drag'n'drop and setting the property either in Page_Load or in the click_button of some button.) I know this must be so easy, but I am still facing problems with it.. Thanks!!
-
Hi, I am having trouble with user controls. Just for testing, I created a user control named "MyUC". Inside, I added a label called "Label1". I then went to WebForm1.aspx, and dragged and dropped the MyUC user control. What I need is to be able to set the Label1 Text property from CodeBehind of the webform (WebForm1.aspx.cs). Can you please give me the exact code for the two steps? (ie. 1-defining the property in MyUc.ascx.cs, 2-Referencing the user control I added by drag'n'drop and setting the property either in Page_Load or in the click_button of some button.) I know this must be so easy, but I am still facing problems with it.. Thanks!!
benqazou wrote: I then went to WebForm1.aspx, and dragged and dropped the MyUC user control. Don't use the drag and drop functionality because a/ you should learn how to write the code b/ the designer makes a mess of your HTML. benqazou wrote: Can you please give me the exact code for the two steps? Sure In the control public string LabelText { set { Label1.Text = value; } } In the page, in whatever event you like. MyUC.LabelText = "The label text"; This is C# code, I suspect you're using VB.NET, but the idea is the same, just the syntax changes. Christian Graus - Microsoft MVP - C++
-
benqazou wrote: I then went to WebForm1.aspx, and dragged and dropped the MyUC user control. Don't use the drag and drop functionality because a/ you should learn how to write the code b/ the designer makes a mess of your HTML. benqazou wrote: Can you please give me the exact code for the two steps? Sure In the control public string LabelText { set { Label1.Text = value; } } In the page, in whatever event you like. MyUC.LabelText = "The label text"; This is C# code, I suspect you're using VB.NET, but the idea is the same, just the syntax changes. Christian Graus - Microsoft MVP - C++
Thanks a lot for your reply, But I am still wondering how to add the user control, in another way than drag'n'drop. Suppose I have my user control MyUC well defined, do you suggest the use of LoadControl method? Either you do or you have another solution, can you please give once more the code for the solution(s if more than one)? Thanks!
-
Thanks a lot for your reply, But I am still wondering how to add the user control, in another way than drag'n'drop. Suppose I have my user control MyUC well defined, do you suggest the use of LoadControl method? Either you do or you have another solution, can you please give once more the code for the solution(s if more than one)? Thanks!
benqazou wrote: do you suggest the use of LoadControl method? Hell, no. The way to do it is to register your control at the top of the page like this: <%@ Register TagPrefix="myPrefix" TagName="ControlName" Src="/Controls/MyUC.ascx" %> then in the page you can use it like this: You can set any property of the control here, with the name="value" syntax. Then in the code behind put an instance of the control with the same name, make it protected, and the framework will hook them up for you. Christian Graus - Microsoft MVP - C++
-
benqazou wrote: do you suggest the use of LoadControl method? Hell, no. The way to do it is to register your control at the top of the page like this: <%@ Register TagPrefix="myPrefix" TagName="ControlName" Src="/Controls/MyUC.ascx" %> then in the page you can use it like this: You can set any property of the control here, with the name="value" syntax. Then in the code behind put an instance of the control with the same name, make it protected, and the framework will hook them up for you. Christian Graus - Microsoft MVP - C++