"Error Creating Control" and "Cast from String"

Ask a Question related to ASP.NET Building Controls, Design and Development.

  1. #1

    Default "Error Creating Control" and "Cast from String"

    I'm creating a custom date control. In appearance, it's just a
    textbox and a button. It has three custom properties: CalDate,
    CalDateType and Required. Required is just a boolean, and CalDate is
    the contents of the textbox. CalDateType is an enum with four values:
    CalDate, Today, Jan_01_1900, and This_Year_Start. If it's CalDate, it
    leaves CalDate alone; otherwise, it sets CalDate to the chosen value.

    I'm having two problems. When I put the control into a test page, I
    get a rectangle that says "Error Creating Control". When I mouse over
    the block, it says "Cast from String". It doesn't say what I'm
    improperly casting from a string, and the control compiles just fine.
    The other problem, which is more minor, is that the control I drag
    into the page is given the name LLCal2. I've never seen a control
    that implements INamingContainer start with 2.

    Here is the code:

    Imports System
    Imports System.IO
    Imports System.ComponentModel
    Imports System.Web
    Imports System.Web.UI
    Imports System.Web.UI.WebControls
    Imports System.Web.UI.HtmlControls

    <DefaultProperty("CalDate"), ToolboxData("<{0}:LLCal
    runat=server></{0}:LLCal>")> _
    Public Class LLCal
    Inherits System.Web.UI.WebControls.WebControl
    Implements INamingContainer

    Private DateBox As TextBox

    Public Enum CalDateTypes
    CalDate
    Today
    This_Year_Start
    Jan_01_1900
    End Enum

    Public Sub New(ByVal sCalDateType As String, ByVal bRequired As
    Boolean)
    DateBox = New TextBox

    ViewState("CalDateType") = sCalDateType
    ViewState("Required") = bRequired

    Select Case ViewState("CalDateType")
    Case CalDateTypes.Jan_01_1900.ToString
    DateBox.Text = "01/01/1900"
    Case CalDateTypes.This_Year_Start.ToString
    DateBox.Text = "01/01/" & CType(Year(Now), String)
    Case CalDateTypes.Today.ToString
    DateBox.Text = Now.ToString("MM/dd/yyyy")
    End Select

    End Sub

    Public Sub New()
    DateBox = New TextBox
    ViewState("CalDateType") = CalDateTypes.CalDate.ToString
    ViewState("Required") = False
    End Sub

    Protected Overrides Sub OnPreRender(ByVal e As EventArgs)
    MyBase.OnPreRender(e)
    RegisterScript()
    End Sub

    Protected Overridable Sub RegisterScript()
    If Not Page.IsClientScriptBlockRegistered("LLCal_vbs") Then
    Dim reader As New
    System.IO.StreamReader(Me.GetType().Assembly.GetMa nifestResourceStream(Me.GetType(),
    "LLCal.vbs"))
    Dim script As String = "<script language='vbscript'
    type='text/vbscript' >" + ControlChars.Cr + ControlChars.Lf + "<!--" +
    ControlChars.Cr + ControlChars.Lf + reader.ReadToEnd() +
    ControlChars.Cr + ControlChars.Lf + "//-->" + ControlChars.Cr +
    ControlChars.Lf + "</script>"
    Page.RegisterClientScriptBlock("LLCal_vbs", script)
    End If
    End Sub

    <Bindable(True), Browsable(True), Category("Appearance"),
    DefaultValue(""), Description("The date which appears in the
    textbox"), DesignerSerializationVisibility(DesignerSerializat ionVisibility.Content)>
    _
    Public Property CalDate() As String
    Get
    EnsureChildControls()
    Return ViewState("CalDate")
    End Get
    Set(ByVal Value As String)
    If Not IsDate(Value) Then
    EnsureChildControls()
    DateBox.Text = ""
    ViewState("CalDate") = ""
    Else
    EnsureChildControls()
    DateBox.Text = CType(Value,
    Date).ToString("MM/dd/yyyy")
    ViewState("CalDate") = CType(Value,
    Date).ToString("MM/dd/yyyy")
    End If
    End Set
    End Property

    <Bindable(True), Browsable(True), Category("Appearance"),
    DefaultValue(CalDateTypes.CalDate), Description("Default Value Type
    for DateBox"), DesignerSerializationVisibility(DesignerSerializat ionVisibility.Content)>
    _
    Public Property CalDateType() As CalDateTypes
    Get
    Return ViewState("CalDateType")
    End Get
    Set(ByVal Value As CalDateTypes)
    ViewState("CalDateType") = Value.ToString
    Select Case Value
    Case CalDateTypes.Jan_01_1900
    DateBox.Text = "01/01/1900"
    Case CalDateTypes.This_Year_Start
    DateBox.Text = "01/01/" & CType(Year(Now), String)
    Case CalDateTypes.Today
    DateBox.Text = Now.ToString("MM/dd/yyyy")
    End Select
    End Set
    End Property

    <Bindable(True), Browsable(True), Category("Misc"),
    DefaultValue(False), Description("Whether the datebox must have a
    value or not"), DesignerSerializationVisibility(DesignerSerializat ionVisibility.Content)>
    _
    Public Property Required() As Boolean
    Get
    Return ViewState("Required")
    End Get
    Set(ByVal Value As Boolean)
    ViewState("Required") = Value
    End Set
    End Property

    Public Overrides ReadOnly Property Controls() As ControlCollection
    Get
    EnsureChildControls()
    Return MyBase.Controls
    End Get
    End Property

    Protected Overrides Sub CreateChildControls()

    Controls.Clear()

    Dim MyImage As System.Web.UI.WebControls.Image
    Dim MyLabel As Label
    Dim MyTable As Table
    Dim MyRow As TableRow
    Dim MyCell As TableCell

    '<span style="font-size:6;background-color:silver;border:outset
    2" onclick="showCalHTML(document.myform.reportdate2, 4)"
    name=choosereportdate2 id=choosereportdate2
    onmousedown="this.style.borderStyle='inset'"
    onmouseup="this.style.borderStyle='outset'"><img src="LLCal.gif"
    border="0">&nbsp;</span>

    ' DateBox = New TextBox
    DateBox.ID = "DateBox"
    DateBox.Width = System.Web.UI.WebControls.Unit.Pixel(80)
    DateBox.Attributes("RunAt") = "server"
    DateBox.Attributes("OnBlur") = "me.value =
    CorrectDate(me.value)"
    DateBox.Attributes("OnFocus") = "me.select()"
    DateBox.Text = ViewState("Value")
    DateBox.EnableViewState = True

    MyLabel = New Label
    MyLabel.ID = "DateButton"
    MyLabel.Attributes("style") = "font-size:6;
    background-color:silver; border:outset 2"
    MyLabel.Attributes("onmouseup") =
    "me.style.borderStyle='Outset'"
    MyLabel.Attributes("onmousedown") =
    "me.style.borderStyle='Inset'"
    MyLabel.Attributes("onclick") = "showCal(" & Me.ClientID &
    "_DateBox" & ")"
    MyLabel.Attributes("align") = "absMiddle"

    MyImage = New System.Web.UI.WebControls.Image

    If Not System.IO.File.Exists("C:\temp\LLCal.gif") Then
    Dim LLCal_gif() As System.Byte
    Dim myAssembly As System.Reflection.Assembly =
    System.Reflection.Assembly.GetExecutingAssembly
    Dim myStream As Stream =
    myAssembly.GetManifestResourceStream("LLCal.LLCal. gif")
    LLCal_gif = New System.Byte(myStream.Length) {}
    myStream.Read(LLCal_gif, 0, CInt(myStream.Length))
    Dim myTempFile As New FileStream("C:\temp\LLCal.gif",
    FileMode.Create)
    myTempFile.Write(LLCal_gif, 0, CInt(myStream.Length))
    myTempFile.Close()
    End If

    MyImage.ImageUrl = "C:\temp\LLCal.gif"
    MyLabel.Controls.Add(MyImage)
    MyLabel.Controls.Add(New LiteralControl("&nbsp;"))

    MyTable = New Table
    MyTable.BorderStyle = BorderStyle.None
    MyTable.CellPadding = 0
    MyTable.CellSpacing = 0

    MyCell = New TableCell
    MyCell.Attributes("style") = "padding:0"
    MyCell.Controls.Add(DateBox)

    MyRow = New TableRow
    MyRow.Cells.Add(MyCell)

    MyCell = New TableCell
    MyCell.Attributes("style") = "padding:0"
    MyCell.Controls.Add(MyLabel)

    MyRow.Cells.Add(MyCell)

    MyTable.Rows.Add(MyRow)

    Controls.Add(MyTable)

    End Sub

    Protected Overrides Sub Render(ByVal output As
    System.Web.UI.HtmlTextWriter)
    Me.EnsureChildControls()
    RenderContents(output)
    End Sub

    End Class

    LLCal.gif, LLCal.bmp and LLCal.vbs are all in the assembly, so that I
    can have the control be completely self-contained. Am I missing
    something obvious here?

    Thanks in advance,
    Lisa
    Lisa Guest

  2. Similar Questions and Discussions

    1. #37773 [Asn->Csd]: iconv_substr() gives "Unknown error" when string length = 1"
      ID: 37773 Updated by: iliaa@php.net Reported By: dave at dgx dot cz -Status: Assigned +Status: Closed Bug Type: ICONV...
    2. #37773 [Ver->Asn]: iconv_substr() gives "Unknown error" when string length = 1"
      ID: 37773 Updated by: tony2001@php.net Reported By: dave at dgx dot cz -Status: Verified +Status: Assigned Bug Type: ...
    3. #39018 [NEW]: Error control operator '@' fails to suppress "Uninitialized string offset"
      From: mpb dot mail at gmail dot com Operating system: Gentoo Linux PHP version: 5.1.6 PHP Bug Type: Scripting Engine problem...
    4. #26292 [Opn->Bgs]: substr returns "0" for any offset on the string "0"
      ID: 26292 Updated by: sniper@php.net Reported By: ravacholp at hotmail dot com -Status: Open +Status: ...
    5. convert visual basic "string" data type to DB2 "blob"
      Does anyone know if a visual basic string data type can be converted to DB2 blob datatype? I have all data in XML files and I use Visual Basic to...
  3. #2

    Default Re: "Error Creating Control" and "Cast from String"

    "Lisa" <lisa@starways.net> wrote in message
    news:cc62d1fa.0408120558.4a9cd2c@posting.google.co m...
    > I'm creating a custom date control. In appearance, it's just a
    > textbox and a button. It has three custom properties: CalDate,
    > CalDateType and Required. Required is just a boolean, and CalDate is
    > the contents of the textbox. CalDateType is an enum with four values:
    > CalDate, Today, Jan_01_1900, and This_Year_Start. If it's CalDate, it
    > leaves CalDate alone; otherwise, it sets CalDate to the chosen value.
    >
    > I'm having two problems. When I put the control into a test page, I
    > get a rectangle that says "Error Creating Control". When I mouse over
    > the block, it says "Cast from String". It doesn't say what I'm
    > improperly casting from a string, and the control compiles just fine.
    Lisa, I don't see any exception handling in the code you supplied, so it's
    not surprising that there is no indication of where the "cast from string"
    comes from.

    What happens at runtime when you use this control on a page? Hopefully, the
    "cast from string" exception will happen at runtime as well, and you'll be
    able to see what's causing it.

    The alternative requires a little work creating your own designer, so that
    you can control what happens at design-time. I don't have an example handy,
    but the basic idea is to derive your own designer from ControlDesigner (or
    whatever the default designer is for the control your control derives from).
    Then, override the GetDesignTimeHtml method like this:

    namespace <yourControlsNamespace>.Design
    {
    public class MyControlsDesigner : ControlDesigner
    {
    protected override string GetDesignTimeHtml()
    {
    try
    {
    return base.GetDesignTimeHtml();
    }
    catch (Exception ex)
    {
    return CreatePlaceHolderDesignTimeHtml(ex.ToString());
    }
    }
    }
    }

    Then specify this class as the designer for your control using the
    DesignerAttribute:

    [Designer(typeof(Design.MyControlsDesigner))]
    public class MyControl : Control {}
    --
    John Saunders
    johnwsaundersiii at hotmail


    John Saunders Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139