Frage

Rufen Sie mich an eine ‚n00b‘, aber ich bin neu auf die Schaffung Script Steuerelemente. Ich möchte eine einfache Steuerung mit 3 Textfelder erstellen. Ich habe eine CS-Datei, die wie folgt aussieht:

    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;
    }
}

und ich habe absolut keine Ahnung, warum werden nicht die Textbox machen? Ich glaube, ich bin etwas fehlt ...

[EDIT] Der Fehler Ich erhalte die „Objektverweis nicht auf eine Instanz eines Objekts festgelegt wird“.

[BEANTWORTET] ich die Datei wie folgt verändert, und es funktioniert, zumindest bisher.

   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();
    }
War es hilfreich?

Lösung

Haben Sie versucht, 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();
}

Andere Tipps

Was machst du denn hier ist der Aufbau einer Verbundsteuerung, kein Skript-Steuerung, per se. Was Sie wirklich wollen, ist zu tun, erben von Composite ( und nach dem Modell für eine Verbundsteuerung zu schaffen ) und die Implementierung IScriptControl statt . Sie sind für eine Menge von Sodbrennen fragen sie Ihren Weg machen (Viewstate und Postback Probleme, usw.).

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top