0

Firstly, I'm a beginner programmer in ASP.NET(VB). As stated in the title how do i access the value of a dynamically created texbox

For example : On Aspx Page

<asp:Table id= "Table1" runat="server">
</asp:Table>

On Aspx.Vb page on Page_Load, I have

dim i as integer = 0

While i < 3
  Dim tempCell as New TableCell
  Dim tempCell2 as New TableCell
  Dim temprow as New TableRow 

  tempCell.Controls.Add(New LiteralControl("<asp:TextBox id = 'aa" & i & "' runat="server">this is value for ab " & i & "</asp:TextBox>"))
  tempCell2.Controls.Add(New LiteralControl("<asp:TextBox id = 'ab" & i & "' runat="server">this is value for ab " & i & "</asp:TextBox>"))

  temprow.Cells.Add(tempCell) 
  temprow.Cells.Add(tempCell2)

  Table1.Rows.Add(temprow)

  i = i + 1
End While

So, this is roughly what I wanted to do.

The code works, except that, How do i get the data on button click? I've done some google search but couldn't get to an anwser. I've tried the page.FindControl("ab" & i), but i still can't get the value.

Where did I do wrong with this? Thanks in advance.

1 Answer 1

1

Use directcast to access your cells. Here's an example code I wrote to access a dynamically generated textbox.

Dim txt = New TextBox()
txt.Name = "name1"
txt.Size = New Size(200, 70)
txt.Location = New Point(40, 40)
txt.Text = "I am a new textbox"
Me.Controls.Add(txt)
DirectCast(Me.Controls("name1"), TextBox).Text = "Some stuff here" 'The magic happens here
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks alot for this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.