我编写了代码来显示下面的动态文本框,代码工作正常。但是如何在另一个服务器单击事件中获取这些文本框的值。我无法在按钮 btnUpdate_ServerClick 单击事件中检索值。以下代码不起作用。
protected void ClickShowModal(object sender, EventArgs e)
{
LinkButton btnEdt = (LinkButton)sender;
string[] arguments = btnEdt.CommandArgument.Split(';');
mdllblUniqueNumber.InnerText = arguments[0];
mdllblItemCode.InnerText = arguments[1];
mdltxtScanLocation.Value = arguments[4];
mdltxtReceiveQuantity.Value = arguments[5];
string managed_serial = arguments[6];
int open_qty = Convert.ToInt32(arguments[7]);
if (managed_serial == "Y")
{
mdltxtReceiveQuantity.Visible = false;
for (int i = 1; i <= open_qty; i++)
{
// Create a new TextBox
TextBox textBox = new TextBox();
textBox.ID = "mdltxtSerialNumber" + i;
textBox.Attributes["placeholder"] = "Key in Serial Number";
textBox.CssClass = "form-control";
// Add the TextBox to the Panel
panel_serial.Controls.Add(textBox);
LiteralControl spacerDiv = new LiteralControl("<div style='height: 5px; clear: both;'></div>");
panel_serial.Controls.Add(spacerDiv);
}
}
//btnResetToInitial.CommandArgument = arguments[4] == arguments[2] ? arguments[2] : arguments[3];
ScriptManager.RegisterStartupScript(this, this.GetType(), "ModalView", "<script>$(function() { $('#mdlSLRQ').modal('show'); $('#mdlSLRQ').focus(); $('#mdlSLRQ').on('shown.bs.modal', function () { $(this).find('[autofocus]').focus();}); });</script>", false);
}
protected void btnUpdate_ServerClick(object sender, EventArgs e)
{TextBox tb = new TextBox();
for (int i = 1; i < open_qty; i++)
{
tb = (TextBox)panel_serial.FindControl("mdltxtSerialNumber" + i.ToString());
string value = tb.Text; //You have the data now
}
}
Control.ControlCollection.Find(String, Boolean)
方法让它工作:
var tbName = "mdltxtSerialNumber" + i; // no need to call ToString, it;s automatically converted when concatenating with string
var ctrl = panel1.Controls.Find(tbName, true)
.FirstOrDefault();
if(ctrl is TextBox textBox)
{
MessageBox.Show("Found text box " + tbName + " with text " + textBox.Text);
}