Jump to content
Xtreme .Net Talk

FindControl question .. Creating a server control from code behind


Recommended Posts

Posted

Hello,

 

I am tring to create a textbox on the code behind and then check its value when a button is clicked.

 

Dim ctrl As New WebControls.TextBox

ctrl.ID = "lname"

ctrl.Text = "smith"

 

plc1.Controls.Add(ctrl) ' placeholder contol

 

 

Private Sub btn1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn1.Click

 

Dim ctrl1 As Control

ctrl1 = Page.FindControl("lname")

If (Not ctrl1 Is Nothing) Then

' Get control's parent.

Response.Write("Id = " & ctrl1.ID)

Else

Response.Write("Control not found.....")

End If

 

end sub

 

I am lawyas getting "Control not found" and I think the reason is because I am not able to assign "runat='server'" property to the textbox on the code behind.

 

How does one create a server control from code behind?

 

Thank you,

 

Burak

Posted

Well let's say you need a list of link buttons that get populated from the database, you would create them dynamically right?

 

Right now I hardcoded a number of link buttons because I know how many records are in the database.

 

In the future if the number of records changes, I would have to add new link buttons.

 

Burak

Posted
It didn't let me add the control using Page.Controls.Add, which makes sense because there is no placeholder associated with the control. How would it know where to put the control on the page?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...