Check out this sample
ASPX
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="Test" %>
<!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>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="No. of Products:"></asp:Label>
<asp:DropDownList ID="ddlItems" runat="server" AutoPostBack="True"
onselectedindexchanged="ddlItems_SelectedIndexChanged">
<asp:ListItem Value="0">-Select-</asp:ListItem>
<asp:ListItem Value="1">1</asp:ListItem>
<asp:ListItem Value="2">2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
</asp:DropDownList>
<br />
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</div>
</form>
</body>
</html>
背 景
using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void ddlItems_SelectedIndexChanged(object sender, EventArgs e)
{
if (ddlItems.SelectedIndex > 0)
{
DataTable dt = new DataTable();
int cntr=Convert.ToInt32(ddlItems.SelectedValue);
dt.Columns.Add(new DataColumn("SNO",typeof(int)));
dt.Columns.Add(new DataColumn("ProductName",typeof(string)));
dt.Columns["SNO"].AutoIncrement=true;
dt.Columns["SNO"].AutoIncrementSeed = 1;
for(int i=1;i<=cntr;i++)
{
DataRow dr=dt.NewRow();
dr["ProductName"]= " Sample Product Name for Item #"+ i;
dt.Rows.Add(dr);
}
GridView1.DataSource=dt;
GridView1.DataBind();
}
}
}