나는 응용 프로그램을 만들려고 노력했다. 내가 masterpage을 가지고, 내 ASP : 콘텐츠가 여기에 있습니다 : 나는 두 개의 텍스트 상자를 추가 할FindControl() return null
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<asp:ScriptManager ID="scriptManager1" runat="server">
</asp:ScriptManager>
<div style="margin: 10px">
<asp:UpdatePanel ID="updatePanel1" runat="server">
<ContentTemplate>
<asp:PlaceHolder runat="server" ID="myPlaceHolder" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnAdd" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
</div>
<asp:Button ID="btnAdd" runat="server" Text="Add" />
btnAdd에서 클릭하십시오.
static int myCount = 1;
private TextBox[] color;
private TextBox[] text;
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
color = new TextBox[myCount];
text = new TextBox[myCount];
for (int i = 0; i < myCount; i++)
{
TextBox tbColor = new TextBox();
tbColor.ID = "colorTextBox" + i.ToString();
myPlaceHolder.Controls.Add(tbColor);
color[i] = tbColor;
TextBox tbText = new TextBox();
tbText.ID = "textTextBox" + i.ToString();
myPlaceHolder.Controls.Add(tbText);
text[i] = tbText;
LiteralControl literalBreak = new LiteralControl("<br />");
myPlaceHolder.Controls.Add(literalBreak);
}
}
public Control GetPostBackControl(Page page)
{
Control control = null;
string ctrlname = page.Request.Params.Get("__EVENTTARGET");
if (ctrlname != null && ctrlname != string.Empty)
{
control = page.FindControl(ctrlname);
}
else
{
foreach (string ctl in page.Request.Form)
{
Control mycontrol = page.FindControl(ctl);
if (mycontrol is System.Web.UI.WebControls.Button)
{
control = mycontrol;
// This gives you ID of which button caused postback
break;
}
}
}
return control;
}
protected void Page_PreInit(object sender, EventArgs e)
{
Control myControl = GetPostBackControl(this.Page);
if (myControl != null)
if (myControl.ClientID.ToString() == "btnAdd")
myCount = myCount + 1;
}
protected void btnAdd_Click(object sender, EventArgs e)
{
//handled in PreInit
}
때 기능 GetPostBackControl에서() loap의 foreach는 내 btnAdd을 찾고, ctl00 $ MainContent $ scriptManager1 "클릭률 (CTR)에 대한 첫 번째 반복 예를 들면 : 나는 http://jagdeepmankotia.wordpress.com/2010/01/30/dynamically-add-controls-in-asp-net-c/
이 내 코드처럼 그것을 시도해도 ", myControl은 null입니다 ... 다음 반복에서도 ... 그래서 내 함수는 항상 null을 반환합니다. 이유가 무엇일까요?
답장을 보내 주셔서 감사합니다. 나중에 여기에 두 번째 정적 버튼을 추가합니다. 동적으로 여기에만 텍스트 상자가 표시됩니다. 그래서 내가 여기 btnAdd_Click()을 사용할 수 있다고 생각하니? myCount를 증가시키고 나중에 myCount에서 새로운 값으로 페이지를 다시로드하려면 어떻게해야합니까? 당신은 무엇을 생각합니까? – cadi2108