This article mainly introduces viewstate and DataTable dynamic input data samples, need friends can refer to the following
Code as follows: <%@ Page language= "C #" enableviewstate= "true"%> <%@ Import namespace= "System.Data"%> ;! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.w3.org/TR/xhtml1/DTD/ Xhtml1-transitional.dtd "> <script runat=" Server "> private DataTable stovetable = null; protected void Page_Load (object sender, EventArgs e) { if (!) Page.IsPostBack) { //create EmptyDataTemplate this. Gridview_list. DataBind (); } } protected void Gridview_list_rowdatabound (object sender, GridViewRowEventArgs e) &n Bsp { if (E.row.rowtype = datacontrolrowtype.datarow) { String usage = Datab Inder. Eval (E.row.dataitem, "usage"). ToString (); String steelkind = DataBinder.Eval (E.row.dataitem, "Steelkind"). ToString (); String Castington = DataBinder.Eval (e.row. DataItem, "Castington"). ToString (); DropDownList x1 = E.row.findcontrol ("x1") as DropDownList; DropDownList x2 = E.row.findcontrol ("x2") as DropDownList; TextBox x3 = E.row.findcontrol ("x3") as TextBox; X3. Text = Castington; ListItem xx1 = x1. Items.findbyvalue (usage); if (xx1!= null) xx1. Selected = true; ListItem xx2 = x2. Items.findbyvalue (Steelkind); if (xx2!= null) xx2. Selected = true; } } protected void LinkButton1_Click (object sender, EventArgs e) { DropDownList x1, x2; TextBox x3; if (gridview_list. Rows.Count = 0) { x1 = gridview_list. Controls[0]. Controls[0]. FindControl ("x1") as DropDownList; x2 = gridview_list. Controls[0]. Controls[0]. FindControl ("X2") as DropDownList; x3 = gridview_list. Controls[0]. Controls[0]. FindControl ("x3") as TextBox; } Else { GridViewRow r = gridview_list. Footerrow; x1 = R.findcontrol ("x1") as DropDownList; x2 = R.findcontrol ("x2") as DropDownList; x3 = R.findcontrol ("x3") as TextBox; } if (viewstate["dt"] = null) { stovetable = new DataTable (); STOVETABLE.COLUMNS.ADD ("Usage", typeof (String)); STOVETABLE.COLUMNS.ADD ("Steelkind", typeof (String)); STOVETABLE.COLUMNS.ADD ("Castington", typeof (String)); } Else { stovetable = (DataTable) viewstate["DT"]; &N Bsp } DataRow NewRow = Stovetable.newrow (); newrow["usage"] = x1. SelectedValue; newrow["steelkind"] = x2. SelectedValue; newrow["Castington"] = x3. Text; STOVETABLE.ROWS.ADD (NewRow); viewstate["dt"] = stovetable; this. Gridview_list. DataSource = stovetable; this. Gridview_list. DataBind (); } protected void Linkbutton2_click (object sender, EventArgs e) { if (viewstate[) DT "] = = null) { return; } stovetable = (DataTable) ViewState ["DT"]; if (StoveTable.Rows.Count < 1) return; StoveTable.Rows.RemoveAt (stovetable.rows.count-1); viewstate["dt"] = stovetable; this. Gridview_list. DataSource = stovetable; this. Gridview_list. DataBind (); } protected void x1_selectedindexchanged (object sender, EventArgs e) { DROPDOWNL IST x1 = sender as DropDownList; GridViewRow r = x1. Parent.parent as GridViewRow;   if (viewstate["dt"] = = null) { Response.Write ("Error"); RET Urn } stovetable = (DataTable) viewstate["DT"]; stovetable.rows[r.rowindex]["usage"] = x1. SelectedValue; viewstate["dt"] = stovetable; this. Gridview_list. DataSource = stovetable; this. Gridview_list. DataBind (); } protected void x2_selectedindexchanged (object sender, EventArgs e) { DROPDOWNL ist x2 = sender as DropDownList; GridViewRow r = X2. Parent.parent as GridViewRow; if (viewstate["dt"] = null) { Response.Write ("Error"); &NB Sp Return } stovetable = (DataTable) viewstate["DT"]; stovetable.rows[r.rowindex]["steelkind"] = x2. SelectedValue; viewstate["dt"] = stovetable; this. Gridview_list. DatAsource = stovetable; this. Gridview_list. DataBind (); } protected void x3_textchanged (object sender, EventArgs e) { TextBox x3 = sender as TextBox; GridViewRow r = x3. Parent.parent as GridViewRow; if (viewstate["dt"] = null) { Response.Write ("Error"); &NB Sp Return } stovetable = (DataTable) viewstate["DT"]; stovetable.rows[r.rowindex]["Castington"] = x3. Text; viewstate["dt"] = stovetable; this. Gridview_list. DataSource = stovetable; this. Gridview_list. DataBind (); } </script> <html xmlns= "http://www.w3.org/1999/xhtml" > <head id= "Head1" runat= "Server" > <title></title> </head> <body> <form id= "Form1" runat= "Server" > < Asp:gridview id= "gridview_list" runat= "Server" showfooter= "true" AutogeneraTecolumns= "false" onrowdatabound= "Gridview_list_rowdatabound" > <emptydatatemplate > <table style= "border-collapse:collapse; width:100% "cellspacing=" 0 "rules=" all " border=" 1 "> <tr> <th scope= "col" > Select 1 &NB Sp </th> <th scope= "col" > &NB Sp Option 2 </th> <th scope= "col" > input text </th> </tr> & nbsp <tr><td> <asp:dropdownlist id= "x1" runat= "Server" & Gt <ASP:LISTITEM>L0≪/asp:listitem> <asp:ListItem>L1</asp:ListItem> <asp:ListItem>L2</asp:ListItem> <asp :listitem>l3</asp:listitem> </asp:DropDownList> & nbsp </td><td> <asp:dropdownlist id= "x2" runat= "Server" > &N Bsp <asp:ListItem>10#</asp:ListItem> <asp: listitem>20#</asp:listitem> <asp:listitem>30#</asp: listitem> <asp:ListItem>40#</asp:ListItem> </asp:DropDownList> </td><td> &NBS P <asp:textbox id= "x3" runat= "SerVer "></asp:TextBox> </td></tr> </table> & nbsp </EmptyDataTemplate> <Columns> <asp:templatefield headertext= Option 1 "> <ItemTemplate> <asp:dropdownlist id=" x1 " runat= "Server" autopostback= "true" onselectedindexchanged= "x1_selectedindexchanged" > & nbsp <asp:ListItem>L0</asp:ListItem> <ASP:LISTITEM>L1 </asp:ListItem> <asp:ListItem>L2</asp:ListItem> <asp:ListItem>L3</asp:ListItem> </as p:dropdownlist> </ItemTemplate> <FooterTemplate> & nbsp <asp:D ropdownlist id= "x1" runat= "Server" > <asp:listitem>l0</asp: listitem> <asp:ListItem>L1</asp:ListItem> & nbsp <asp:ListItem>L2</asp:ListItem> <asp:listitem >L3</asp:ListItem> </asp:DropDownList> < /footertemplate> </asp:TemplateField> <asp:templatefield headertext= "SELECT 2" > <ItemTemplate> <asp:dropdown List id= "x2" runat= "Server" autopostback= "true" onselectedindexchanged= "x2_selectedindexchanged" > <asp:ListItem>10#</asp:ListItem> <as P:listitem>20#</asp:listitem>   <asp:ListItem>30#</asp:ListItem> <asp: listitem>40#</asp:listitem> </asp:DropDownList> & nbsp </ItemTemplate> <FooterTemplate> <ASP:DROPD Ownlist id= "x2" runat= "Server" > <asp:listitem>10#</asp:listitem > <asp:ListItem>20#</asp:ListItem> &N Bsp <asp:ListItem>30#</asp:ListItem> <asp:listitem>40# </asp:ListItem> </asp:DropDownList> </foote rtemplate> </asp:TemplateField> <asp:templatefield headertext= " Input text ">   <ItemTemplate> <asp:textbox id= "x3" runat= "Server" autopostback= "true" Ontextchanged= "x3_textchanged" ></asp:TextBox> </ItemTemplate> <FooterTemplate> <asp:textbox id= "x3" runat= "Server" > </asp:TextBox> </FooterTemplate> </asp:templatefield > </Columns> </asp:GridView> <asp:linkbutton id= "LinkButton1" runat= Server "text=" Add Content "onclick=" LinkButton1_Click "></asp:LinkButton> <asp:linkbutton id=" LinkButton2 "runat=" server "text=" Delete content "onclick=" Linkbutton2_click "></asp:LinkButton> </form> </body> </html>