Ajax 스크립트 컨트롤 생성
-
20-08-2019 - |
문제
저를 '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;
}
}
그리고 텍스트 상자가 왜 렌더링되지 않을지 전혀 모른다. 뭔가 빠진 것 같아요 ...
편집] 내가 얻는 오류는 "객체 참조가 인스턴스로 설정되지 않은 것"입니다.
답변] 파일을 다음과 같이 변경했는데 지금까지는 적어도 작동합니다.
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 및 Postback 문제 등).
제휴하지 않습니다 StackOverflow