我整个上午都在苦苦思索这个问题,所以希望我能得到一些帮助。本质上,我在从 .net 4 中动态创建的某些文本框控件获取值时遇到问题。
这是应用程序所需的流程。
1).用户从作为字母模板的下拉菜单中选择一个 html 文档。此 html 文档包含格式为 $VARIABLENAME$ 的标签,这些标签将被替换为正确的值。
2).该程序通过模板运行并提取格式为 $STRING$ 的所有字符串并将它们存储在列表中。
3).对于此列表中的每个条目,程序都会根据原始 $VARIABLENAME$ 字段生成一个带有唯一 ID 的 asp:label 和 asp:textbox。
4).用户输入替换值,然后点击提交。
5).程序用替换值替换所有 $STRING$ 并输出结果。
在我需要从文本框中获取值之前,一切都运行良好。我很确定这是页面生命周期的问题,但是因为在用户从下拉列表中选择所需模板之前不会生成文本框,所以我不确定如何使它们通过回发持久存在以便我可以引用它们。
我是不是做错了?在提交按钮事件发生回发后,如何访问从下拉事件创建的文本字段?
编辑: 这是大部分相关代码。
protected void createTextBoxes(List<string> results)
{
if (results != null)
{
foreach (string result in results)
{
string formattedResult = result.Substring(1, result.Length - 2);
formattedResult = formattedResult.ToLower();
formattedResult = char.ToUpper(formattedResult[0]) + formattedResult.Substring(1);
var label = new Label();
label.ID = formattedResult;
label.Text = formattedResult + ": ";
templateFormPlaceholder.Controls.Add(label);
var textBox = new TextBox();
textBox.ID = result;
templateFormPlaceholder.Controls.Add(textBox);
templateFormPlaceholder.Controls.Add(new LiteralControl("<br />"));
previewBtn.Visible = true;
}
}
}
protected void templateDD_SelectedIndexChanged(object sender, EventArgs e)
{
var templatePath = "";
if (templateDD.SelectedIndex == 0)
{
previewBtn.Visible = false;
}
if (templateDD.SelectedIndex == 1)
{
templatePath = employeePath;
}
else if (templateDD.SelectedIndex == 2)
{
templatePath = managerPath;
}
List<string> regMatches = FindMatches(templatePath);
Session["regMatches"] = regMatches;
createTextBoxes(regMatches);
}
protected void Page_Init(object sender, EventArgs e)
{
if (Session["regMatches"] != null)
{
createTextBoxes((List<string>)Session["regMatches"]);
}
}
稍后,我尝试将这些文本框中的值添加到字典中。参数是字典的名称。关键字段是 $STRING$,结果是用户在文本框中输入的内容。
protected void previewBtn_Click(object sender, EventArgs e)
{
List<string> matchResults = (List<string>)Session["regMatches"];
Dictionary<string, string> parameters = new Dictionary<string, string>();
foreach (string result in matchResults)
{
TextBox tb = (TextBox)templateFormPlaceholder.FindControl(result);
parameters.Add(result, tb.Text);
}
var template = ReplaceKeys(parameters);
outputLBL.Text = template;
这是 .aspx 代码。
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="offerLetter.aspx.cs" Inherits="templateRegexTesting.offerLetter" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<p>
Which template would you like to use?
</p>
<asp:DropDownList ID="templateDD" runat="server" OnSelectedIndexChanged="templateDD_SelectedIndexChanged"
AutoPostBack="true">
<asp:ListItem></asp:ListItem>
<asp:ListItem Value="1">Employee</asp:ListItem>
<asp:ListItem Value="2">Manager</asp:ListItem>
</asp:DropDownList>
<br />
<asp:PlaceHolder ID="templateFormPlaceholder" runat="server" />
<div>
<asp:Button ID="previewBtn" runat="server" Text="Preview" Visible="false" OnClick="previewBtn_Click" />
</div>
<div>
<asp:Label ID="outputLBL" runat="server"></asp:Label>
</div>
<br />
</div>
</form>
</body>
</html>
编辑:当我想出来时,我把它放在评论中,但我想我应该把它移到问题中,这样它就更明显了:
我想我应该更新这个。我觉得自己有点像个白痴,但我确实设法让它工作了。基本上,我为控件分配了一个等于替换标记的 ID(例如 ID="$FIRSTNAME$")。我什至没有想到 $ 会引起任何问题。当我刚更改为 ID="Firstname"格式时,它工作得很好。感谢您提供的所有帮助!
最佳答案
你是对的,这都是关于页面生命周期的。必须在 Page_Init
处重新创建动态创建的控件阶段,以便在 View 状态绑定(bind)阶段之前存在。这意味着必须以某种方式(可能使用 Session
)存储多少 textboxes
您已经在先前的处理中创建以重新创建它们。提醒使用相同的 ID 并将它们添加到您的控件树(中继器或您正在使用的其他东西)。
更新
我给你一个建议:
1.声明类型为List<TextBox>
的类属性(我们称之为 CreatedTextBoxes
)
声明一个方法,该方法接收创建文本框所需的任何内容。此方法不得读取其范围之外的任何内容。它只会接收一些参数,创建文本框并将它们添加到另一个控件(例如
Repeater
)。将创建的每个文本框添加到CreatedTextBoxes
在下拉更改事件中,读取选项,将其保存到
Session
并调用这个方法在
Page_Init
, 验证Session
处的对象.如果它为 null 或空,则不执行任何操作。如果它有一个值,调用相同的方法,传递相同的参数- 当您需要从动态创建的文本框中检索它时,请使用
CreatedTextBoxes
而不是FindControls()
关于c# - 从 asp.net 中动态创建的文本框中获取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11992311/