创建AJAX脚本控件

时间:2009-01-30 02:06:22

标签: c# asp.net ajax scriptcontrol

叫我'n00b',但我不熟悉创建脚本控件。我想用3个文本框创建一个简单的控件。我有一个.cs文件,如下所示:

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

我完全不知道为什么文本框不会渲染?我想我错过了什么......

[编辑]我得到的错误是“对象引用未设置为对象的实例”。

[ANSWERED]我按如下方式更改了文件,至少到目前为止它都有效。

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

2 个答案:

答案 0 :(得分:1)

你试过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();
}

答案 1 :(得分:1)

你在这里做的是构建一个复合控件,而不是脚本控件本身。您真正想要做的是继承CompositeControland following the model for creating a composite control),并实施IScriptControl。你要求你做很多胃灼热(ViewState和回发问题等)。