Search

Wednesday, February 12, 2014
0 comments

How to add Controls Dynamically in ASP.NET

7:34 AMWednesday, February 12, 2014
There are times when it is more practical to create a control at run time than at design time. For example, imagine a search results page in which you want to display results in a table. Because you do not know how many items will be returned, you want to dynamically generate one table row for each returned item.

Note Existing controls can often provide the functionality you get from creating controls dynamically. For example, controls such as the Repeater, DataList and RadioButtonList controls can dynamically create rows or other control elements when the page runs.

To programmatically add a control to a page, there must be a container for the new control. For example, if you are creating table rows, the container is the table. If there is no obvious control to act as container, you can use a Placeholder or Panel web server control.

In some instances, you might want to create both static text and controls. To create static text, you can use either a Literal or a Label web server control. You can then add these controls to the container as you would any other control.

Note : Manipulating controls programmatically is often used when creating composite controls.

To Add a Control to a Web Forms Page Programmatically 

Create an instance of the control and its properties

Label firstname = new Label();
firstname.Text = " Enter First Name";

Controls are typically added to the page during the page's load stage.

Add the New Control to the Controls Collection of a Container Already on the Page

Panel1.Controls.Add(firstname);

Note : Because Controls is a collection, you can use the AddAt  method to place the new control at a specific location- for example, in front of other controls. However, this approach can introduce errors into the page.

The following shows the event handler for the SelectedIndexChanged event of a control called DropDownList1. The handler creates as many label controls as the user has selected from the dropdown list. The labels display the word label plus a counter. The container for the controls is a Placeholder Web server control called Placeholder1.

Note : User input in a Web Forms page can include potentially malicious client script. By default, the web forms page validates that user input does not include script or HTML elements.

Complete Code


           <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .style1
        {
            font-size: xx-large;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <span class="style1">How many label , you want to create</span><br />
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
            Height="25px" onselectedindexchanged="DropDownList1_SelectedIndexChanged" 
            Width="113px">
            <asp:ListItem>Select</asp:ListItem>
            <asp:ListItem>5</asp:ListItem>
            <asp:ListItem>6</asp:ListItem>
            <asp:ListItem>7</asp:ListItem>
        </asp:DropDownList> 
     
    </div>
    <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
    </form>
</body>
</html>

//Code Behind Code

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

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        int numbers = Convert.ToInt32(DropDownList1.SelectedItem.Text);
        for (int i = 0; i <=numbers ; i++)
        {
            Label l1 = new Label();
            l1.Text = "Label" + i.ToString();
            l1.ID = "Label" + i.ToString();
            PlaceHolder1.Controls.Add(l1);
            PlaceHolder1.Controls.Add(new LiteralControl("<br/>"));

        }
        
        
    }
}

Code generate the following output

How to add Controls Dynamically in ASP.NET

Protected by Copyscape Online Copyright Protection Software

0 comments :

Post a Comment

 
Toggle Footer
Top