C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Forums » .NET » ASP.NET »

Add control dynamically


Posted Date: 04 Jul 2009      Posted By: bill      Member Level: Gold     Points: 1   Responses: 5



hi,
how to add the control in the asp.net web page dynamically?I have link button.When a user click the button, controls have to be added in the page by javascript with predefined layout.Also how to use their(contorl's)value?
regards
Bill





Responses

Author: Abhinav Dawra    05 Jul 2009Member Level: GoldRating: 2 out of 52 out of 5     Points: 2

in an asp.net page you can create controls dynamically by instantiating the object of the control class and then adding that control on the page.
e.g. To create a dynamic label use
Label l = new Label();
Page.Controls.Add(l);

This will add the control to this page.

Thanks,
Abhinav
Please rate if this answer was helpful



Author: Devendra    05 Jul 2009Member Level: GoldRating: 2 out of 52 out of 5     Points: 2

There is 2 way to add control dynamically

One add HTML controls using string builder another on user control class object.

Ist Option
StringBuilder sb = new StringBuilder();
sb.append("<input type='text' size='20' value='' ID='text1' />");

2nd Option
TextBox txt1 = new textBox();
PlaceHolder.Controls.Add(txt1);

By this way you can add dynamic controls on your page.

Thakns
dkmisra



Author: Deepika Haridas    05 Jul 2009Member Level: DiamondRating: 2 out of 52 out of 5     Points: 2

Hi,

Here is a sample code

Dim lblMessage as New Label()
lblMessage.Text = "Hello, World!"
lblMessage.Font.Bold = True

'Finally, we need to add this control to the Web page. Not surprisingly, the Controls property has an Add method, which is demonstrated in the following code:

Controls.Add(lblMessage)




Thanks & Regards,
Deepika
Editor

If U want to shine like a SUN..First U have to burn like the SUN!!
Need a Guide? Join my mentor program..



Author: UltimateRengan    05 Jul 2009Member Level: DiamondRating: 2 out of 52 out of 5     Points: 2

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim btn As Button
Dim txt As TextBox
For i As Integer = 0 To 2
btn = New Button
txt = New TextBox
txt.ID = "Textbox" & i
txt.Text = "Textbox" & i
btn.Text = "DynamicButton" & i
btn.ID = "DynamicButton" & i
AddHandler txt.TextChanged, AddressOf Txt_TextChanged
AddHandler btn.Click, AddressOf btn_Click
form1.Controls.Add(btn)
form1.Controls.Add(txt)
Next
End Sub
Protected Sub btn_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Response.Write("G.Renganathan(UltimateRengan" & CType(sender, Button).ID)
End Sub

Protected Sub Txt_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Response.Write("Hai")
End Sub
End Class

Advance Happy Diwali
SAP B1



Author: pradeep    06 Jul 2009Member Level: SilverRating: 1 out of 5     Points: 1

with objects


Post Reply

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.


Next : Checking dates in msaccess
Previous : How to store the login information for my website?
Return to Discussion Forum
Post New Message
Category: ASP.NET

Related Messages



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use