简体   繁体   English

如何获取动态创建的文本框的文本值

[英]how to get the text value of dynamically created textbox

I want to get the value of my textbox that I created dynamically when I click a button 我想获取单击按钮时动态创建的文本框的值

I need to do this cause the value of my textbox is used for retrieve data from database 我需要这样做,因为我的文本框的值用于从数据库检索数据

how could I achieved this thing?? 我怎么能实现这个目标?

the flow is Button click - creating textbox - filling textbox with value - Button Click - Get Text of textbox 流程为按钮单击-创建文本框-用值填充文本框-按钮单击-获取文本框的文本

here is my code to make the textbox 这是我制作文本框的代码

Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    For i As Integer = 0 To 4
        textbox = New TextBox With {.ID = "TextBox" & i}
        plchld.Controls.Add(textbox)
    Next
End Sub

I have tried something like this but the code didn't work 我已经尝试过类似的操作,但是代码不起作用

Protected Sub OkButton_Click(sender As Object, e As EventArgs) Handles OkButton.Click
    Dim a(5) As String
    For i As Integer = 0 To 4
        a(i) = CType(plchld.FindControl("Textbox" & i), TextBox).Text
    Next
End Sub

thanks in advance for any help 预先感谢您的任何帮助

edit for the answer 编辑答案

I've found the way to solve this. 我找到了解决此问题的方法。 I use request.form to get the value of my textbox. 我使用request.form来获取文本框的值。

Thanks for anyone that participating 感谢参加的任何人

Regards, 问候,

Julian 朱利安

This is how I have done in my asp.net application. 这就是我在asp.net应用程序中所做的事情。

Creating dynamic control 创建动态控制

TextBox txtDate = new TextBox();
txtDate.EnableViewState = true;
txtDate.ID = "PreixValue" + 1;
txtDate.Text = "07 Feb 2014"
pnl.controls.add(txtdate); 

To retrieve the value from that textbox 从该文本框中检索值

DateTime datefrom = DateTime.Now ;

                for (int cnt = 0; cnt < Request.Form.Count; cnt++)
                {
                    if (Request.Form.AllKeys[cnt].Contains("Prefixvalue"))
                    {
                        int ParamStartPoint = Request.Form.AllKeys[cnt].IndexOf("Prefix") + 4;
                        int ParamNameLength = Request.Form.AllKeys[cnt].Length - ParamStartPoint;

                        string[] ControlName = Request.Form.AllKeys[cnt].Substring(ParamStartPoint, ParamNameLength).Split('$');

                                if (ControlName[0] == "Date From")
                                {

                    datefrom = DateTime.Parse(Request.Form[cnt]); 
                    //datefrom has value now
                                }
    }
}

This is how I have done in my web application, but there may be other ways achieve this. 这是我在Web应用程序中所做的事情,但是可能还有其他方法可以实现此目的。

basically when you create Dynamic control in webform this will be available through Request.Form. 基本上,当您在Webform中创建动态控件时,可以通过Request.Form使用。

hope this helps you. 希望对您有帮助。

Protected Sub OkButton_Click(sender As Object, e As EventArgs) Handles OkButton.Click
Dim a(5) As String
For i As Integer = 0 To 4
  Dim anotherObj As TextBox = Me.Controls.Item("Textbox" & i)  
  a(i) =anotherObj.Text
Next 

The issue is that dynamic controls are lost on a postback so when the OkButton click event is handled, there is nothing inside your plchld control. 问题是动态控件在回发时丢失,因此,在处理OkButton click事件时, plchld控件内部没有任何内容。 You must recreate your controls with the same ID on postback if you want to retrieve the text in the textboxes. 如果要在文本框中检索文本,则必须在回发时使用相同的ID重新创建控件。

Using your code, all you need to do is on postback determine if the textboxes were created and if so, recreate them. 使用代码,您需要做的就是回发以确定文本框是否已创建,如果是,则重新创建它们。

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    ' Determine if the text boxes were created and if so, recreate them.
    If CBool(ViewState("TextBoxesCreated")) Then
        CreateTextBoxes()
    End If
End Sub

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
    CreateTextBoxes()
    ViewState("TextBoxesCreated") = True
End Sub

Private Sub CreateTextBoxes()
    For i As Integer = 0 To 4
        plchld.Controls.Add(New TextBox With {.ID = "TextBox" & i})
    Next
End Sub

Protected Sub OkButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles OkButton.Click
    Dim a(4) As String
    For i As Integer = 0 To 4
        a(i) = CType(plchld.FindControl("Textbox" & i), TextBox).Text
    Next
End Sub

I don't know the full extent of what you are doing but I would suggest not creating them dynamically if you don't need to. 我不知道您在做什么,但是如果您不需要,建议不要动态创建它们。 Just show or hide the textboxes instead. 只需显示或隐藏文本框即可。

Reference: http://www.codeproject.com/Articles/3684/Retaining-State-for-Dynamically-Created-Controls-i 参考: http : //www.codeproject.com/Articles/3684/Retaining-State-for-Dynamically-Created-Controls-i

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM