【问题标题】:Programmatically created textbox isn't showing Text, assigned in code-behind, retrieved from DB以编程方式创建的文本框不显示文本,在代码隐藏中分配,从数据库中检索
【发布时间】:2012-03-16 16:13:32
【问题描述】:

我在 asp.net 中有一个如下所示的表单:

  1. 不到一年
  2. Age in years[]____________

1,2 是单选按钮列表,其中 2 我有一个文本框,如果选择了 #2,它会接受输入。 此表单可以保存,但我在尝试显示结果时遇到了问题。

我在代码隐藏中创建了这些单选按钮列表和文本框。现在,当我试图显示答案时,我在 Gridview.rowDatabound 中再次创建这些控件,并使用 sqlDataReader 从数据库中检索它们的值。代码如下所示:

            Dim txtOther As New TextBox
            txtOther.ID = "txtOther"

            Dim strOtherTxt As String = "Why the **** this isn't working"


            fillResponse(rdoLstOption, txtChildID.Text, intQuestionID, strOtherTxt)

            txtOther.Text = strOtherTxt

            e.Row.Cells(2).Controls.AddAt(0, rdoLstOption)
            e.Row.Cells(2).Controls.AddAt(1, txtOther)

fillResponse 看起来像这样:(这里我正在检查它是单选按钮还是复选框列表 - 我的 checkBoxList 也不起作用)

        con.Open()
        Dim rdr As SqlDataReader
        rdr = cmd.ExecuteReader
        If typeCtrlType.Name = "RadioButtonList" Then
            fillRdoBtnList(ctrlToFill, rdr, strOtherText)
        ElseIf typeCtrlType.Name = "CheckBoxList" Then
            fillChckBoxList(ctrlToFill, rdr)
        End If
        con.Close()

最后 fillRdoBtnList 看起来像这样:

Private Sub fillRdoBtnList(ByRef rdoBtnListToFill As RadioButtonList, rdr As SqlDataReader, ByRef strOtherTxt As String)
    While rdr.Read()
        rdoBtnListToFill.SelectedValue = rdr.GetInt32(rdr.GetOrdinal("fkLuAnswerID"))
        If Not rdr.IsDBNull(rdr.GetOrdinal("strOtherTxt")) Then
            strOtherTxt = rdr.GetString(rdr.GetOrdinal("strOtherTxt"))
        End If
    End While
End Sub

我在 strOtherTxt 中得到了我期望的字符串,但它只是没有出现在我的表单中!我已经调试了我的程序,我发现了 txtOther。文本得到正确的文本,但即使那样它也不会出现在我的表单上!

有什么想法吗?

【问题讨论】:

    标签: asp.net textbox


    【解决方案1】:

    我认为这与视图状态有关。早些时候,我将选定的值绑定到 PostBack 上的复选框。但现在我在单独的页面中输入并将值发送到我的“复选框列表页面”。这解决了问题。

    虽然这里发生了什么,但我仍然感到困惑。任何解释将不胜感激。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-09-08
      • 2011-05-30
      • 2018-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多