Let us suppose that you have four controls (TextBox, DropDownList, ListBox ), and your requirement is that when the user enters texts or make a selection, all these items/values get stored inside an ArrayList and then this Array List is used as the Data Source for a Gridview control.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DynamicData.aspx.cs" Inherits="DynamicData" %>
<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server" /><br />
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>A</asp:ListItem>
<asp:ListItem>B</asp:ListItem>
<asp:ListItem>C</asp:ListItem>
</asp:DropDownList><br />
<asp:DropDownList ID="DropDownList2" runat="server">
<asp:ListItem>A</asp:ListItem>
<asp:ListItem>B</asp:ListItem>
<asp:ListItem>C</asp:ListItem>
</asp:DropDownList><br />
<asp:ListBox ID="ListBox1" runat="server">
<asp:ListItem>A</asp:ListItem>
<asp:ListItem>B</asp:ListItem>
<asp:ListItem>C</asp:ListItem>
</asp:ListBox><br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Txt1" HeaderText="TextBox1" />
<asp:BoundField DataField="DDL1" HeaderText="DropDownList1" />
<asp:BoundField DataField="DDL2" HeaderText="DropDownList2" />
<asp:BoundField DataField="Lst1" HeaderText="ListBox1" />
</Columns>
</asp:GridView>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /></div>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
public partial class DynamicData : System.Web.UI.Page
{
private List<Row> Data
{
get
{
if (this.ViewState["Data"] == null)
{
this.ViewState["Data"] = new List<Row>();
}
return this.ViewState["Data"] as List<Row>;
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.GetData();
}
}
private void GetData()
{
GridView1.DataSource = this.Data;
GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
Row obj = new Row();
obj.Txt1 = TextBox1.Text;
obj.DDL1 = DropDownList1.SelectedValue;
obj.DDL2 = DropDownList2.SelectedValue;
obj.Lst1 = ListBox1.SelectedValue;
this.Data.Add(obj);
this.GetData();
}
}```
```csharp
[Serializable()]
public class Row
{
private string _txt1;
private string _ddl1;
private string _ddl2;
private string _Lst1;
public string Lst1
{
get { return _Lst1; }
set { _Lst1 = value; }
}
public string DDL2
{
get { return _ddl2; }
set { _ddl2 = value; }
}
public string DDL1
{
get { return _ddl1; }
set { _ddl1 = value; }
}
public string Txt1
{
get { return _txt1; }
set { _txt1 = value; }
}
}