locked
Exposing Text property RRS feed

  • Question

  • User-1355361091 posted

    In my custom control I made a Text property to expose a label

    public string Text
    {
       get
       {
          return selectedItemsLabel.Text.Trim();
       }
    }

    And in the class definition I put

    [ValidationProperty("Text")]
    public class MyListBox : WebControl

     Now should that not let me access source.text in my client customvalidator javascript function?...keeps coming back as "undefined" when I do an alert...but I get [object] when I do an alert on the variable itself...so I assume it's found the darn thing.

    function RequiredField(source, arguments)
    {
       
    var text = document.getElementById(source.id.replace(/ReqValidator/, ''));
        alert(text.Text);
    }

    Wednesday, June 25, 2008 3:39 PM

Answers

  • User543031324 posted

    If you go with the first method of deriving from TextBox then the server will not know about anything else besides the TextBox, becuase you would just be telling the HtmlTextWriter to just send this HTML to the page along with the actual TextBox rendering (which is <input type="text" />) which is what I would do if you don't need any other Server Controls (ie a Button, ListBox and not a <div>).

     The second method tells the Validator to use the TextBox's ClientID when registering the javascript for validation. Which reminds me you are going to have to do the same thing with the ID property or the server side validation will fail.

    I would personally choose the first method.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 25, 2008 4:13 PM

All replies

  • User543031324 posted

    use the value property in javascript alert(text.value); There is no such property as Text in javascript.

    Wednesday, June 25, 2008 3:44 PM
  • User-1355361091 posted

    Ahhh...I figured since it didnt have a type it just inherited whatever the object had/has.

    Still returning undefined though

    Wednesday, June 25, 2008 3:51 PM
  • User543031324 posted

    Its showing as undefined because there is no <input type="text" /> with that id. When you put a validator on your custom control, it's taking the custom controls ID which is not the same as the TextBox inside the custom control. It looks like you are trying to create a custom drop down list or something. I would inherit from TextBox and then override its render method to make your drop down list (or whatever). This way the <input type="text" /> will have the same clientID as your custom control. You could also try overriding the ClientID of the custom control and returning the TextBox's ClientID.

    Wednesday, June 25, 2008 4:01 PM
  • User-1355361091 posted

    Ooooh, so when it renders, everything inside my custom control gets thrown onto the page "ungrouped" from their parent control then?

    Wednesday, June 25, 2008 4:06 PM
  • User543031324 posted

    If you go with the first method of deriving from TextBox then the server will not know about anything else besides the TextBox, becuase you would just be telling the HtmlTextWriter to just send this HTML to the page along with the actual TextBox rendering (which is <input type="text" />) which is what I would do if you don't need any other Server Controls (ie a Button, ListBox and not a <div>).

     The second method tells the Validator to use the TextBox's ClientID when registering the javascript for validation. Which reminds me you are going to have to do the same thing with the ID property or the server side validation will fail.

    I would personally choose the first method.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 25, 2008 4:13 PM