vb.net 如何获取动态创建的文本框的文本值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21622503/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to get the text value of dynamically created textbox
提问by Julian Sanjaya
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
朱利安
回答by Lingaraj
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.
希望这对你有帮助。
回答by Rohit
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
回答by singularhum
The issue is that dynamic controls are lost on a postback so when the OkButton click event is handled, there is nothing inside your plchldcontrol. You must recreate your controls with the same ID on postback if you want to retrieve the text in the textboxes.
问题是动态控件在回发时丢失,因此在处理 OkButton 单击事件时,您的plchld控件内没有任何内容。如果要检索文本框中的文本,则必须在回发时使用相同的 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

