【问题标题】:How to reference value of dynamic field in asp modalpopup如何在asp modalpopup中引用动态字段的值
【发布时间】: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


【解决方案1】:

你的情况

TextBox MyTextBox = PayDetail_ModalPopupExtender.FindControl("txtMedicare") as TextBox;

这里是关于 Control.FindControl 的信息 https://msdn.microsoft.com/en-us/library/486wc64h(v=vs.110).aspx

【讨论】:

  • 感谢您提供的信息,但我不确定它是否那么简单。在这种情况下,当我尝试该代码时 MyTexBox 为空。我已经阅读了一些关于视图状态的文章,并且在我尝试读取值时我认为动态文本框超出了范围?
  • 在更准确地阅读了您的案例后,我更新了我的答案。希望对您有所帮助。
  • 感谢埃尔达 - 但仍然没有喜悦。一旦我到达弹出窗口上 Save 按钮的 Click 事件,modalpopup 的控件集合为空...
  • 我在大象经过时仔细观察)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-04
  • 1970-01-01
  • 2016-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多