locked
CreateChildControls sub RRS feed

  • Question

  • User-2001022558 posted

    Hi;

    I want to use from custom server control in my application.I Added a web control library project in my application.

    I write down code in sub createchildcontrols.

    Protected Overrides Sub CreateChildControls()

    Dim a As New TextBox

    a.Text = "reza"

    a.ID = Me.UniqueID

    Me.Controls.Add(a)

    Me.ChildControlsCreated = True

    End Sub

    but html code down render.

    <span id="Custom2"></span> 

    Why correct not render html code?

    Thanks,mohsen

    Tuesday, May 27, 2008 1:24 AM

Answers

  • User-667042492 posted

    hi mohsen,

    this is not mine code, its your? i only add line 4 (Me.Text = a.Text ) ..

    let's get back to the error, where do you use this control? in a custom grid? if so, you must give Id of textbox

    unique.. maybe you can use columnname+Me.UniquId..

    i tried this an aspx page, its works..

    also premier subject is how do you use this control in your page? in a grid? in an other control?

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 27, 2008 12:09 PM

All replies

  • User-667042492 posted

    hi mohsen,

    your code seems ok for CreateChildControls method.. did you try to debug ?

    i think there is a different problem.. Can you show your class ?

     

    Tuesday, May 27, 2008 3:29 AM
  • User-2001022558 posted

    hi mohsen,

    your code seems ok for CreateChildControls method.. did you try to debug ?

    i think there is a different problem.. Can you show your class ?

     

    My class is:

    Imports System

    Imports System.Collections.Generic

    Imports System.ComponentModel

    Imports System.Text

    Imports System.Web

    Imports System.Web.UI

    Imports System.Web.UI.WebControls

     

    <DefaultProperty("Text"), ToolboxData("<{0}:Custom runat=server></{0}:Custom>")> _

    Public Class Custom

    Inherits WebControl

    Implements INamingContainer

    <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

    Protected Overrides Sub RenderContents(ByVal output As HtmlTextWriter)

    output.Write(Text)

    End Sub

    Public Overrides ReadOnly Property UniqueID() As String

    Get

    Return MyBase.UniqueID

    End Get

    End Property

    Protected Overrides Sub CreateChildControls()

    Dim a As New TextBox

    a.Text = "reza"

    a.ID = Me.UniqueID

    Me.Controls.Add(a)

    Me.ChildControlsCreated = True

    End Sub

    Private Sub Custom_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init

    Me.EnsureChildControls()

    End Sub

    End Class

    Tuesday, May 27, 2008 7:24 AM
  • User-667042492 posted

    hi mohsen,

    if your code be like below, it works; 

    1    Protected Overrides Sub CreateChildControls()
    2            Dim a As New TextBox
    3            a.Text = "reza"
    4            a.ID = Me.UniqueID
    5            Me.Text = a.Text  // Here for set Text of custom control
    6            Me.Controls.Add(a)
    7            Me.ChildControlsCreated = True
    8        End Sub
    
    i hope this helps you..
    Tuesday, May 27, 2008 9:47 AM
  • User-2001022558 posted

    hi mohsen,

    if your code be like below, it works; 

    1    Protected Overrides Sub CreateChildControls()
    2            Dim a As New TextBox
    3            a.Text = "reza"
    4            a.ID = Me.UniqueID
    5            Me.Text = a.Text  // Here for set Text of custom control
    6            Me.Controls.Add(a)
    7            Me.ChildControlsCreated = True
    8        End Sub
    i hope this helps you..

    Hi;
    I write your code but  down exception  occur:
    Multiple controls with the same ID 'CustomGridView' were found. FindControl requires that controls have unique IDs. 
    
    Tuesday, May 27, 2008 10:45 AM
  • User-667042492 posted

    hi mohsen,

    this is not mine code, its your? i only add line 4 (Me.Text = a.Text ) ..

    let's get back to the error, where do you use this control? in a custom grid? if so, you must give Id of textbox

    unique.. maybe you can use columnname+Me.UniquId..

    i tried this an aspx page, its works..

    also premier subject is how do you use this control in your page? in a grid? in an other control?

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 27, 2008 12:09 PM