Hi,
I have a page bill.aspx and its code-behind bill.aspx.cs.
On bill.aspx I have:
===
Select a month: <asp:dropdownlist runat="server" id="lstDate"
autopostback="True" />
<br>
<asp:table runat="server" id="tabBill" />
<br>
<asp:button runat="server" id="btnSave" text="Save" cssclass="button" />
On bill.aspx.cs I got:
===
protected System.Web.UI.WebControls.Table tabBill;
protected System.Web.UI.WebControls.DropDownList lstDate;
protected System.Web.UI.WebControls.Button btnSave;
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
FillDateList(lstDate);
}
}
private void FillDateList(DropDownList lst)
{
//fill lstDate
}
private void lstDate_SelectedIndexChanged(object sender, System.EventArgs e)
{
if(lstDate.SelectedIndex>-1)
{
string sdate=lstDate.SelectedItem.Value;
GPerson gp=new GPerson();
string s=gp.GetAccountByRepXml(repID);
if(s.Length>0)
{
TableRow r;
TableCell c;
TextBox txt;
RegularExpressionValidator v;
r=new TableRow();
c=new TableCell();
c.ID="ct0";
c.Text="";
c.Visible=false;
r.Cells.Add(c);
c=new TableCell();
c.ID="ct1";
c.Text="ESID";
r.Cells.Add(c);
c=new TableCell();
c.ID="ct2";
c.Text="Actual kWh";
r.Cells.Add(c);
c=new TableCell();
c.ID="ct3";
c.Text="";
c.Visible=false;
r.Cells.Add(c);
tabBill.Rows.Add(r);
XmlDocument xd=new XmlDocument();
xd.LoadXml("<root>"+s+"</root>");
XmlNodeList xnl=xd.SelectNodes("root/account");
int j=1;
foreach(XmlNode xn in xnl)
{
string acctNum="";
int billUsage=0, electID=0, acctID=0;
//get value for above variables from xml nodes
r=new TableRow();
c=new TableCell();
c.ID="c0"+j.ToString();
c.Text=acctID.ToString();
c.Visible=false;
r.Cells.Add(c);
c=new TableCell();
c.ID="c1"+j.ToString();
c.Text=acctNum;
r.Cells.Add(c);
c=new TableCell();
c.ID="c2"+j.ToString();
if(billUsage>0)
{
c.Text=billUsage.ToString();
}
else
{
txt=new TextBox();
txt.ID="txt"+j.ToString();
c.Controls.Add(txt);
v=new RegularExpressionValidator();
v.ControlToValidate="txt"+j.ToString();
v.ErrorMessage="Usage must be integer";
v.Display=ValidatorDisplay.Dynamic;
v.ValidationExpression="\\d+";
c.Controls.Add(v);
}
r.Cells.Add(c);
c=new TableCell();
c.ID="c3"+j.ToString();
c.Text=electID.ToString();
c.Visible=false;
r.Cells.Add(c);
tabBill.Rows.Add(r);
j++;
}//end of foreach(XmlNode xn in xnl)
xnl=null;
xd=null;
}//end of if(s.Length>0)
gp=null;
}
}
private void btnSave_Click(object sender, System.EventArgs e)
{
if(lstDate.SelectedIndex>-1)
{
Response.Write(tabBill.Rows.Count.ToString());
string endDate=lstDate.SelectedItem.Value.Trim();
foreach(TableRow r in tabBill.Rows)
{
if (r.Cells[2].HasControls())
{
foreach(Control ctr in r.Cells[2].Controls)
{
if (ctr.GetType().Name=="TextBox")
{
TextBox txt1 = (TextBox)ctr;
int billUsage=Tools.ToInt(txt1.Text.Trim());
int electID=Tools.ToInt(r.Cells[3].Text.Trim());
int acctID=Tools.ToInt(r.Cells[0].Text.Trim());
if(billUsage>0)
{
GPerson gp=new GPerson();
ok=gp.UpdateAcountMonthBillable(acctID,electID,bil lUsage,endDate);
gp=null;
}
}
}
}
}//end of foreach(TableRow r in tabBill.Rows)
}//end of if(lstDate.SelectedIndex>-1)
}
After selecting a month in lstDate, the dynamic generated table shows up
with content got in lstDate_SelectedIndexChanged(). But in btnSave_Click(),
Response.Write(tabBill.Rows.Count.ToString()) shows zero. Would you
please tell me why the dynamic generated rows and cells in table tabBill
disappears?
Thank you
hb