Need help with C# code


D

Durango2008

Hi, I have a form which contains a table(Table1), within that table I have
label with an ID set to "1A".
Using something like:

if(Table1.FindControl("1B") != null)
{
labelID = "1C";
}
else if(Table1.FindControl("1A") != null)
{
labelID = "1B";
}

Label lblType = new Label();
lblType.ID = labelID;

TableRow tr = new TableRow();

TableCell tc = new TableCell();

tc.HorizontalAlign = HorizontalAlign.Center;

tr.Controls.Add(tc);

tc.Controls.Add(typeLabel);

Table11.Controls.Add(tr);

So whenever this code runs if it finds the latest Label for example "1B" it
will add a new tr and cell with an updated label "1C" or if it finds "1A" it
will update with new label "1B".

It does work with the initial label that is set in the .aspx file however
when run it again it will not find the new label to reupdate things.

Anyone have any suggestions?

thank you for any advice.
 
Ad

Advertisements

P

pipo

What do you mean by 'run it again'??
When Postbacking...
the whole Page will be new, so make sure to add your newly created control
as well otherwise it will not exists.
 
D

Durango2008

pipo said:
What do you mean by 'run it again'??
When Postbacking...
the whole Page will be new, so make sure to add your newly created control
as well otherwise it will not exists.

I do add the new control with the lines:
TableRow tr = new TableRow();
TableCell tc = new TableCell();
tc.HorizontalAlign = HorizontalAlign.Center;
tr.Controls.Add(tc);
tc.Controls.Add(typeLabel);
Table11.Controls.Add(tr);

Did you mean something else?
 
P

pipo

What do you mean by 'run it again'??


Durango2008 said:
I do add the new control with the lines:
TableRow tr = new TableRow();
TableCell tc = new TableCell();
tc.HorizontalAlign = HorizontalAlign.Center;
tr.Controls.Add(tc);
tc.Controls.Add(typeLabel);
Table11.Controls.Add(tr);

Did you mean something else?
 
Ad

Advertisements

P

pipo

When posted back the 'original' page will be build and displayed.
So when you recieve a postback make sure to make the control there as well.
The first time looks like it works because there you build the control in.
With the next postback that control isnot there anymore, create it and then
it will work.

Hope this helps.

Simple example:
2 buttons on a page.
By clicking Button1 a label is created.
Now click Button2.......label is disappeared, right?
So, re create the label in Page_Load when it is a postback and it is needed
(e.g. Button1 was clicked)
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{}

protected void Button1_Click(object sender, EventArgs e)
{
Label lbl = new Label();
lbl.Text = "New control";
Controls.Add(lbl);
}
protected void Button2_Click(object sender, EventArgs e)
{}
}
 
Ad

Advertisements


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

Top