burak Posted September 25, 2003 Posted September 25, 2003 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 Quote
mr relaxo Posted September 26, 2003 Posted September 26, 2003 Once you postback any control you create dynamically no longer exists. Why do you need to create it dynamically? Quote You can not get ye flask.
burak Posted September 26, 2003 Author Posted September 26, 2003 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 Quote
*Gurus* Derek Stone Posted September 26, 2003 *Gurus* Posted September 26, 2003 Use the Page.Controls.Add()/AddRange() methods instead. You do not need to use FindControl(). Quote Posting Guidelines
burak Posted September 26, 2003 Author Posted September 26, 2003 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? Quote
*Gurus* Derek Stone Posted September 26, 2003 *Gurus* Posted September 26, 2003 It would make sense then to add a PlaceHolder to the location in the page flow where the dynamic controls should be placed. Quote Posting Guidelines
mr relaxo Posted September 27, 2003 Posted September 27, 2003 You could use a datagrid or datalist control to give you a button for each record in the database. Quote You can not get ye flask.
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.