c# - ASP.net dynamic grid of textboxes -
i have 2 lists of strings , i'd dynamically create grid of textboxes:
list<string> x = {"a", "b", "c"}; list<string> y = {"1", "2", "3", "4"}; b c 1 tbx tbx tbx 2 tbx tbx tbx 3 tbx tbx tbx 4 tbx tbx tbx [button]
when enter data textboxes , click button, i'd able iterate on these textboxes , determine "x" , "y" coordinates associated each textbox.
i can figure out how dynamically create grid of textboxes, think i'll run issues when post data. how values dynamically created textboxes after postback?
i'll have no issue doing iteration , actual logic can't figure out how data container can iterated. have dynamically create textboxes again while reading posted data? like?
here approach. can have place holder in asp.net page. in code behind dynamically create table dynamic text boxes , insert inside place holder. in instance have control on id of each textbox. on post values entered text boxes retained (since ids definite can javascript manipulation well)
here aspx
<%@ page language="c#" autoeventwireup="true" codebehind="dynamiccontrols.aspx.cs" inherits="test2.dynamiccontrols" %> <!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:placeholder id="phdynamictable" runat="server"></asp:placeholder> <asp:button runat="server" id="btnsubmit" text="submit" onclick="btnsubmit_click" /> </div> </form> </body> </html>
code behind
using system; using system.collections.generic; using system.linq; using system.web; using system.web.ui; using system.web.ui.webcontrols; namespace test2 { public partial class dynamiccontrols : system.web.ui.page { protected void page_load(object sender, eventargs e) { } protected override void onpreinit(eventargs e) { base.onpreinit(e); createtextboxesintable(); } private void createtextboxesintable() { phdynamictable.controls.clear(); list<string> x = new list<string>() { "a", "b", "c" }; list<string> y = new list<string>() { "1", "2", "3", "4" }; table table = new table(); table.id = "dynamictable"; tablerow tr; foreach (string y in y) { tr = new tablerow(); foreach (string x in x) { tablecell tc = new tablecell(); textbox textbox = new textbox(); textbox.id = "txt_" + x + y; tc.controls.add(textbox); tr.cells.add(tc); } table.rows.add(tr); } phdynamictable.controls.add(table); } protected void btnsubmit_click(object sender, eventargs e) { if (phdynamictable.controls.count > 0) { table dynamictable = (table)phdynamictable.findcontrol("dynamictable"); if (dynamictable != null) { foreach (tablerow tr in dynamictable.rows) { foreach (tablecell tc in tr.cells) { textbox textbox = (textbox)tc.controls[0]; string text = textbox.text; //do whatever want control } } } } } } }
few screen shots
input screen data
on postback (submit)
after submit
Comments
Post a Comment