单击按钮后无法访问动态创建的文本框的文本
Posted
技术标签:
【中文标题】单击按钮后无法访问动态创建的文本框的文本【英文标题】:Can not access text of dynamically created textbox after button click 【发布时间】:2020-11-05 20:23:51 【问题描述】:我有问题。我有一个带有一个 ContentPlaceHolder 的 aspx 页面。在那个 ContentPlaceHolder 我有一个 div 和一个按钮,如下所示:
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<div id="divCustomCustomers" runat="server" >
</div>
<asp:Button runat="server" ID="btnInsert" Text="Insert" OnClick="btnInsert_OnClick" />
</asp:Content>
在 PageLoad 上,我动态地将文本框控件添加到 divCustomCustomers,如下所示:
private void AddControls(string currentCustomer)
var customTable = new Table();
customTable.ID = "tableCustomCustomer_" + currentCustomer;
var customTrHeaders = new TableRow();
customTrHeaders.Cells.Add(new TableCell() Text = "TextId" );
customTable.Controls.Add(customTrHeaders);
var customTableCells = new TableRow();
var tableCellTextId = new TableCell();
var textBoxTextId = new TextBox() ID = "tbTextId_" + currentCustomer ;
tableCellTextId.Controls.Add(textBoxTextId);
customTableCells.Cells.Add(tableCellTextId);
customTable.Controls.Add(customTableCells);
divCustomCustomers.Controls.Add(customTable);
然后我在该文本框中输入一些随机文本 (var textBoxTextId = new TextBox() ID = "tbTextId_" + currentCustomer ;)。
当我单击按钮并调用函数 btnInsert_OnClick 时,我似乎无法获取文本框的文本。回发后,文本框消失了。我尝试通过以下方式获取文本框:
protected void btnInsert_OnClick(object sender, EventArgs e)
string currentCustomer = "sameCustomerAsWhenCreated";
var control = divCustomCustomers.FindControl("tbTextId_" + currentCustomer) as TextBox;
var control2 = divCustomCustomers.FindControl("ContentPlaceHolder1_tbTextId_" + currentCustomer) as TextBox;
var control3 = this.FindControl("tbTextId_" + currentCustomer) as TextBox;
var control4 = this.FindControl("ContentPlaceHolder1_tbTextId_" + currentCustomer) as TextBox;
我也尝试过使用我读到的 FindControl 方法:
protected void btnInsert_OnClick(object sender, EventArgs e)
string currentCustomer = "sameCustomerAsWhenCreated";
var control = FindControl(this.Controls, "tbTextId_" + currentCustomer);
private Control FindControl(ControlCollection controlCollection, string name)
foreach (Control control in controlCollection)
if (control.ID.ToLower() == name.ToLower() || control.ClientID.ToLower() == name.ToLower())
return control;
if (control.Controls.Count > 0)
Control result = FindControl(control.Controls, name);
if (result != null)
return result;
return null;
但没有任何效果,我无法控制和使用文本。你们能帮帮我吗?请!
【问题讨论】:
【参考方案1】:如果没有回发,您是否运行了 AddControls()?
在页面加载时总是运行 AddControls(),你应该从文本框中获取文本。
我在 aspx 中添加了一个 Literal,下面是我的 aspx.cs 中的代码
<div id="divCustomCustomers" runat="server" >
</div>
<asp:Button runat="server" ID="btnInsert" Text="Insert" OnClick="btnInsert_Click" />
<hr />
<asp:Literal ID="ltResult" runat="server"></asp:Literal>
protected void Page_Load(object sender, EventArgs e)
//Don't run AddControls only in first load.
//if (this.IsPostBack)
//
// AddControls("sameCustomerAsWhenCreated");
//
AddControls("sameCustomerAsWhenCreated");
protected void btnInsert_Click(object sender, EventArgs e)
string currentCustomer = "sameCustomerAsWhenCreated";
var control = divCustomCustomers.FindControl("tbTextId_" + currentCustomer) as TextBox;
this.ltResult.Text = "control: " + control.Text + "<br>";
Here is my test result
【讨论】:
以上是关于单击按钮后无法访问动态创建的文本框的文本的主要内容,如果未能解决你的问题,请参考以下文章
XCUITest 无法检测到我动态生成的 tableView 的可访问性 id