Knowledgebase Home Page  >  RapidSpell Web ASP.NET  >  Misc
Search the Knowledge Base
Using RapidSpellWInline as a child control inside a Server Control (with RapidSpellWebMultiple). (VB.NET)
https://keyoti.com/kb/Default.aspx?ToDo=view&questId=158&catId=60

Options

Print this page
Email this to a friend

It may be desirable to wrap the RapidSpellWInline control inside another Server Control, as a child control.  This is slightly more difficult than a User Control, but creates a reusable control inside a DLL.

Overview

Creating a Server Control with RapidSpellWInline as a child control is mostly standard practice, as it would be when using any other Server Control as a child control.  The child control is created in CreateChildControls and added to the Controls collection.  If a RapidSpellWebMultiple is used on the page that the new composite control (we'll call SpellcheckTextBoxGroup) is used on, then it will be necessary to setup the RapidSpellWebMultiple.RapidSpellWebLaunchers property in the codebehind for the page.  This also relates to a consideration that must be made;

The RapidSpellWebMultiple control needs to receive the IDs of the RapidSpellWInline controls and to find those controls, therefore the CreateChildControls method in SpellcheckTextBoxGroup must be called before the Page_Load method is called in the page's codebehind.  I.e. the RapidSpellWInline needs to be created before the RapidSpellWebLaunchers property is set.  To make sure this happens, call EnsureChildControls in the Init event handler, in the control;

    'Important to call EnsureChildControls at this point
    Private Sub SpellCheckTextBoxGroup_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        EnsureChildControls()
    End Sub

 

Complete Example Code

ASPX Page - default.aspx

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<%@ Register Assembly="Keyoti.RapidSpellWeb.ASP.NETv2" Namespace="Keyoti.RapidSpell"
    TagPrefix="RapidSpellWeb" %>

<%@ Register Assembly="WindowsControlLibrary1" Namespace="WindowsControlLibrary1"
    TagPrefix="cc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Panel ID="Panel1" runat="server" Height="50px" Width="125px">
        <cc1:SpellCheckTextBoxGroup ID="SpellCheckTextBoxGroup1" runat="server" RapidSpellWInlineHelperPage="rswihelper.aspx" />
        <cc1:SpellCheckTextBoxGroup ID="SpellCheckTextBoxGroup2" runat="server" RapidSpellWInlineHelperPage="rswihelper.aspx" />
        </asp:Panel>
        <RapidSpellWeb:RapidSpellWebMultiple ID="RapidSpellWebMultiple1"  runat="server" RapidSpellWebLaunchers="Panel1">
            <Button runat="server" BackColor="" BorderColor="" BorderStyle="NotSet" BorderWidth=""
                CssClass="" Enabled="True" ForeColor="" Height="" TabIndex="0" type="button"
                Value="Check Spelling" Width="" />
        </RapidSpellWeb:RapidSpellWebMultiple>
           
    </div>
    </form>
</body>
</html>

Codebehind - default.aspx.vb

Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  'Add the inline controls to the RapidSpellWebMultiple
        RapidSpellWebMultiple1.RapidSpellWebLaunchers.Add(SpellCheckTextBoxGroup1.RSWInLine.ClientID)
        RapidSpellWebMultiple1.RapidSpellWebLaunchers.Add(SpellCheckTextBoxGroup2.RSWInLine.ClientID)

    End Sub

End Class

 

Control class - SpellCheckTextboxGroup.vb

(This control class should be housed in a separate project that creates a control library.)

Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Text
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports Keyoti.RapidSpell

<DefaultProperty("Text"), ToolboxData("<{0}:SpellCheckTextBoxGroup runat=server></{0}:SpellCheckTextBoxGroup>")> _
Public Class SpellCheckTextBoxGroup
    Inherits WebControl
    Implements IPostBackDataHandler


    Public Event TextChanged(ByVal Sender As Object, ByVal e As EventArgs)
    Dim lbl As New Label
    Dim txt As New RapidSpellWInlineTextBox
    Dim rs As New RapidSpellWInline
    Dim val As New RequiredFieldValidator

    Public Enum StatusCodes
        Invisible
        [ReadOnly]
        Required
        [Optional]
    End Enum

    <Bindable(True), Category("Appearance"), DefaultValue(""), Localizable(True)> Property Text() As String
        Get
            Dim s As String = 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
            If Not txt Is Nothing Then txt.Text = Value
        End Set
    End Property

    <Bindable(True), Category("Appearance"), DefaultValue("Label"), Localizable(True)> Property LabelText() As String
        Get
            Dim s As String = ViewState("LabelText")
            If s Is Nothing Then
                Return String.Empty
            Else
                Return s
            End If
        End Get

        Set(ByVal Value As String)
            ViewState("LabelText") = Value
            If Not lbl Is Nothing Then lbl.Text = Value
        End Set
    End Property

    <Bindable(True), Category("Appearance"), DefaultValue(""), Localizable(True)> Property ValidationText() As String
        Get
            Dim s As String = ViewState("ValidationText")
            If s Is Nothing Then
                Return String.Empty
            Else
                Return s
            End If
        End Get

        Set(ByVal Value As String)
            ViewState("ValidationText") = Value
            If Not val Is Nothing Then val.ErrorMessage = Value
        End Set
    End Property

    <Bindable(True), Category("Appearance"), DefaultValue(200), Localizable(True)> Overrides Property Width() As Unit
        Get
            Return ViewState("Width")
        End Get

        Set(ByVal Value As Unit)
            ViewState("iWdth") = Value
            If Not lbl Is Nothing Then
                lbl.Width = Value
                txt.Width = Value
                val.Width = Value
            End If
        End Set
    End Property

    <Bindable(True), Category("Appearance"), DefaultValue(200), Localizable(True)> Property Rows() As Integer
        Get
            Return ViewState("Rows")
        End Get

        Set(ByVal Value As Integer)
            ViewState("Rows") = Value
            If Not txt Is Nothing Then
                txt.Height = Value
            End If
        End Set
    End Property

    <Bindable(True), Category("Appearance"), DefaultValue(200), Localizable(True)> Property Columns() As Integer
        Get
            Return ViewState("Columns")
        End Get

        Set(ByVal Value As Integer)
            ViewState("Columns") = Value
            If Not txt Is Nothing Then
                txt.Width = Value
            End If
        End Set
    End Property

    <Category("Appearance"), DefaultValue(False), Localizable(True)> Property AutoPostback() As Boolean
        Get
            Return ViewState("AutoPostback")
        End Get

        Set(ByVal Value As Boolean)
            ViewState("AutoPostback") = Value
            If Not txt Is Nothing Then
                txt.AutoPostBack = Value
            End If
        End Set
    End Property

    <Category("Appearance"), DefaultValue(False), Localizable(True)> Property Multiline() As Boolean
        Get
            Return ViewState("Multiline")
        End Get

        Set(ByVal Value As Boolean)
            ViewState("Multiline") = Value
            If Not txt Is Nothing Then
                If Value Then txt.TextMode = TextBoxMode.MultiLine Else txt.TextMode = TextBoxMode.SingleLine
            End If
        End Set
    End Property

    <Category("Appearance"), DefaultValue(False), Localizable(True)> Property RapidSpellWInlineHelperPage() As String
        Get
            Dim s As String = ViewState("RapidSpellWInlineHelperPage")
            If s Is Nothing Then
                Return String.Empty
            Else
                Return s
            End If
        End Get

        Set(ByVal Value As String)
            ViewState("RapidSpellWInlineHelperPage") = Value
            If Not rs Is Nothing Then
                rs.RapidSpellWInlineHelperPage = Value
            End If
        End Set
    End Property

    ReadOnly Property RapidSpellWInline() As RapidSpellWInline
        Get
            Return rs
        End Get

    End Property

    <Category("Appearance"), DefaultValue(False), Localizable(True)> Property ShowButton() As Boolean
        Get
            Return ViewState("ShowButton")
        End Get

        Set(ByVal Value As Boolean)
            ViewState("ShowButton") = Value
            If Not rs Is Nothing Then
                rs.ShowButton = Value
            End If
        End Set
    End Property

    <Category("Appearance"), DefaultValue(False), Localizable(True)> Property RightClickForMenu() As Boolean
        Get
            Return ViewState("RightClickForMenu")
        End Get

        Set(ByVal Value As Boolean)
            ViewState("RightClickForMenu") = Value
            If Not rs Is Nothing Then
                rs.RightClickForMenu = Value
            End If
        End Set
    End Property

    <Category("Appearance"), DefaultValue(False), Localizable(True)> Property TextComponentID() As String
        Get
            Dim s As String = ViewState("TextComponentID")
            If s Is Nothing Then
                Return String.Empty
            Else
                Return s
            End If
        End Get

        Set(ByVal Value As String)
            ViewState("TextComponentID") = Value
            If Not rs Is Nothing Then
                rs.TextComponentID = Value
            End If
        End Set
    End Property

    <Category("Appearance"), DefaultValue(False), Localizable(True)> Property RSMultipleID() As String
        Get
            Dim s As String = ViewState("RSMultipleID")
            If s Is Nothing Then
                Return String.Empty
            Else
                Return s
            End If
        End Get

        Set(ByVal Value As String)
            ViewState("RSMultipleID") = Value
            If Not rs Is Nothing Then
                rs.RSMultipleID = Value
            End If
        End Set
    End Property


    <Browsable(False)> ReadOnly Property RSWInLine() As RapidSpellWInline
        Get
            Return rs
        End Get
    End Property

    <Category("Appearance"), DefaultValue(StatusCodes.Optional), Localizable(True)> Property Status() As StatusCodes
        Get
            Return ViewState("Status")
        End Get

        Set(ByVal Value As StatusCodes)
            ViewState("Status") = Value
            If Not txt Is Nothing Then
                SetStatus(Value)
            End If
        End Set
    End Property

    Protected Sub SetStatus(ByVal stat As StatusCodes)
        Select Case stat
            Case StatusCodes.Invisible 'invisible
                txt.Visible = False
                lbl.Visible = False
                val.Visible = False
                val.Enabled = False
            Case StatusCodes.ReadOnly   'read-only
                txt.CssClass = "rofield"
                txt.ReadOnly = True
                lbl.CssClass = "roflabel"
                val.Enabled = False
            Case StatusCodes.Required   'required
                txt.CssClass = "requiredfield"
                lbl.CssClass = "rflabel"
                val.Enabled = True
            Case StatusCodes.Optional   'optional
                txt.CssClass = "optionalfield"
                lbl.CssClass = "oflabel"
                val.Enabled = False
        End Select
    End Sub

    Protected Overrides Sub CreateChildControls()
        lbl.ID = Me.UniqueID & "_lbl"
        txt.ID = Me.UniqueID & "_txt"
        val.ID = Me.UniqueID & "_val"
        rs.ID = Me.UniqueID & "_rs"
        val.ControlToValidate = txt.ID

        Me.Controls.Add(lbl)
        Me.Controls.Add(txt)
        Me.Controls.Add(rs)
        Me.Controls.Add(val)

 

    End Sub

    Protected Overrides Sub Render(ByVal output As HtmlTextWriter)
        output.Write("<div style=""width:" & Me.Width.ToString() & ";height:" & Me.Height.ToString() & ";")
        For Each key As String In Me.Style.Keys
            output.Write(key & ":" & Me.Style.Item(key) & ";")
        Next
        output.Write(""">")
        lbl.RenderControl(output)
        output.Write("<br />")
        txt.RenderControl(output)
        output.Write("<br />")
        rs.RenderControl(output)
        output.Write("<br />")
        val.ControlToValidate = txt.ID
        val.RenderControl(output)
        output.Write("</div>")
    End Sub

    Public Function LoadPostData(ByVal postDataKey As String, ByVal postCollection As System.Collections.Specialized.NameValueCollection) As Boolean Implements System.Web.UI.IPostBackDataHandler.LoadPostData
        Dim presentValue As String = Text
        Dim postedValue As String = postCollection(postDataKey & "_txt")
        Text = postedValue
        If Not presentValue.Equals(postedValue) Then
            Return True
        End If
        Return False

    End Function

 

    Public Sub RaisePostDataChangedEvent() Implements System.Web.UI.IPostBackDataHandler.RaisePostDataChangedEvent
        RaiseEvent TextChanged(Me, New EventArgs)
    End Sub

    Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs)
        If Not Page Is Nothing Then
            rs.TextComponentID = txt.ClientID 'Wire up the spell checker to the textbox

            Page.RegisterRequiresPostBack(Me)
        End If
    End Sub


    'Important to call EnsureChildControls at this point
    Private Sub SpellCheckTextBoxGroup_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        EnsureChildControls()
    End Sub

End Class

 

 

 


Related Questions:

Attachments:

No attachments were found.