Textbox Custom Attribute

Discussion in 'ASP .Net Web Controls' started by Ryan, Dec 14, 2006.

  1. Ryan

    Ryan Guest

    Hello everyone,

    I am brand new to writing custom controls and I thought I would start
    with an easy one. Adding an attribute, OriginalValue, to a textbox.

    I got this together, and while stepping through it, the property is
    setting the viewstate properly with the given value, but on the
    postback I am getting empty quotes.

    I realize I should probably obtain a book and read further, but by
    following examples, it looks correct and I am at wits-end . . . here is
    the complete custom class. I get the Text value back properly, but not
    the OriginalValue.

    As you can see, I tried the AddAttributesToRender and I tried putting
    the RenderContents method in (seeing examples with and without it).
    None of this helped. I'm sure I am just missing something obvious. I
    thought by putting the values in viewstate, they would be available
    during a postback.


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

    <Assembly: TagPrefix("EBMS.Web.UI.WebControls", "EBMS")>

    <DefaultProperty("Text"), ToolboxData("<{0}:ValueChangedTextBox
    runat=server></{0}:ValueChangedTextBox>")> _
    Public Class ValueChangedTextBox
    Inherits TextBox
    Implements INamingContainer

    <Bindable(True), DefaultValue(""), Category("Appearance"),
    Localizable(True), Description("This stores the original value to be
    used in a comparison.")> Property OriginalValue() As String
    Get
    Dim strOriginalValue As String =
    CStr(ViewState("OriginalValue"))
    If strOriginalValue Is Nothing Then
    Return String.Empty
    Else
    Return strOriginalValue
    End If
    End Get

    Set(ByVal Value As String)
    ViewState("OriginalValue") = Value
    End Set
    End Property

    'Protected Overrides Sub AddAttributesToRender(ByVal writer As
    System.Web.UI.HtmlTextWriter)

    ' MyBase.AddAttributesToRender(writer)

    ' writer.AddAttribute("OriginalValue", OriginalValue)

    'End Sub

    'Protected Overrides Sub RenderContents(ByVal output As
    HtmlTextWriter)
    ' output.Write(OriginalValue)
    'End Sub

    End Class

    Thank you for any assistance. Have a nice evening,
    Ryan
    Ryan, Dec 14, 2006
    #1
    1. Advertising

  2. Ryan

    Ryan Guest

    Figured it out. Had to implement the IStateManager, then add the value
    to the statebag . . .

    Thanks anyway,
    Ryan




    Ryan wrote:
    > Hello everyone,
    >
    > I am brand new to writing custom controls and I thought I would start
    > with an easy one. Adding an attribute, OriginalValue, to a textbox.
    >
    > I got this together, and while stepping through it, the property is
    > setting the viewstate properly with the given value, but on the
    > postback I am getting empty quotes.
    >
    > I realize I should probably obtain a book and read further, but by
    > following examples, it looks correct and I am at wits-end . . . here is
    > the complete custom class. I get the Text value back properly, but not
    > the OriginalValue.
    >
    > As you can see, I tried the AddAttributesToRender and I tried putting
    > the RenderContents method in (seeing examples with and without it).
    > None of this helped. I'm sure I am just missing something obvious. I
    > thought by putting the values in viewstate, they would be available
    > during a postback.
    >
    >
    > Imports System
    > Imports System.Collections.Generic
    > Imports System.ComponentModel
    > Imports System.Text
    > Imports System.Web
    > Imports System.Web.UI
    > Imports System.Web.UI.WebControls
    >
    > <Assembly: TagPrefix("EBMS.Web.UI.WebControls", "EBMS")>
    >
    > <DefaultProperty("Text"), ToolboxData("<{0}:ValueChangedTextBox
    > runat=server></{0}:ValueChangedTextBox>")> _
    > Public Class ValueChangedTextBox
    > Inherits TextBox
    > Implements INamingContainer
    >
    > <Bindable(True), DefaultValue(""), Category("Appearance"),
    > Localizable(True), Description("This stores the original value to be
    > used in a comparison.")> Property OriginalValue() As String
    > Get
    > Dim strOriginalValue As String =
    > CStr(ViewState("OriginalValue"))
    > If strOriginalValue Is Nothing Then
    > Return String.Empty
    > Else
    > Return strOriginalValue
    > End If
    > End Get
    >
    > Set(ByVal Value As String)
    > ViewState("OriginalValue") = Value
    > End Set
    > End Property
    >
    > 'Protected Overrides Sub AddAttributesToRender(ByVal writer As
    > System.Web.UI.HtmlTextWriter)
    >
    > ' MyBase.AddAttributesToRender(writer)
    >
    > ' writer.AddAttribute("OriginalValue", OriginalValue)
    >
    > 'End Sub
    >
    > 'Protected Overrides Sub RenderContents(ByVal output As
    > HtmlTextWriter)
    > ' output.Write(OriginalValue)
    > 'End Sub
    >
    > End Class
    >
    > Thank you for any assistance. Have a nice evening,
    > Ryan
    Ryan, Dec 15, 2006
    #2
    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. Bostonasian
    Replies:
    1
    Views:
    810
    Joris Gillis
    Sep 18, 2005
  2. Dave
    Replies:
    0
    Views:
    445
  3. Joe Kovac
    Replies:
    2
    Views:
    928
    Joe Kovac
    May 2, 2007
  4. Bill Anderson

    Custom Component and Custom Textbox binding

    Bill Anderson, Dec 10, 2003, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    473
    Bill Anderson
    Dec 15, 2003
  5. John Wright

    Custom TextBox with custom attributes and properties question

    John Wright, Feb 27, 2004, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    195
    John Wright
    Mar 1, 2004
Loading...

Share This Page