.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

Dynamic controls

Posted By:      Posted Date: September 07, 2010    Points: 0   Category :ASP.Net
i want to generate button dynamically and i want to select the dimension and onclick event for that

View Complete Post

More Related Resource Links

Need help regarding C# / AJAX Dynamic Controls creation


radiobuttons is assigned before page init.. 

public partial class Default : System.Web.UI.Page
//Assign values as a specific Dynamic WebControl Creation.
List<RadioButton> radioButtons = new List<RadioButton>();
List<CheckBox> checkBoxes = new List<CheckBox>();

Then I created a class that would dynamically create a table containing the available flights depending on session.. (See CallPageLoad())

The placeholder and button is inside the UpdatePanel...

on the button inside the update panel, (btnSelect)

i have this code which works... 

protected void btnSelect_Click(object sender, EventArgs e)

Label1.Text = Convert.ToString(radioButtons.Count);

It detects the number of radiobuttons that were dynamically created...

But when I use the following code inside the BtnSelectClick event...

//PLS HELP.......
foreach (RadioButton button in radioButtons)
if (button.Checked == true)
Label1.Text = "You select... " + button;



It does not work even though t

Problem with Dynamic Controls



I have a custom user control that contains within it a repeater control. Inside this repeater I programmatically add another custom user control, which contains a text box and a custom validator. This final custom user control can be added any number of times depending on the application's logic. All of this is part of a wizard step.

When the user completes the step, if any of the data added to the text boxes is incorrect the validator gets fired on post back. However, because I can have any number of these controls added to the repeater, the user has to scroll down to see any of the incorrectly completed boxes.

Due to the controls being added dynamically it is not possible to use a validation summary control - I have tried!!! Therefore, is there away I can use JavaScript to position a cursor or to jump to each incorrectly completed textbox without the user needing to scroll down the page? For example, if there are 10 textboxes, and textboxes 8 and 9 have numbers inside them inside strings, then the JavaScript should jump textbox 9.

dynamic creating controls in webpart


Hello everybody,


My webpart (wss 3.0, VS2005) initialy contain a listbox. Depeding on the selection made a certain amount of textboxes are to be created and rendered. Via these dynamically created / rendered textboxes text can be submitted (via a button), for futher processing.


I am not sure how, or where in the webpart code, to dynamically create these textboxes.


Maybe somebody can give me some advise on this topic.


Thanks in advance









add any number of dynamic controls on press of button

how can i add new control like image on every click of a button. the number of controls added should be number of times i pressed that button

Problem with Dynamic Controls inside an UpdatePanel

Hello Everybody ,I have this problem,if anyone could help me i'll be so gratefull. I had been created some dynamic radio buttons in a loop inside a dinamy table which is inside an UpdatePanel , but i can not handle the events my code is below:public void MetodoCreacion(){RadioButton RBtn = null;Table table = new Table();TableRow row = null;TableCell cell = null;for (int i = 0; i < 5; i++){row = new TableRow();cell = new TableCell();RBtn = new RadioButton();RBtn.ID = "RBGN_" + i.ToString();RBtn.GroupName = "RBGN1";RBtn.Text = "Opcion " + i.ToString();RBtn.CheckedChanged += new EventHandler(RBtn_CheckedChanged);RBtn.AutoPostBack = true;cell.Controls.Add(RBtn);row.Controls.Add(cell);table.Controls.Add(row);}UpdatePanel1_UpdatePanelAnimationExtender.Controls .Add(table);}public void RBtn_CheckedChanged (object sender, EventArgs e){Label1.Text = ((RadioButton)sender).ID;}and the controls doesent call the event ...thanks a lot

Validation on detailsview that contains both dynamic and standard controls?

I have a form that, originally, contained only dynamic controls. Validation worked perfectly, all in one spot.Today I changed three of the controls to cascading dropdown lists, two of them being required fields.After adding a RequiredFieldValidator for the two required fields, it's as though I have two separate validation events:1. Dynamic field validation - any errors show up in validation summary, and after all are resolved, no longer appear2. Required field validation - do not appear in validation summary until after all dynamic fields are validatedA user could press submit, and be alerted of one or more validation errors from the dynamic fields. Then, after resolving all those, the user would THEN see the RequiredFieldValidator messages in the validation summary. That would be understandably frustrating.Aside from using non-dynamic validation & RequiredFieldValidator for each field ... is there any way to have one validation sequence?Hope this makes sense.Thank you!

Need Help : Dynamic ASP.net controls

Hello,I have a calendar control and am dynamically adding textboxes to each cell for each day.Each day has some value assigned to it.It's an asp text box. I'm just using e.Cells.Add(textbox).user fills in some values for each day and then wants to save data.How do i get a handle to each text box.I want to iterate through the text boxes and get the data for each day and then save it in the database.Want to specifically used any features that .net provides i.e. accessing values of dynamically ceated controls.Thanks...

JQuery with dynamic ASP.NET controls (DropDownList)

I am mixing JQuery with ASP.NET webforms.  Adding dynamic controls and accessing the selected values with JQuery works differently under different circumstances and I am trying to establish the difference. Why does Senario 1 work in debug mode but not without breakpoints?  What does databinding to a GridView do that adding DropDownList controls to a asp:panel does not? Is there some sort of DOM registration going on in DataBind? All senarios use the same JQuery: var ddlCollection = $("select[name$='ddlDynamicDropDown']") Senario 1: Breakpoints on each line (Success) Add DropDownList controls to a asp:panel dynamically in the PageLoad method (Does the page lifecycle matter? OnLoad method instead?). Run the code Select values and click on a button firing a JavaScript function (Break in Debug) Break in JavaScript file Access the dropdowns and their selected values successfully Senario 2:  Run the Senario 1 code without breakpoints (Fails to locate the ddlCollection) Senario 3: Use a GridView (Success) Add values to populate the DropDownList controls to a Generic List. Bind the values to a GridView control containing the DropDownList controls Run the code Select values and click on a button firing a JavaScript function Use JQuery to Access the dropdowns and their selected values successfully.  

Viewstate dynamic controls in wizard

Hello group, I have a question about retrieving the values from dynamic created controls. I'am using an wizard witch collects it data based on the steps in the wizard. At a certain wizard step i generate dropdownlists and textboxes depending on some records from a database. At this point no problem, the controls are created perfect. When i press the next button on the wizard i read out the eventarg e.currentstepindex, iterate throug the gridview witch is displaying the controls and try to find the control with "findcontrol" function.   The controls are "nothing" and i cant access them. THe source code of the page lets me show that the controls are there and that they have the assigned ID's. I investigated on google what the problem could be. I has to do with the viewstate and i have to regenerate the controls on page_load. This is something i don't understand.   Could somebody please help me with this?   Thank in advance, René 

TemplateBinding for Dynamic Controls

I create a templated RadioButton at run time. After initializing the templated RadioButton, I set the DataContext and Tag property and then add this button in a StackPanel. The problem is that the template binding does not work. below is the XAML and the code behind. All this work if I assign values to these properties in XAML. Any ideas? Code: TemplatedRadioButton commandButton = new TemplatedRadioButton(); commandButton.DataContext = "bla"; // Some txt that I will. commandButton.Tag = MyImage; // This is the ImageIcon that I create at run time too. MyStackPanel.Children.Add(commandButton); XAML: <ControlTemplate TargetType="{x:Type RadioButton}">  <Grid SnapsToDevicePixels="true">   <Border>    <StackPanel Orientation="Horizontal" Margin="0 6 0 0" HorizontalAlignment="Right">     <Border x:Name="Bd" BorderThickness="0 2 0 0" CornerRadius="8" BorderBrush="{TemplateBinding BorderBrush}" Height="68" Width="80">      <Grid Margin="0 8 0 1">       <Grid.RowDefinitions>        <RowDefinition Height="4*"/>        <Row

Dynamic Controls created missing after postback


While generating dynamic controls like , textboxes, dropdownlist, checkboxes and radiobuttons, after filling the textboxes and etc and submit and then postback occurs, the dynamic created controls missing. I try to find a way to make the controls persist for validation checking. Tried to use Viewstate to store the pnlTextBoxes and etc, but it has serializable error.  



protected void Page_Load(object sender, EventArgs e)
            pnlTextBoxes.Visible = false;
            pnlCheckBoxes.Visible = false;
            pnlRadioButtons.Visible = false;
            pnlDropDownList.Visible = false;
            pnlSubmitButton.Visible = false;
            pnlResults.Visible = false;


        protected void btnGenerateForm_Click(object sender, EventArgs e)

            if (tbTextBox.Text != String.Empty)

                if (Convert.ToInt32(tbTextBox.Text) > 0)
                    Label lblTextBox;
                    TextBox tbTextBox1;

                    for (int i = 0; i < Convert.ToInt32(tbTextBox.Text); i++)
                        lblTextBox = new Label();
                        lblTextBox.ID = "lbl" + i;
                        lblTextBox.Text = "TextBox" + i + ":&quo

Add very dynamic controls to a asp:table


 Hi all. I want to create a TableRow with a TextBox. I want when click a Button a TableRow add to asp:table. in Button_Click event code I can add a row by a TableRow & TableCell & TextBox Objects and with help a HiddenField.
My problem is that: after add first row that has a textbox, next row doesn't add by click button. I think Controls delete after post back in server and first textbox delete and new textbox add same position. what is solution after every click, a row with textbox add to the table?

            TableRow tr = new TableRow();
            TableCell tc1 = new TableCell();
            TableCell tc2 = new TableCell();
            TableCell tc3 = new TableCell();
            TextBox txt1 = new TextBox();
            TextBox txt2 = new TextBox();
            DropDownList drp = new DropDownList(); drp.Width = Unit.Pixel(100);


"view state" does't work for Image's ImageURL in dynamic controls


Hi all. in my page_load method I call a mthod that it create some dynamic controls. one of them is an Image control that it's ImageURL property set to an URL that is saved in data base. image shows that URL but when postback ocuurs, previous image controls doesn't their image. but other controls for example TextBoxes keep text values. what should I do for keep image url property for this dynamic Images controls?


Creating dynamic web controls outside Init method



I am creating a dynamic <asp:Table> based on a user's selection from a DropDownList. The number of columns and rows in the table depends on the user's choice from the DropDown.

The final two columns in each row needs to contain an Edit and Delete button. I am able to create these buttons programmatically. However, I am unable to get the click event to fire.

After researching this, it appears to be the case that my dynamic Web controls need to be created in the page Init method.

However, I am unable to do this as I do not know how many table rows, columns and buttons to create, until after the user has made a selection from a DropdownList.

I would be be very grateful for some tips on the correct way to create and enable the events for my dynamic buttons, in this scenario.

Here is a brief outline of how the program currently works (this is just a brief outline I quickly sketched, small details may be wrong!):


<asp:DropDownList ID="ddlMyDropDown" runat="server" 
                onselectedindexchanged="ddlMyDropDown_SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList>

<asp:Table ID="tblMyTable" runat="server" /> 

Trouble with update panel and dynamic controls and post back


 I have a page that is mostly dynamically built in the page load of my VB.net code behind page.

The issue is that I made a change to the page and added a button that redirects to a secondary page. When the button is clicked there is a very noticeable lag until the button click event fires because the page is being rebuilt every time.

I had not wrapped the code in the load event in the If not ispostback.... because I have dynamic controls that the user interacts with and I need the page to be rebuilt when the user clicks them.

The dynamic controls are clickable table cells. I did some looking and I found that the RegisterPostBackControl. I thought this would be perfect but, it does not although I maybe using it incorrectly.

Here is the code flow.

Page Load

If not ispostback then


end if


private sub setuppage()

'get a reference to the scriptmanager on the master page

Dim scriptManager = DirectCast

Using dynamic controls (making arrays of controls)


Hi guys I m new to forum

I m stuck up in following code what i want to do is every time user clicks the button it should display 1 more panel (in project button is for showing for experience input fields so if some person have more than one experience than by simply click this button it should show one more set of controls  )

please check code below

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class AddExprience : System.Web.UI.Page
    Label[] Company_label_arr=new Label[50];
    TextBox[] Company_textbox_arr = new TextBox[50];

    Label[] Position_label_arr = new Label[50];
    TextBox[] Position_textbox_arr = new TextBox[50];

    Label[] Responsiblity_label_arr = new Label[50];
    TextBox[] Responsiblity_textbox_arr = new TextBox[50];

    Label[] ResonForLeaving_label_arr = new Label[50];
    TextBox[] ResonForLeaving_textbox_arr = new TextBox[50];

    Panel[] panel_arr=new Panel[50];
    Int32 count=0;
    Label lsd = new Label();
    Control[] control11=new Control[50]; 
    protected void Page_Load(object sender, EventArgs e)

    protected void AddExpButton_Click(object sender, EventArgs e)
        count = System.Convert.ToInt32(Session["count1"

how to avoid dynamic change of controls names in ascx page sharepoint


when creating webpart contols using ascx in sharepoint deployed . the webpart added to site. the controls name is page has changed. please provide detials to avoid name changes

Thanks in advance

ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend