【问题标题】:Trying to retrieve values in an UpdatePanel from the code behind尝试从后面的代码中检索 UpdatePanel 中的值
【发布时间】:2009-08-20 15:24:15
【问题描述】:

我有一个 asp.net 网络表单,有一个更新面板,其中包含一个带有文本框的表格,用户可以在其中输入值。该表位于更新面板中,因为文本框是从非常长时间运行的数据库查询中生成的。它是在表单加载几秒钟后使用计时器控件生成的。

当表单被回发时,表格对我们的代码不可用...

这是该部分的代码,这是一个动态数据编辑页面。

 <asp:UpdateProgress ID="UpdateProgress1" runat="server" 
            AssociatedUpdatePanelID="UpdatePanel2">
            <ProgressTemplate>
                <div>Getting subjects...</div>
            </ProgressTemplate>
        </asp:UpdateProgress>
        <asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional"  >
            <ContentTemplate>
                <asp:Timer ID="Timer1" runat="server" Interval="2000" Enabled="false" >
                </asp:Timer>
                <div id="subjects_fav">
                    <asp:Table ID="tabSubjectsFav" runat="server" BorderWidth="2" BorderColor="Aquamarine">
                    </asp:Table>
                </div>

            <asp:Button ID="UpdateButton" runat="server" OnClick="SaveEverything" Text="Save Everything" />

            </ContentTemplate>
        </asp:UpdatePanel>


Protected Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs)
    'DynamicDataManager1.RegisterControl(DetailsView1)
    DynamicDataManager1.RegisterControl(lvArticles1)
    DynamicDataManager1.RegisterControl(lvArticles2)
    DynamicDataManager1.RegisterControl(lvArticles3)
    DynamicDataManager1.RegisterControl(lvArticles4)
End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

    'trying to get the id_project for this page
    'get article id for first article

    If Not IsPostBack Then
        Timer1.Enabled = True
    Else
        UpdatePanel2.Update()
    End If

    'now get related project from db
    Dim db As New MTRData.mtddDataContext()
    Dim art = (From a In db.articles _
                 Where a.id = Request.QueryString("id") Take 1 _
                 Select a).SingleOrDefault()
    'txtid_project.Text = art.id_project

    'need to use this all over the place so saving it as a property type thing as well
    _ProjectID = art.id_project
    _PublicationID = art.id_publication
    _ArticleID = art.id

End Sub

Protected Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick

    If boolTimerFired = False Then
        Timer1.Enabled = False
        boolTimerFired = True
        GenerateSubjectsFavGrid(_ArticleID)
        UpdatePanel2.Update()
    End If
    Timer1.Enabled = False

End Sub

【问题讨论】:

    标签: asp.net-ajax


    【解决方案1】:

    如果没有一些代码,就无法真正诊断出这个问题。我猜你是在页面生命周期的错误部分生成文本框。如果您在 OnInit() 期间没有这样做,然后在提交时以相同的方式重建它们,那么表单上将不会有任何文本框。

    请记住,每个新的回发都会创建一个带有一组全新控件实例的新页面对象。如果您的动态控件不是每次都以相同的方式创建,那么它们就不会存在。

    【讨论】:

    • 嗨,womp,谢谢你,在 OnInit 中没有完成,因为它们需要 30 秒才能将当前值从数据库中提取出来(不要问!)。因此,它们会在页面通过计时器加载几秒钟后完成......
    • 是的,如果“GenerateSubjectsFavGrid(_ArticleID)”是您将文本框动态添加到页面的位置,那么这就是问题所在。此处添加的任何控件都不会出现在下一次回发时创建的下一个 Page 对象中。网格总是相同的大小吗?是否有可能使网格成为非动态的,将可见性设置为 false,并在填充完成时显示它?
    • 嗨,womp,遗憾的是,不会因客户而异。这些值不会在 response.form 中吗?我想当它们通过 JavaScript 更改时,我必须将值保存到会话或其他东西,然后从那里填充值。实际上进行此对话使我认为我可能会以某种方式缓存所需的控件,然后只担心进入会话的值或其他东西。感谢我不经意间为我指明了正确的方向。干杯,戴夫。
    • 实际值将在 response.form... 但文本框不会出现在新的 Page 对象中,因此它们不会去任何地方。如果您可以以完全相同的方式重新生成文本框,以使它们具有相同的 ID 等等,那么从那里检索它们是一种方法。有点像实现自己的视图状态。请注意,对于列表控件,这是行不通的,因为只有选定的值来自表单 post... 而不是列表的其余部分,因此您的列表需要重新绑定。但是只有一个值与文本框相关联,所以它可以工作。
    • 更新:从 response.form 中获取值以更新数据库,然后在更新面板中启用更新网格的计时器以再次显示它。
    猜你喜欢
    • 2013-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多