Question

Call me a 'n00b', but I am new to creating Script Controls. I want to create a simple control with 3 text boxes. I have a .cs file that looks like this:

    public class SmokingCalc : ScriptControl
    {
        public SmokingCalc()
        {
            Render(htmlWriter);
        }
        protected override void Render(HtmlTextWriter writer)
        {
            costTextbox.RenderControl(writer);
            base.Render(writer);
        }
        protected override IEnumerable<ScriptDescriptor>
                GetScriptDescriptors()
        {
            ScriptControlDescriptor descriptor = new ScriptControlDescriptor("SmokingCalc.SmokingCalc", this.ClientID);
            yield return descriptor;
        }

        // Generate the script reference
        protected override IEnumerable<ScriptReference>
                GetScriptReferences()
        {
            yield return new ScriptReference("SmokingCalc.SmokingCalc.js", this.GetType().Assembly.FullName);
        }

        protected HtmlTextWriter htmlWriter;
        protected TextBox costTextbox;
        protected TextBox amountTextbox;
        protected TextBox yearsTextbox;
        protected Button submitButton;
    }
}

and I have absolutely no idea why the textbox wont render? I think I am missing something...

[EDIT] The error I'm getting is the "Object reference is not set to an instance to an object".

[ANSWERED] I changed the file as follows, and it works, so far at least.

   public class SmokingCalc : ScriptControl
    {
        public SmokingCalc()
        {
        }
        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            costTextbox.ID = "costTextbox";
            amountTextbox.ID = "amountTextbox";
            yearsTextbox.ID = "yearsTextbox";
            submitButton.ID = "submitButton";
            submitButton.Text = "Submit";
            Controls.Add(costTextbox);
            Controls.Add(amountTextbox);
            Controls.Add(yearsTextbox);
            Controls.Add(submitButton);
        }
        protected override IEnumerable<ScriptDescriptor>
                GetScriptDescriptors()
        {
            ScriptControlDescriptor descriptor = new ScriptControlDescriptor("SmokingCalc.SmokingCalc", this.ClientID);
            descriptor.AddProperty("costTextboxID", costTextbox.ClientID);
            descriptor.AddProperty("amountTextboxID", amountTextbox.ClientID);
            descriptor.AddProperty("yearsTextboxID", amountTextbox.ClientID);
            descriptor.AddProperty("submitButtonID", submitButton.ClientID);
            yield return descriptor;
        }

        // Generate the script reference
        protected override IEnumerable<ScriptReference>
                GetScriptReferences()
        {
            yield return new ScriptReference("SmokingCalc.SmokingCalc.js", this.GetType().Assembly.FullName);
        }

        protected TextBox costTextbox = new TextBox();
        protected TextBox amountTextbox = new TextBox();
        protected TextBox yearsTextbox = new TextBox();
        protected Button submitButton = new Button();
    }
Was it helpful?

Solution

Did you try CreateChildControls?:

public class SmokingCalc : ScriptControl
{
    protected override void CreateChildControls()
    {
        this.Controls.Add(costTextbox);
    }

    protected override IEnumerable<ScriptDescriptor>
            GetScriptDescriptors()
    {
        ScriptControlDescriptor descriptor = new ScriptControlDescriptor("SmokingCalc.SmokingCalc", this.ClientID);
        yield return descriptor;
    }

    // Generate the script reference
    protected override IEnumerable<ScriptReference>
            GetScriptReferences()
    {
        yield return new ScriptReference("SmokingCalc.SmokingCalc.js", this.GetType().Assembly.FullName);
    }

    protected HtmlTextWriter htmlWriter;
    protected TextBox costTextbox = new TextBox();
    protected TextBox amountTextbox = new TextBox();
    protected TextBox yearsTextbox = new TextBox();
    protected Button submitButton = new Button();
}

OTHER TIPS

What you are doing here is building a composite control, not a script control, per se. What you really want to be doing is inheriting from CompositeControl (and following the model for creating a composite control), and implementing IScriptControl, instead. You're asking for a lot of heartburn doing it your way (ViewState and postback problems, etc.).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top