locked
custom control error RRS feed

  • Question

  • User1039481381 posted

    I am writing a custom control and calling in javascript dynamically to make any one of textbox disable in javascript code in this custom control.

    I am not sure how I can reference this custom control in asp.net page. when I tried to reference the javascript custom control that I have written in aspx page, I get runtime error

    Unknown server tag 'cc1:CustomControl'.

    I know I only have pieces of code and no idea how I can call this javascript custom control from aspx page. please help. here is the code part that i currently have.

    AddressControl.cs
    ----
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace AddressControl
    {
        [DefaultProperty("Text")]
        [ToolboxData("<{0}:CustomControl  runat=server></{0}:CustomControl >")]
        public class AddressControl : TextBox
        {
    
            private TextBox m_cityText = new TextBox();
            private TextBox m_townshipText = new TextBox();
    
    
            protected override void OnPreRender(EventArgs e)
            {
                DisableCityorTownship();
    
                base.OnPreRender(e);
            }
    
            //newcode new function
            /// <summary>
            /// Register javascript to disable the township or city
            /// </summary>
            private void DisableCityorTownship()
            {
                String script = "DisableTextBox" + this.ClientID;
                Type csType = this.GetType();
    
                StringBuilder sb = new StringBuilder();
                sb.AppendFormat("function DisableTextBox{0}()", this.ClientID);
                sb.Append(" { ");
                //sb.Append(Environment.NewLine);
                sb.AppendFormat("var townShipID = document.getElementById('{0}');", this.m_townshipText.ClientID);
                // sb.Append(Environment.NewLine);
                sb.AppendFormat("var cityID = document.getElementById('{0}');", this.m_cityText.ClientID);
                //sb.Append(Environment.NewLine);
                sb.AppendFormat("if (townShipID.value.length >= 1) ");
                sb.Append("{ ");
                sb.AppendFormat("cityID.disabled = true;");
                sb.Append("} ");
                sb.AppendFormat("else if (cityID.value.length >= 1) ");
                sb.Append("{ ");
                sb.AppendFormat("townShipID.disabled = true;");
                sb.Append("}");
                sb.Append("}");
    
              
    
                ScriptManager.RegisterClientScriptBlock(this, csType, script, sb.ToString(), true);
            }
    
        }
    }
    
    
    default.aspx
    ------
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        
            
            <cc1:CustomControl runat="server"></cc1:CustomControl>
        </div>
        </form>
    </body>
    </html>
    
    
    Saturday, December 30, 2017 4:02 PM

All replies

  • User1039481381 posted

    Hi - please help me on how would I render two texboxes in custom control and how would I reference the aspx page to use this custom control. that will be very helpful. thanks.

    Saturday, December 30, 2017 4:33 PM
  • User1039481381 posted

    Hi - I have a custom control that has two textbox fileds in it. I need to write a javascript dynamic script in to this custom control to disable one textbox when we start enter data in other textbox and viceversa.

    I have written dynamic javascript as this in custom control. ut I am not sure how I can make this javascript function to render with a disabled textbox in browser. If you could show me a coding example of this that would be helpful. thanks for the help in advance.

    This is the code I have currently 

    protected override void OnPreRender(EventArgs e)
            {
                DisableCityorTownship();
    
                base.OnPreRender(e);
            }
     private void DisableCityorTownship()
            {
                String script = "DisableTextBox" + this.ClientID;
                Type csType = this.GetType();
    
                StringBuilder sb = new StringBuilder();
                sb.AppendFormat("function DisableTextBox{0}()", this.ClientID);
                sb.Append(" { ");
                //sb.Append(Environment.NewLine);
                sb.AppendFormat("var townShipID = document.getElementById('{0}');", this.m_townshipText.ClientID);
                // sb.Append(Environment.NewLine);
                sb.AppendFormat("var cityID = document.getElementById('{0}');", this.m_cityText.ClientID);
                //sb.Append(Environment.NewLine);
                sb.AppendFormat("if (townShipID.value.length >= 1) ");
                sb.Append("{ ");
                sb.AppendFormat("cityID.disabled = true;");
                sb.Append("} ");
                sb.AppendFormat("else if (cityID.value.length >= 1) ");
                sb.Append("{ ");
                sb.AppendFormat("townShipID.disabled = true;");
                sb.Append("}");
                sb.Append("}");
    
    
                
                ScriptManager.RegisterClientScriptBlock(this, csType, script, sb.ToString(), true);
            }
    
     protected override void RenderContents(HtmlTextWriter output)
            {
                //how can I render  javascript function (that is DisableCityorTownship) disabled textboxes .plese help
            }
    Saturday, December 30, 2017 11:47 PM
  • User632428103 posted

    Hello madhan1,

    it was more than 3 years i don't have any custom control ...

    but read this article

    http://jasongaylord.com/blog/embed-javascript-in-custom-aspnet-server-controls

    And if i have your problem i'm just try to run the custom control and on page load just fire an alert message  ...

    tell me if you continue to have some problem, i m sure i can remenber how to build custom control :)

    Monday, January 8, 2018 11:28 AM