Hi,Recently i had the follwing problem and i provided the solution for that one using the below code..My requirement is i have a table. and in that i have two rows(trs)
in first row i have two tds .In first td i have a textbox and in second td i have a button named “save”
In second row (tr) i have only one td ……..this td consists of a button named “add new row”if i click on the “add new row ” button another row has to be generated dynamically with textbox and button like first row.
Solution
<!–File:Dynamically_Generate_Row.aspx–><%@ Page Language=”C#” AutoEventWireup=”true”
CodeFile=”Dynamically_Generate_Row.aspx.cs”
Inherits=”Dynamically_Generate_Row” %><!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>
<table>
<% for (int i=0; i<_nRowCount; i++) {%>
<tr>
<td>
<input type=”text” value=”” id=”text<%=i %>>”/>
</td>
<td>
<input type=”button” value=”Save” id=”btn<%=i %>>” />
</td>
<% } %> </tr>
<tr>
<td>
<asp:Button ID=”btn_CreateRow” runat=”server” OnClick=”OnCreateRow” Text=”AddNew”/>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
//File:Dynamically_Generate_Row.aspx.csusing 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;public partial class Dynamically_Generate_Row : System.Web.UI.Page
{
public int _nRowCount=1;
protected void Page_Load(object sender, EventArgs e)
{
if (null == Session[“row_count”])
Session[“row_count”] = “1”;
_nRowCount = System.Convert.ToInt32(Session[“row_count”]);
} protected void OnCreateRow(object sender, EventArgs e)
{
if (null == Session[“row_count”])
Session[“row_count”] = “1”;
else
{
int i = (System.Convert.ToInt32(Session[“row_count”]) + 1);
Session[“row_count”] = i.ToString();
} _nRowCount = System.Convert.ToInt32(Session[“row_count”]);
}
}