gpt4 book ai didi

asp.net - FindControl() 返回 null

转载 作者:行者123 更新时间:2023-12-02 21:04:28 25 4
gpt4 key购买 nike

我尝试创建动态添加控件的应用程序。我有母版页,我的 asp:Content 在这里:

<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 后,我想添加两个文本框。我尝试像 http://jagdeepmankotia.wordpress.com/2010/01/30/dynamically-add-controls-in-asp-net-c/ 那样做

这是我的代码:

    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

}

当在 loap foreach 中的 GetPostBackControl() 函数中查找我的 btnAdd 时,例如在 ctr“ctl00$MainContent$scriptManager1”的第一次迭代中,myControl 为 null...在下一次迭代中也是...所以我的函数始终返回无效的。原因是什么?

最佳答案

FindControl 仅搜索容器的直接子级。由于您是从页面级别开始的,因此您需要递归通过子 UpdatePanel 控件来访问 btnAdd 控件。

看看here例如如何执行此操作。

编辑:我不确定我是否理解您为什么要以这种方式“寻找”按钮,因为屏幕上只有一个静态按钮 - 在这种情况下您不需要使用 FindControl

<asp:Button ID="btnAdd" runat="server" Text="Add" onclick="btnAdd_Click" />

(或在代码中,btnAdd.OnClick += new EventHandler(btnAdd_Click);)

即使您在表单中动态添加了多个按钮,您也可以将它们全部连接到同一个按钮单击处理程序,在这种情况下,sender 将包含被单击的按钮控件。您通常会使用 FindControl 从动态添加的输入控件(文本框等)中抓取数据,而不是查看哪个控件导致回发(因为适当的事件处理程序中的“发送者”会更容易)

编辑 2:您可以像其他控件一样动态添加按钮

    Button myButton = new Button();
myButton.Text = "Click Me";
myButton.Click += new EventHandler(btnAdd_Click);
myPlaceHolder.Controls.Add(myButton);

如果您希望已添加的所有控件在回发之间“停留”,请在页面和控件上启用 View 状态,然后确保在 OnInit 中仅添加控件一次而不回发:

   base.OnInit(e);    
if (!IsPostBack)
{ // ... Add controls here

您可以将“mycount”的状态保留在隐藏字段中(在同一个更新面板中,并启用 View 状态) - 您每次都需要将其解析为 int。或者您可以使用 SessionState 来跟踪它。

关于asp.net - FindControl() 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8589950/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com