从updatepanel中动态创建的控件中获取价值

时间:2013-02-21 10:25:54

标签: c# vb.net dynamic updatepanel

asp:DataList中,我有不同的行,其中包含一些文本,每行都有一个按钮。在此按钮上,OnClientClick打开一个jquery对话框,onclick调用后端并在显示的对话框中填充更新面板。

后端给了我像" AV" " TEXT",这意味着我应填充文本框,并在返回值时,其标识符称为" AV"。可以有无限数量的控件,我将它们添加到(动态创建的)表中。

这很好用。但是在updatepanel中我有一个按钮,应该保存这些值和ID,但我找不到它们!我已经尝试将表格添加到Session,让我找到控件,但值不存在。如果没有添加到会话中,则控件不在那里。

<div id="dialog-confirm" style="display: none;">
<div id='dialog'>
    <asp:UpdatePanel ID="upEditUpdatePanel" UpdateMode="Conditional" runat="server">
        <ContentTemplate>
            <asp:Panel ID="pnlVars" runat="server">
            </asp:Panel>
            <asp:Button ID="btnOK" runat="server" Text="OK" OnClientClick="StartLoader();" OnClick="btnOK_Click" align="center" />
        </ContentTemplate>
        <Triggers>
            <asp:PostBackTrigger ControlID="btnOK" />
        </Triggers>
    </asp:UpdatePanel>
</div>

控件添加如下:

Dim pnlVars As Panel = upEditUpdatePanel.FindControl("pnlVars")
...
controls = GetControls()
Swtich controls.type
Case "TEXT"
Dim txtVar As New TextBox
txtVar.ID = "AV"
tblCellVal.Controls.Add(txtVar)
tblRow.Cells.Add(tblCellVal)
tblVars.Controls.Add(tblRow)
pnlVars.Controls.Add(tblVars)
....
upEditUpdatePanel.Update()

我尝试获取值并迭代控件的方式:

    Protected Sub btnOK_Click(ByVal sender As Object, _
                      ByVal e As EventArgs)
    For Each c As Control In pnlVars.Controls
        If TypeOf c Is Control Then

            If TypeOf c Is TextBox Then
                Dim ctr As TextBox = DirectCast(c, TextBox)
            End If

        End If
        If c.Controls.Count > 0 Then
            GetUserControls(c.Controls)
        End If
    Next
End Sub

接受vb.net或c#

中的答案

1 个答案:

答案 0 :(得分:0)

您应该了解asp.net Page Cycle

http://msdn.microsoft.com/en-us/library/ms178472%28v=vs.100%29.aspx

要获取已动态创建的值,您必须从

获取它们
protected void Page_PreRender(object sender, EventArgs e) {
    // get values from your dynamically created elements
}