Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. A simple problem with user controls

A simple problem with user controls

Scheduled Pinned Locked Moved ASP.NET
winformstestingbeta-testinghelpquestion
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    benqazou
    wrote on last edited by
    #1

    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!!

    C 1 Reply Last reply
    0
    • B benqazou

      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!!

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      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++

      B 1 Reply Last reply
      0
      • C Christian Graus

        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++

        B Offline
        B Offline
        benqazou
        wrote on last edited by
        #3

        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!

        C 1 Reply Last reply
        0
        • B benqazou

          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!

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          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++

          B 1 Reply Last reply
          0
          • C Christian Graus

            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++

            B Offline
            B Offline
            benqazou
            wrote on last edited by
            #5

            Thank you man, it eventually worked..

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups