添加后,ASP.NET控件立即添加到占位符丢失值

时间:2012-12-19 16:45:54

标签: asp.net controls

在以下问题上工作超过5小时:

Private Sub ModulEdit_PreInit(sender As Object, e As EventArgs) Handles Me.PreInit

    Dim modulid As Integer = 1

    loadeditors(modulid)



End Sub


Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

End Sub

Public Sub loadeditors(modulID As Integer)



    PlaceHolder1.Controls.Clear()

    Using dbContext As New EntitiesModel()
        Dim mps As List(Of ef.Modulparameter) = dbContext.Modulparameters.Where(Function(c) c.ModulID = modulID).ToList

        Dim mmid As Int16
        If EditMode.Checked = True Then
            mmid = RadComboBox3.SelectedValue
        End If

        Dim mp As ef.Modulparameter

        For Each mp In mps


            Dim lbl As New Label
            lbl.Text = "<BR>" & mp.Name & "<BR>"
            PlaceHolder1.Controls.Add(lbl)

            Select Case mp.Editor.Name

                Case "textbox1line"

                    Dim con As New TextBox
                    con.ID = mp.ID

                    If EditMode.Checked = True Then
                        Using dbContext2 As New EntitiesModel
                            Try
                                Dim mpa As ef.Menu_modul_paramvalue = dbContext2.Menu_modul_paramvalues.Where(Function(c) c.ModulparameterID = mp.ID And c.Menu_modulID = mmid).First
                                con.Text = mpa.Valuestring

                            Catch ex As Exception
                                con.Text = "AAAA"
                            End Try

                        End Using
                    End If



                    PlaceHolder1.Controls.Add(con)
                    'RadAjaxManagerProxy1.AjaxSettings.AddAjaxSetting(Panel1, con, Nothing)
                    'RadAjaxManagerProxy1.AjaxSettings.AddAjaxSetting(con, con, Nothing)


                Case "radeditor"

                    Dim con As New RadEditor
                    con.ID = mp.ID
                    con.ToolsFile = "\admin\controls\ToolsFile.xml"
                    'con.CssFiles.Add("\Content\frenzy\css\frenzy-orange.css")
                    If EditMode.Checked = True Then
                        Using dbContext2 As New EntitiesModel
                            Try
                                Dim mpa As ef.Menu_modul_paramvalue = dbContext2.Menu_modul_paramvalues.Where(Function(c) c.ModulparameterID = mp.ID And c.Menu_modulID = mmid).First
                                con.Content = mpa.Valuestring

                            Catch ex As Exception
                                con.Content = "BBBB"
                            End Try

                        End Using
                    End If


                    PlaceHolder1.Controls.Add(con)
                    'RadAjaxManagerProxy1.AjaxSettings.AddAjaxSetting(Panel1, con, Nothing)
                    'RadAjaxManagerProxy1.AjaxSettings.AddAjaxSetting(con, con, Nothing)



            End Select




        Next

    End Using


End Sub

我动态添加控件,在pre_init中调用上面的代码部分(尝试加载和init也得到相同的结果) 控件的值(文本)一直存在,直到该行PlaceHolder1.Controls.Add(con) con.text为空后。

之后添加控件,但没有值。 奇怪的是,在同一个proc中我添加了另一个控件(标签),其中文本值位于页面之后。

添加其他信息: 控制值(文本或内容),在调试LoadEditors时),总是正确设置。但是在页面上(文本框和radeditor)都是空的 路由从pre init调用,如许多相关帖子中所述。

2 个答案:

答案 0 :(得分:1)

您正在ModulEdit_Init中调用加载程序。这不应该是LoadControls吗?

答案 1 :(得分:0)

我自己解决了这个问题:

  • 在将控件添加到占位符之前添加“con.ViewStateMode = System.Web.UI.ViewStateMode.Disabled”
  • 在RadComboBox3中调用“loadeditors()”

很可能问题是,我在页面加载或init中加载了编辑器,它获得了正确的值,但随后调用了RadComboBox3.SelectedIndexChanged事件,它以某种方式覆盖了值

所以我的答案不是真正的答案,但它现在有效(我讨厌这样:它有效,但我不知道为什么);)