【发布时间】:2015-07-06 16:02:13
【问题描述】:
我有一个 modalpopup 扩展器,当用户单击“添加”按钮时会显示该扩展器。我可以在该弹出窗口上创建一个动态文本框。
我的问题是我无法在代码隐藏中获得对该文本框的引用。
我在文本框中添加了一个“textchanged”事件处理程序,但由于它需要 AutoPostBack = true(我认为),所以在我的事件触发之前,modalpopup 被销毁!即使我重新显示 modalpopup,动态字段现在也消失了。
这是 modalpopup 的 ASP:
<asp:ModalPopupExtender ID="PayDetail_ModalPopupExtender" runat="server" PopupControlID="PayDetailPopupPanel" TargetControlID="btnShowDetailPopup"
CancelControlID="btnClosePayDetail" BackgroundCssClass="modalBackground">
</asp:ModalPopupExtender>
<asp:Button ID="btnShowDetailPopup" runat="server" style="display:none" />
<asp:Panel ID="PayDetailPopupPanel" runat="server" CssClass="modalPayPopup" align="center" style = "display:none">
<asp:table id="addClaimDetailTable" runat="server" class="table" >
<asp:TableRow style="display:none">
<asp:TableCell >
<asp:Label ID="lblPayHeaderID" runat="server" Text="Pay Header ID:"></asp:Label>
</asp:TableCell>
<asp:TableCell >
<asp:TextBox ID="txtPayHeaderID" runat="server" Enabled="false"></asp:TextBox>
</asp:TableCell>
</asp:TableRow>
</asp:table>
</asp:Panel>
我正在像这样在代码隐藏中创建一个动态文本框:
//add dynamic fields
TableRow tRow = new TableRow(); //row
TableCell hCell = new TableCell(); //header
TableCell dCell = new TableCell(); //data
Label lblHeader = new Label();
lblHeader.ID = "lblMedicare";
lblHeader.Text = "Medicare:";
TextBox txtData = new TextBox();
txtData.ID = "txtMedicare";
txtData.Text = "";
txtData.TextChanged += new EventHandler(txtData_TextChanged);
txtData.AutoPostBack = true;
//and add to LIST
PayDetailDynamicList.Add(txtData);
//myValue = txtData.ID;
hCell.Controls.Add(lblHeader);
dCell.Controls.Add(txtData);
tRow.Controls.Add(hCell);
tRow.Controls.Add(dCell);
addClaimDetailTable.Controls.Add(tRow);
我将文本框存储在静态列表中:
static List<TextBox> PayDetailDynamicList = new List<TextBox>();
这是事件处理程序的代码隐藏:
void txtData_TextChanged(object sender, EventArgs e)
{
TextBox t = sender as TextBox;
if (t == null)
throw new ArgumentException("sender not a textbox", "sender");
//add value to list?
string value = t.Text;
string id = t.ID;
}
我的问题是:如何进入事件处理程序来存储用户输入的数据?或者我怎样才能获得对这个动态文本框的引用,以便检索值?
有没有更好的方法来做到这一点?为我的 modalpopup 使用表单?如果我知道 jquery 和 ajax,我可能会那样做动态字段,但我不会......
感谢任何帮助!如果您需要更多详细信息,请告诉我...谢谢!
【问题讨论】:
-
更新:这不是我所希望的答案,但它成功了。我最终通过在 modalpopup 表单上使用 10 个固定字段并根据数据要求使用循环使它们可见/不可见来解决我的问题......不是真正动态的,但随着截止日期的临近,我不能浪费时间试图再弄清楚。
-
我遇到了完全相同的问题。我应该使用固定的文本框移动吗?
标签: c# asp.net ajax dynamic modalpopupextender