Note that there are some explanatory texts on larger screens.

plurals
  1. POUnable to gain access to custom textbox property through javascript
    text
    copied!<p>I have made a custom textbox that inherits from textbox.</p> <pre><code> using System.Drawing; using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace GNB.DPS.MVAD.CustomWebControls { [Designer("GNB.DPS.MVAD.CustomWebControls.MVADTextBox, GNB.DPS.MVAD.CustomWebControls"), DefaultProperty("Text"), ToolboxData("&lt;{0}:MVADTextBox runat=server&gt;&lt;/{0}:MVADTextBox&gt;")] public class MVADTextBox : TextBox { public virtual string SampleText { get { string s = (string)ViewState["SampleText"]; return s ?? String.Empty; } set { ViewState["SampleText"] = value; } } [Bindable(false)] [Category("Properties")] [DefaultValue("")] [Localizable(true)] public string Mask { get { String tblName = (String)ViewState["Mask"]; return (Mask ?? String.Empty); } set { ViewState["Mask"] = value; } } protected override void OnLoad(EventArgs e) { if(!Page.IsPostBack) { if(Text == String.Empty) { if (SampleText != "") { CssClass = "sampleText"; this.Text = SampleText; var onFocus = "&lt;script language=\"javascript\"&gt;function ClearField(input) { if(input.value == input.defaultValue){input.value = \"\"; input.className = 'regularText';} } &lt;/script&gt;"; var onBlur = "&lt;script language=\"javascript\"&gt; function PopulateField(input) {if (input.value == \"\") {input.value = input.defaultValue; input.className = 'sampleText'; } } &lt;/script&gt;"; Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OnFocus", onFocus); this.Attributes.Add("onfocus", "ClearField(this);"); Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OnBlur", onBlur); this.Attributes.Add("onblur", "PopulateField(this);"); } } } } } } </code></pre> <p>On the .aspx it can be used as such...</p> <pre><code>&lt;script language="javascript"&gt; function MaskInput(input) { var textbox = document.getElementById(input.id); var mask = textbox.getAttribute('Mask'); } &lt;/script&gt; &lt;MVAD:MVADTextBox ID="tbMVAD2" runat="server" SampleText="Hello" Mask="###-###" onKeyPress="MaskInput(this);" /&gt; </code></pre> <p>However... this line returns null var mask = textbox.getAttribute('Mask');</p> <p>in the view source we see...</p> <pre><code>&lt;input name="ctl00$cphListBody$tbMVAD2" type="text" value="Hello" id="ctl00_cphListBody_tbMVAD2" class="sampleText" onKeyPress="MaskInput(this);" onfocus="ClearField(this);" onblur="PopulateField(this);" /&gt; </code></pre> <p>notice how there is no property in the source called Mask or Sample Text. Therefore I can't access the property through javascript. Is my control set up properly? Any Ideas?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload