I want to show Components in a tabs , so first of all create few components. In this project we have three components, First View Component public class AllViewComponent : ViewComponent { private readonly UserManager<ApplicationUser> _userManager; public AllViewComponent(UserManager<ApplicationUser> userManager) { _userManager = userManager; } public async Task<IViewComponentResult> InvokeAsync() { List<StudentViewModel> allUsers = new List<StudentViewModel>(); var items = await _userManager.Users.ToListAsync(); foreach (var item in items) { allUsers.Add(new StudentViewModel {Id=item.Id, EnrollmentNo = item.EnrollmentNo, FatherName = item.FatherName, Name = item.Name, Age = item.Age, Birthdate = item.Birthdate, Address = item.Address, Gender = item.Gender, Email = item.Email }); }
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.
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/>"));
}
}
}
Comments
Post a Comment