Help needed: Problem with dynamic generated TableRow and TableCell

H

hb

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,billUsage,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
 
G

Guest

Dynamically generated controls and events for those controls(if any) will need to be regenerated everytime there's a postback

Suresh

----- hb wrote: ----

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 lstDat


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 node

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()

els

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,billUsage,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
 
M

Malek

you did not persist it in any way... It has nothing to do with being
dynamically generated ...

let me make it easier :

int x=0;
private void Button1_Click(object sender, System.EventArgs e)
{
x = 20;
}
private void Button2_Click(object sender, System.EventArgs e)
{
Response.write (x.ToString());
}

what value do you expect x to have on Button2_Click ?

The page has re_loaded (a new Page object has been instanciated, and all
....). so you either persist it, or get it back from the dropdownList
(viewstate persists it) ...

hb said:
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,billUsage,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
 
G

Guest

Hi HB,

Thank you for posting in the community!

Based on my understanding, you want to dynamic populate your server side
Table in a dropdownlist's SelectedIndexChanged event. But, then you want to
save the table content in a Button.Click event.

=======================================
Just as Malek said, the problem is that your Table is refreshed when
postback. So all the dynamicly added content is lost.
I have writen a sample project to prove this:

protected System.Web.UI.WebControls.DropDownList lstDate;
protected System.Web.UI.WebControls.Table tabBill;
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)
{
ListItem li1=new ListItem("a1","b1");
ListItem li2=new ListItem("a2","b2");
ListItem li3=new ListItem("a3","b3");
ListItem li4=new ListItem("a4","b4");
ListItem li5=new ListItem("a5","b5");

lst.Items.Add(li1);
lst.Items.Add(li2);
lst.Items.Add(li3);
lst.Items.Add(li4);
lst.Items.Add(li5);
}

private void btnSave_Click(object sender, System.EventArgs e)
{
this.Response.Write(tabBill.Rows.Count.ToString());
}

private void lstDate_SelectedIndexChanged(object sender, System.EventArgs e)
{
foreach(ListItem li in lstDate.Items)
{
TableCell tc1=new TableCell();
tc1.Text=li.Text+ lstDate.SelectedIndex.ToString();

TableCell tc2=new TableCell();
tc2.Text=li.Value+lstDate.SelectedIndex.ToString();

TableRow tr=new TableRow();
tr.Cells.Add(tc1);
tr.Cells.Add(tc2);

tabBill.Rows.Add(tr);
}
}

The output will always be 0.

I think what you want to do is dynamicly populate the table based on the
index(or value) of the DropDownList. So you can store the value in the
viewstate, then populate the table in each postback. So I modify the sample
code like this:

protected System.Web.UI.WebControls.DropDownList lstDate;
protected System.Web.UI.WebControls.Table tabBill;
protected System.Web.UI.WebControls.Button btnSave;

private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
FillDateList(lstDate);
}
else
{
if(this.ViewState["SelectedIndex"]!=null)
{
populatetable((int)this.ViewState["SelectedIndex"]);
}
}
}

private void FillDateList(DropDownList lst)
{
ListItem li1=new ListItem("a1","b1");
ListItem li2=new ListItem("a2","b2");
ListItem li3=new ListItem("a3","b3");
ListItem li4=new ListItem("a4","b4");
ListItem li5=new ListItem("a5","b5");

lst.Items.Add(li1);
lst.Items.Add(li2);
lst.Items.Add(li3);
lst.Items.Add(li4);
lst.Items.Add(li5);
}

private void btnSave_Click(object sender, System.EventArgs e)
{
this.Response.Write(tabBill.Rows.Count.ToString());
}

private void lstDate_SelectedIndexChanged(object sender, System.EventArgs e)
{
this.ViewState["SelectedIndex"]=lstDate.SelectedIndex;
populatetable(lstDate.SelectedIndex);
}

private void populatetable(int index)
{
foreach(ListItem li in lstDate.Items)
{
TableCell tc1=new TableCell();
tc1.Text=li.Text+ index.ToString();

TableCell tc2=new TableCell();
tc2.Text=li.Value+index.ToString();

TableRow tr=new TableRow();
tr.Cells.Add(tc1);
tr.Cells.Add(tc2);

tabBill.Rows.Add(tr);
}
}

Then, the output will be 5, and you can save the table content as you like.

=====================================
Please apply my suggestion above and let me know if it helps resolve your
problem.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.
Have a nice weekend!!

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
G

Guest

you did not persist it in any way..

By this I assume you mean persisting the drop down list value

The TableCell, TableRow which were dynamically added in the SelectIndexChange method will need to re generated again on postback. These won't magically appear after postback if you just persist these objects(is it even possible??) or the drop down list value in the viewstate

So what do you mean by this?
It has nothing to do with being dynamically generated ..

Suresh


----- Malek wrote: ----

you did not persist it in any way... It has nothing to do with bein
dynamically generated ..

let me make it easier

int x=0
private void Button1_Click(object sender, System.EventArgs e

x = 20

private void Button2_Click(object sender, System.EventArgs e

Response.write (x.ToString())


what value do you expect x to have on Button2_Click

The page has re_loaded (a new Page object has been instanciated, and al
....). so you either persist it, or get it back from the dropdownLis
(viewstate persists it) ..

hb said:
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 lstDat
private void lstDate_SelectedIndexChanged(object sender, System.EventArg
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)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 node
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()

els

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,billUsage,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
 
G

Guest

Hi Suresh,

So, as I think the simplest way is only persist the KEY value for
populating the table. Just as the sample code I pasted in another reply of
mine, I persisted the selected value in the dropdownlist, then in each
postback, I will re-build the table through the selected item value.

This way will also have the benefit of performance(Not too large viewstate)

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
G

Guest

Hi Suresh,

Is your problem resolved? Does my reply make sense to you?

If you still have anything unclear, please feel free to tell me, I will
help you.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top