Declare Custom DLL Control in Code-Behind to set properties

Discussion in 'ASP .Net' started by Technical Support, Apr 1, 2004.

  1. Good afternoon,
    I created a custom control, compiled it into a DLL, and added it to the BIN
    directory. I placed an instance of the control in a webForm and am trying to
    programatically change its properties via the Page_Load subroutine in a
    code-behind file. Can someone please tell me the proper procedure for
    declaring the new custom control in the code-behind file? I have supplied
    all of the code associated with this very basic example and am using VS.NET.
    The solution's name is "Learn"

    Thank you in advance.

    Eric

    Compiled Custom Control (showcolor.dll)
    -------------------------------------------
    Imports System
    Imports System.Web
    Imports System.Web.UI

    Namespace myControls
    Public Class ShowColor : Inherits Control
    Public Text As String
    Public Color As String
    Protected Overrides Sub Render(ByVal objTextWriter As
    HtmlTextWriter)
    objTextWriter.AddAttribute("Color", Color)
    objTextWriter.RenderBeginTag("Font")
    objTextWriter.Write(Text)
    objTextWriter.RenderEndTag()
    End Sub
    End Class
    End Namespace
    ++++++++++++++++++++++++++

    WebForm (DisplayShowColor.aspx)
    ---------------------------------------
    <%@ Page Language="vb" AutoEventWireup="false"
    Codebehind="DisplayShowColor.aspx.vb" Inherits="learn.DisplayShowColor"%>
    <%@ Register TagPrefix="myControls" Namespace="myControls"
    Assembly="ShowColor" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    <HEAD>
    <title>DisplayShowColor</title>
    </HEAD>
    <body MS_POSITIONING="GridLayout">
    <TABLE height="39" cellSpacing="0" cellPadding="0" width="243"
    border="0" ms_2d_layout="TRUE">
    <TR vAlign="top">
    <TD>
    <myControls:ShowColor id="ctrlShowColor" Runat="server" />
    </TD>
    </TR>
    </TABLE>
    </body>
    </HTML>
    ++++++++++++++++++++++++++++++++++++++

    WebForm - Code-Behind (DisplayShowColor.aspx.vb)
    ---------------------------------------------------------
    Public Class DisplayShowColor
    Inherits System.Web.UI.Page
    Protected ctrlShowColor As myControls.ShowColor
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load
    ctrlShowColor.Text = "Hello Ethan"
    ctrlShowColor.Color = "Orange"
    End Sub
    End Class
     
    Technical Support, Apr 1, 2004
    #1
    1. Advertising

  2. Add the control to your toolbox, then drag the control from the toolbox onto
    your form.
    All the declarations should then happen automatically for you.

    --
    I hope this helps,
    Steve C. Orr, MCSD, MVP
    http://Steve.Orr.net


    "Technical Support" <> wrote in message
    news:...
    > Good afternoon,
    > I created a custom control, compiled it into a DLL, and added it to the

    BIN
    > directory. I placed an instance of the control in a webForm and am trying

    to
    > programatically change its properties via the Page_Load subroutine in a
    > code-behind file. Can someone please tell me the proper procedure for
    > declaring the new custom control in the code-behind file? I have supplied
    > all of the code associated with this very basic example and am using

    VS.NET.
    > The solution's name is "Learn"
    >
    > Thank you in advance.
    >
    > Eric
    >
    > Compiled Custom Control (showcolor.dll)
    > -------------------------------------------
    > Imports System
    > Imports System.Web
    > Imports System.Web.UI
    >
    > Namespace myControls
    > Public Class ShowColor : Inherits Control
    > Public Text As String
    > Public Color As String
    > Protected Overrides Sub Render(ByVal objTextWriter As
    > HtmlTextWriter)
    > objTextWriter.AddAttribute("Color", Color)
    > objTextWriter.RenderBeginTag("Font")
    > objTextWriter.Write(Text)
    > objTextWriter.RenderEndTag()
    > End Sub
    > End Class
    > End Namespace
    > ++++++++++++++++++++++++++
    >
    > WebForm (DisplayShowColor.aspx)
    > ---------------------------------------
    > <%@ Page Language="vb" AutoEventWireup="false"
    > Codebehind="DisplayShowColor.aspx.vb" Inherits="learn.DisplayShowColor"%>
    > <%@ Register TagPrefix="myControls" Namespace="myControls"
    > Assembly="ShowColor" %>
    > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    > <HTML>
    > <HEAD>
    > <title>DisplayShowColor</title>
    > </HEAD>
    > <body MS_POSITIONING="GridLayout">
    > <TABLE height="39" cellSpacing="0" cellPadding="0" width="243"
    > border="0" ms_2d_layout="TRUE">
    > <TR vAlign="top">
    > <TD>
    > <myControls:ShowColor id="ctrlShowColor" Runat="server" />
    > </TD>
    > </TR>
    > </TABLE>
    > </body>
    > </HTML>
    > ++++++++++++++++++++++++++++++++++++++
    >
    > WebForm - Code-Behind (DisplayShowColor.aspx.vb)
    > ---------------------------------------------------------
    > Public Class DisplayShowColor
    > Inherits System.Web.UI.Page
    > Protected ctrlShowColor As myControls.ShowColor
    > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    > System.EventArgs) Handles MyBase.Load
    > ctrlShowColor.Text = "Hello Ethan"
    > ctrlShowColor.Color = "Orange"
    > End Sub
    > End Class
    >
    >
     
    Steve C. Orr [MVP, MCSD], Apr 1, 2004
    #2
    1. Advertising

  3. Technical Support

    Eric Guest

    I only wish it were that easy :)

    I did as you said and now am getting the following error:
    The base class includes the field 'ShowColor1', but its type
    (learn.myControls.ShowColor) is not compatible with the type of control
    (myControls.ShowColor).

    Any ideas?



    "Steve C. Orr [MVP, MCSD]" <> wrote in message
    news:...
    > Add the control to your toolbox, then drag the control from the toolbox

    onto
    > your form.
    > All the declarations should then happen automatically for you.
    >
    > --
    > I hope this helps,
    > Steve C. Orr, MCSD, MVP
    > http://Steve.Orr.net
    >
    >
    > "Technical Support" <> wrote in message
    > news:...
    > > Good afternoon,
    > > I created a custom control, compiled it into a DLL, and added it to the

    > BIN
    > > directory. I placed an instance of the control in a webForm and am

    trying
    > to
    > > programatically change its properties via the Page_Load subroutine in a
    > > code-behind file. Can someone please tell me the proper procedure for
    > > declaring the new custom control in the code-behind file? I have

    supplied
    > > all of the code associated with this very basic example and am using

    > VS.NET.
    > > The solution's name is "Learn"
    > >
    > > Thank you in advance.
    > >
    > > Eric
    > >
    > > Compiled Custom Control (showcolor.dll)
    > > -------------------------------------------
    > > Imports System
    > > Imports System.Web
    > > Imports System.Web.UI
    > >
    > > Namespace myControls
    > > Public Class ShowColor : Inherits Control
    > > Public Text As String
    > > Public Color As String
    > > Protected Overrides Sub Render(ByVal objTextWriter As
    > > HtmlTextWriter)
    > > objTextWriter.AddAttribute("Color", Color)
    > > objTextWriter.RenderBeginTag("Font")
    > > objTextWriter.Write(Text)
    > > objTextWriter.RenderEndTag()
    > > End Sub
    > > End Class
    > > End Namespace
    > > ++++++++++++++++++++++++++
    > >
    > > WebForm (DisplayShowColor.aspx)
    > > ---------------------------------------
    > > <%@ Page Language="vb" AutoEventWireup="false"
    > > Codebehind="DisplayShowColor.aspx.vb"

    Inherits="learn.DisplayShowColor"%>
    > > <%@ Register TagPrefix="myControls" Namespace="myControls"
    > > Assembly="ShowColor" %>
    > > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    > > <HTML>
    > > <HEAD>
    > > <title>DisplayShowColor</title>
    > > </HEAD>
    > > <body MS_POSITIONING="GridLayout">
    > > <TABLE height="39" cellSpacing="0" cellPadding="0" width="243"
    > > border="0" ms_2d_layout="TRUE">
    > > <TR vAlign="top">
    > > <TD>
    > > <myControls:ShowColor id="ctrlShowColor" Runat="server"

    />
    > > </TD>
    > > </TR>
    > > </TABLE>
    > > </body>
    > > </HTML>
    > > ++++++++++++++++++++++++++++++++++++++
    > >
    > > WebForm - Code-Behind (DisplayShowColor.aspx.vb)
    > > ---------------------------------------------------------
    > > Public Class DisplayShowColor
    > > Inherits System.Web.UI.Page
    > > Protected ctrlShowColor As myControls.ShowColor
    > > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    > > System.EventArgs) Handles MyBase.Load
    > > ctrlShowColor.Text = "Hello Ethan"
    > > ctrlShowColor.Color = "Orange"
    > > End Sub
    > > End Class
    > >
    > >

    >
    >
     
    Eric, Apr 1, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Miller [msft]

    Re: Code Behind vs. no code behind: error

    Ben Miller [msft], Jun 27, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    643
    Alphonse Giambrone
    Jun 28, 2003
  2. Guest
    Replies:
    1
    Views:
    362
    Alvin Bruney
    Oct 20, 2003
  3. Max
    Replies:
    7
    Views:
    3,617
    Kevin Spencer
    Jan 20, 2004
  4. John

    Non-code behind to code behind

    John, Feb 19, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    527
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Feb 19, 2007
  5. kwkahler
    Replies:
    0
    Views:
    129
    kwkahler
    Aug 4, 2005
Loading...

Share This Page