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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Web Development
  3. ASP.NET
  4. Referencing User Control Attributes in Page Code Behind

Referencing User Control Attributes in Page Code Behind

Scheduled Pinned Locked Moved ASP.NET
graphicssysadminhelpquestion
12 Posts 3 Posters 16 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.
  • J Offline
    J Offline
    janetb99
    wrote on last edited by
    #1

    I'm trying to reference the attributes of a panel in a User Control in the code behind of a web page. I've tried about a dozen variations. Can't be this hard. Anybody got the correct syntax? Thanks!

    <%@ Register Src="~/inc/ceSteps.ascx" TagName="ceSteps" TagPrefix="uc1" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server"></asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <uc1:ceSteps ID="ceSteps" runat="server" />

    Then in the page's code behind, here's where I get stuck.
    Dim myCtrl As UserControl = CType(FindControl("uc1"), UserControl)
    myCtrl.pnlStage1.BackColor = System.Drawing.ColorTranslator.FromHtml("#dfdfdf")

    yahda, yahda, yahda....

    J R 2 Replies Last reply
    0
    • J janetb99

      I'm trying to reference the attributes of a panel in a User Control in the code behind of a web page. I've tried about a dozen variations. Can't be this hard. Anybody got the correct syntax? Thanks!

      <%@ Register Src="~/inc/ceSteps.ascx" TagName="ceSteps" TagPrefix="uc1" %>
      <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server"></asp:Content>
      <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
      <uc1:ceSteps ID="ceSteps" runat="server" />

      Then in the page's code behind, here's where I get stuck.
      Dim myCtrl As UserControl = CType(FindControl("uc1"), UserControl)
      myCtrl.pnlStage1.BackColor = System.Drawing.ColorTranslator.FromHtml("#dfdfdf")

      yahda, yahda, yahda....

      J Offline
      J Offline
      jkirkerx
      wrote on last edited by
      #2

      The register is the webform side of the dll. The code side is something like, ceSteps is the class name assigned to the program code you want to access. Dim myCtrl As ceSteps = new ceSteps ceSteps myCtrl = new ceSteps; ceSteps *myCtrl = new ceSteps;

      J 1 Reply Last reply
      0
      • J jkirkerx

        The register is the webform side of the dll. The code side is something like, ceSteps is the class name assigned to the program code you want to access. Dim myCtrl As ceSteps = new ceSteps ceSteps myCtrl = new ceSteps; ceSteps *myCtrl = new ceSteps;

        J Offline
        J Offline
        janetb99
        wrote on last edited by
        #3

        I kinda get what you're saying and I tried it, but it won't let me reference the class ceSteps as a namespace like I do when I import at the top of the page (say for my login code) and then reference an instance of a namespace. I've used the code below before in another app, honest, but it's not working and I'm stumped.

        Dim myPnl As Panel = CType(CType(FindControl("ceSteps"), UserControl).FindControl("pnlStage1"), Panel)
        myPnl.BackColor = System.Drawing.ColorTranslator.FromHtml("#dfdfdf")

        J J 2 Replies Last reply
        0
        • J janetb99

          I kinda get what you're saying and I tried it, but it won't let me reference the class ceSteps as a namespace like I do when I import at the top of the page (say for my login code) and then reference an instance of a namespace. I've used the code below before in another app, honest, but it's not working and I'm stumped.

          Dim myPnl As Panel = CType(CType(FindControl("ceSteps"), UserControl).FindControl("pnlStage1"), Panel)
          myPnl.BackColor = System.Drawing.ColorTranslator.FromHtml("#dfdfdf")

          J Offline
          J Offline
          janetb99
          wrote on last edited by
          #4

          I also thought it might be because I was missing the reference through the master/content relationship, but that wasn't it:

          Dim myConPH As ContentPlaceHolder
          myConPH = CType(FindControl("SSLmaster_ContentPlaceHolder1"), ContentPlaceHolder)
          Dim myPnl As Panel = CType(CType(myConPH.FindControl("ceSteps"), UserControl).FindControl("pnlStage1"), Panel)

          J 1 Reply Last reply
          0
          • J janetb99

            I also thought it might be because I was missing the reference through the master/content relationship, but that wasn't it:

            Dim myConPH As ContentPlaceHolder
            myConPH = CType(FindControl("SSLmaster_ContentPlaceHolder1"), ContentPlaceHolder)
            Dim myPnl As Panel = CType(CType(myConPH.FindControl("ceSteps"), UserControl).FindControl("pnlStage1"), Panel)

            J Offline
            J Offline
            jkirkerx
            wrote on last edited by
            #5

            I wasn't able to see your user control. With user controls, like server controls, you don't use the code behind pages on the webform to interact with them. Instead, you write more code in your user control. So just register the control in the webform like you did. Then if you want to do something in page.load, you add an override to your user control.

            Public Class MyControl
            Private lbl_Website_Name As Label

            Protected overrides Sub OnInit(ByVal e as System.EventArgs)
            controls.clear

            //Dim lbl_Website_Name As Label
            lbl_WebsiteName = New Label
            With lbl_Website_Name
            End With
            Controls.Add(lbl_Website_name)

            End Sub
            Protected overrides Sub OnLoad(ByVal e As System.EventArgs)

            Do you stuff.
            lbl_Website_Name.txt = sWebsiteName

            End Sub

            This will append to the current page.load on the web form, and operate identical to page load. You can call the control by the name assigned,

            J 1 Reply Last reply
            0
            • J janetb99

              I kinda get what you're saying and I tried it, but it won't let me reference the class ceSteps as a namespace like I do when I import at the top of the page (say for my login code) and then reference an instance of a namespace. I've used the code below before in another app, honest, but it's not working and I'm stumped.

              Dim myPnl As Panel = CType(CType(FindControl("ceSteps"), UserControl).FindControl("pnlStage1"), Panel)
              myPnl.BackColor = System.Drawing.ColorTranslator.FromHtml("#dfdfdf")

              J Offline
              J Offline
              jkirkerx
              wrote on last edited by
              #6

              Well, you use css to change the background color of the panel. But I don't when you want to change the color. On postback, in the server, or when creating the control If you want to remove the existing css MyPnl.Style.Remove(HtmlTextWriterStyle.backgroundColor) If you want to add a style MyPnl.Style.Add(HtmlTextWriterStyle.backgroundColor, "#dfdfdf")

              J 1 Reply Last reply
              0
              • J jkirkerx

                Well, you use css to change the background color of the panel. But I don't when you want to change the color. On postback, in the server, or when creating the control If you want to remove the existing css MyPnl.Style.Remove(HtmlTextWriterStyle.backgroundColor) If you want to add a style MyPnl.Style.Add(HtmlTextWriterStyle.backgroundColor, "#dfdfdf")

                J Offline
                J Offline
                janetb99
                wrote on last edited by
                #7

                Sorry, that won't work for me if I can't reference the control to change the style. Understand the problem now? Let me try and be a little more precise, maybe that will help. I really think it's something to do with master/child since I was able to successfully reference one when not using the master.

                Master:
                <%@ Master Language="VB" CodeFile="SSLmaster.master.vb" Inherits="master" EnableTheming="true" %>
                <body>
                <form id="form1" runat="server" enableviewstate="true"><asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
                <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server"></asp:ContentPlaceHolder>
                yahda, yahda, yahda...

                Child:
                <%@ Page Title="yahda" Theme="SSL" Language="VB" MasterPageFile="~/inc/SSLmaster.master" AutoEventWireup="false" CodeFile="zdetail1.aspx.vb" Inherits="calendar_zdetail1" %>
                <%@ Register Src="~/inc/ceSteps.ascx" TagName="ceSteps" TagPrefix="uc1" %>
                <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

                UserControl:
                <%@ Control Language="VB" AutoEventWireup="false" CodeFile="ceSteps.ascx.vb" Inherits="inc_ceSteps" %>
                <asp:Panel runat="server" ID="Panel1" > Step 1 </asp:Panel>

                CodeBehind of Child:
                First Stab (successful on page without Master):
                Dim myPnl As Panel = CType(CType(FindControl("ceSteps"), UserControl).FindControl("pnlStage1"), Panel)
                myPnl.BackColor = System.Drawing.ColorTranslator.FromHtml("#dfdfdf")

                Second Stab (and many more variations)
                Dim myConPH As ContentPlaceHolder
                myConPH = CType(FindControl("SSLmaster_ContentPlaceHolder1"), ContentPlaceHolder)
                Dim myPnl As Panel = CType(CType(myConPH.FindControl("ceSteps"), UserControl).FindControl("pnlStage1"), Panel)
                myPnl.BackColor = System.Drawing.ColorTranslator.FromHtml("#dfdfdf")

                J 1 Reply Last reply
                0
                • J jkirkerx

                  I wasn't able to see your user control. With user controls, like server controls, you don't use the code behind pages on the webform to interact with them. Instead, you write more code in your user control. So just register the control in the webform like you did. Then if you want to do something in page.load, you add an override to your user control.

                  Public Class MyControl
                  Private lbl_Website_Name As Label

                  Protected overrides Sub OnInit(ByVal e as System.EventArgs)
                  controls.clear

                  //Dim lbl_Website_Name As Label
                  lbl_WebsiteName = New Label
                  With lbl_Website_Name
                  End With
                  Controls.Add(lbl_Website_name)

                  End Sub
                  Protected overrides Sub OnLoad(ByVal e As System.EventArgs)

                  Do you stuff.
                  lbl_Website_Name.txt = sWebsiteName

                  End Sub

                  This will append to the current page.load on the web form, and operate identical to page load. You can call the control by the name assigned,

                  J Offline
                  J Offline
                  janetb99
                  wrote on last edited by
                  #8

                  Sorry, that won't work for me - I need to do so from the child webpage. Let me try and be a little more precise, maybe that will help. I really think it's something to do with master/child since I was able to successfully reference one when not using the master.

                  Master:
                  <%@ Master Language="VB" CodeFile="SSLmaster.master.vb" Inherits="master" EnableTheming="true" %>
                  <body>
                  <form id="form1" runat="server" enableviewstate="true"><asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
                  <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server"></asp:ContentPlaceHolder>
                  yahda, yahda, yahda...

                  Child:
                  <%@ Page Title="yahda" Theme="SSL" Language="VB" MasterPageFile="~/inc/SSLmaster.master" AutoEventWireup="false" CodeFile="zdetail1.aspx.vb" Inherits="calendar_zdetail1" %>
                  <%@ Register Src="~/inc/ceSteps.ascx" TagName="ceSteps" TagPrefix="uc1" %>
                  <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

                  UserControl:
                  <%@ Control Language="VB" AutoEventWireup="false" CodeFile="ceSteps.ascx.vb" Inherits="inc_ceSteps" %>
                  <asp:Panel runat="server" ID="Panel1" > Step 1 </asp:Panel>

                  CodeBehind of Child:
                  First Stab (successful on page without Master):
                  Dim myPnl As Panel = CType(CType(FindControl("ceSteps"), UserControl).FindControl("pnlStage1"), Panel)
                  myPnl.BackColor = System.Drawing.ColorTranslator.FromHtml("#dfdfdf")

                  Second Stab (and many more variations)
                  Dim myConPH As ContentPlaceHolder
                  myConPH = CType(FindControl("SSLmaster_ContentPlaceHolder1"), ContentPlaceHolder)
                  Dim myPnl As Panel = CType(CType(myConPH.FindControl("ceSteps"), UserControl).FindControl("Panel1"), Panel)
                  myPnl.BackColor = System.Drawing.ColorTranslator.FromHtml("#dfdfdf")

                  1 Reply Last reply
                  0
                  • J janetb99

                    Sorry, that won't work for me if I can't reference the control to change the style. Understand the problem now? Let me try and be a little more precise, maybe that will help. I really think it's something to do with master/child since I was able to successfully reference one when not using the master.

                    Master:
                    <%@ Master Language="VB" CodeFile="SSLmaster.master.vb" Inherits="master" EnableTheming="true" %>
                    <body>
                    <form id="form1" runat="server" enableviewstate="true"><asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
                    <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server"></asp:ContentPlaceHolder>
                    yahda, yahda, yahda...

                    Child:
                    <%@ Page Title="yahda" Theme="SSL" Language="VB" MasterPageFile="~/inc/SSLmaster.master" AutoEventWireup="false" CodeFile="zdetail1.aspx.vb" Inherits="calendar_zdetail1" %>
                    <%@ Register Src="~/inc/ceSteps.ascx" TagName="ceSteps" TagPrefix="uc1" %>
                    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

                    UserControl:
                    <%@ Control Language="VB" AutoEventWireup="false" CodeFile="ceSteps.ascx.vb" Inherits="inc_ceSteps" %>
                    <asp:Panel runat="server" ID="Panel1" > Step 1 </asp:Panel>

                    CodeBehind of Child:
                    First Stab (successful on page without Master):
                    Dim myPnl As Panel = CType(CType(FindControl("ceSteps"), UserControl).FindControl("pnlStage1"), Panel)
                    myPnl.BackColor = System.Drawing.ColorTranslator.FromHtml("#dfdfdf")

                    Second Stab (and many more variations)
                    Dim myConPH As ContentPlaceHolder
                    myConPH = CType(FindControl("SSLmaster_ContentPlaceHolder1"), ContentPlaceHolder)
                    Dim myPnl As Panel = CType(CType(myConPH.FindControl("ceSteps"), UserControl).FindControl("pnlStage1"), Panel)
                    myPnl.BackColor = System.Drawing.ColorTranslator.FromHtml("#dfdfdf")

                    J Offline
                    J Offline
                    jkirkerx
                    wrote on last edited by
                    #9

                    I'm looking at your markup code, I understand the markup code, But I have no idea what your trying to achieve. What the steps are, the purpose of changing the color, when and where. My interpretation is that you want to find the asp.net ID of the panel control in the child webform, make a handle to it, and change the background color of the panel. Not the Answer but here's how it works:: When adding a master page, the id of the control will be prefixed with CT_100_ or CT100_ That's the only difference between the 2 scenarios. MasterPage will also start to auto ID your controls, if you don't assign a ID manually. In other words, when you master page, asp.net will automatically fix all your control id's so it can find and track the controls, and disregards your need to find them. Every control has to have a unique ID, and no one can be the same. So myPnl becomes CT_100_myPnl if you assign the ID atribute. If you don't assign the ID attribute, asp.net will assign a senquential auto naming ID code to the control object, and find control will never find it. Naming Practices: When naming a control in a user control, you can use ID = [ID] & "_myPnl", which produces CT_100_ceSteps_myPnl This will stop the auto id's of your controls, so you actually are in charge of naming your objects. Diagnostics: So look at the html that was created by the server in the browsers source, in both with and without master page, and write down the control id or each one. And put that exact name in find control. Oh, ever the placeholder name will change as well. I haven't used find control in years, and I abandoned the use of it, and have forgotten how to use it in master pages. Over the years I learned how to access any object on the page, at the right time and place, so that's not an issue for me anymore.

                    1 Reply Last reply
                    0
                    • J janetb99

                      I'm trying to reference the attributes of a panel in a User Control in the code behind of a web page. I've tried about a dozen variations. Can't be this hard. Anybody got the correct syntax? Thanks!

                      <%@ Register Src="~/inc/ceSteps.ascx" TagName="ceSteps" TagPrefix="uc1" %>
                      <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server"></asp:Content>
                      <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
                      <uc1:ceSteps ID="ceSteps" runat="server" />

                      Then in the page's code behind, here's where I get stuck.
                      Dim myCtrl As UserControl = CType(FindControl("uc1"), UserControl)
                      myCtrl.pnlStage1.BackColor = System.Drawing.ColorTranslator.FromHtml("#dfdfdf")

                      yahda, yahda, yahda....

                      R Offline
                      R Offline
                      RichardGrimmer
                      wrote on last edited by
                      #10

                      The problem is that UserControl has no pn1Stage1 member... In the first line, you're specifying that myCtrl is of type UserControl. In order to access a specific property of YOUR user control, you need a variable of that type...something like... Dim myCtrl as MyControlType = CType(FindControl("ceSteps", MyControlType)) The trick is that FindControl returns a Control type, which you then cast to the correct type.

                      C# has already designed away most of the tedium of C++.

                      J 2 Replies Last reply
                      0
                      • R RichardGrimmer

                        The problem is that UserControl has no pn1Stage1 member... In the first line, you're specifying that myCtrl is of type UserControl. In order to access a specific property of YOUR user control, you need a variable of that type...something like... Dim myCtrl as MyControlType = CType(FindControl("ceSteps", MyControlType)) The trick is that FindControl returns a Control type, which you then cast to the correct type.

                        C# has already designed away most of the tedium of C++.

                        J Offline
                        J Offline
                        janetb99
                        wrote on last edited by
                        #11

                        Thanks for the reply, Richard. Tried it, but no go. I still think it has something to do with those pesky Master pages. Again, the one I've been using without Master Pages works fine... Got the same error. Dim myCtrl As UserControl = CType(FindControl("ceSteps1"), UserControl) Dim myPnl As Panel = CType(myCtrl.FindControl("pnlStage1"), Panel) myPnl.BackColor = Drawing.Color.Azure

                        1 Reply Last reply
                        0
                        • R RichardGrimmer

                          The problem is that UserControl has no pn1Stage1 member... In the first line, you're specifying that myCtrl is of type UserControl. In order to access a specific property of YOUR user control, you need a variable of that type...something like... Dim myCtrl as MyControlType = CType(FindControl("ceSteps", MyControlType)) The trick is that FindControl returns a Control type, which you then cast to the correct type.

                          C# has already designed away most of the tedium of C++.

                          J Offline
                          J Offline
                          janetb99
                          wrote on last edited by
                          #12

                          I found this, and I kinda get what they're saying, but I can't get it to work either. tip18843

                          Protected MyCtl As Panel
                          Private Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Init
                          MyCtl = DirectCast(FindControl("pnlStage1"), Panel)
                          End Sub
                          Protected Sub btnStep2b_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnStep2b.Click
                          MyCtl.BackColor = Drawing.Color.Azure
                          End Sub

                          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