【问题标题】:textbox value from client side in code behind?后台代码中来自客户端的文本框值?
【发布时间】:2013-08-12 13:53:37
【问题描述】:

我正在编写一个完全动态的网站。意味着所有控件都是由 Response.write 制作的,不,我有一个问题,页面加载后我想获取在代码后面动态生成的文本框的值,但 Request.Form 没有收到,这是我的代码:

Private Function userconf() As string
    Dim script As String

    Dim conString As String = ConfigurationManager.ConnectionStrings("sqlexpress").ConnectionString
    Using con As New System.Data.SqlClient.SqlConnection(conString)

        Dim com As New SqlCommand("SELECT * FROM clientamount WHERE Mode <> 1 ", con)


        con.Open()

        Dim resultid As String
        Dim reader As SqlDataReader = com.ExecuteReader()
        Try
            While reader.Read()

                RenderBuyForm = RenderBuyForm + "<div><input id='tx" & reader("ID") & "' type='text'  />" & reader("avrageamountunit") & "<input id='txd" & reader("ID") & "' type='text'/>"
            End While
        Finally

            reader.Close()
        End Try


    End Using


    Return RenderBuyForm
End Function

这个函数在 page_preload 事件中加载:

   Protected Sub Page_PreLoad(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreLoad
    userconf()


    End If
End Sub

在html页面中我也有

<form id="form1" runat="server">
   <%=RenderBuyForm%>

    <asp:Button ID="Button1" runat="server" Text="Button" />
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

    </form>

至少我现在尝试在 Button1_Click 事件中读取 html 页面中的文本框值:

 Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click



    For Each row As DataRow In tabel1.Rows

        Dim a As String = Request.Form("tx" & tabel1.Rows(0)("ID") & "")

      label1.text = a 


    Next
End Sub

但是 a 正在返回 Nothing 。我可以请你帮我解决这个问题吗?如何在后台代码中从客户端读取这些文本框值?

【问题讨论】:

  • 这是表单提交时使用的名称属性。您的输入似乎缺少此属性。我必须说您似乎没有以正确的方式使用 asp.net webfoms。主要是基于控件的声明和控件对象的操作、回发等。如果你想生成你的标记,你可以看看 MVC

标签: javascript asp.net sql-server vb.net


【解决方案1】:

您还需要添加到

RenderBuyForm = RenderBuyForm + "<div><input id='tx" ....
RenderBuyForm = RenderBuyForm + "<div><input name='tx' & reader("ID") ..

在 userconf() 中。

【讨论】:

  • 不需要javascript,通过在html输入中添加name属性解决了,再次感谢您的建议
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多