打電話給我'n00b',但我是新創建腳本控件。我想用3個文本框創建一個簡單的控件。我有一個.cs文件,看起來像這樣:創建一個AJAX腳本控件
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;
}
}
我絕對不知道爲什麼文本框不會渲染?我想我錯過了一些東西...
[編輯]我得到的錯誤是「對象引用沒有設置爲對象的實例」。
[回答]我改變了文件,如下所示,它至少起作用。
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();
}
嘗試在添加'em之前實例化您的控件。例如costTextbox = new Textbox(); – 2009-01-30 03:47:52
效果很好。謝謝! – BBetances 2009-01-30 15:58:29