题
叫我“的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();
}
解决方案
你有没有尝试 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();
}
其他提示
你在这里做什么是建筑的复合控制,而不是一个脚本控制,本身。你真的想要做什么是由继承CompositeControl的(和以下用于创建复合控制一个模型>),并实施 IScriptControl 时,代替。你问了很多胃灼热的做你的方式(的ViewState和回发的问题,等等)。
不隶属于 StackOverflow