Need help with C# code

Discussion in 'ASP .Net' started by Durango2008, Mar 11, 2008.

  1. Durango2008

    Durango2008 Guest

    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.
     
    Durango2008, Mar 11, 2008
    #1
    1. Advertising

  2. Durango2008

    pipo Guest

    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.




    "Durango2008" <0m> schreef in bericht
    news:b8zBj.16884$...
    > 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.
    >
     
    pipo, Mar 11, 2008
    #2
    1. Advertising

  3. Durango2008

    Durango2008 Guest

    "pipo" <> wrote in message
    news:...
    > 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?
     
    Durango2008, Mar 12, 2008
    #3
  4. Durango2008

    pipo Guest

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


    "Durango2008" <0m> schreef in bericht
    news:zfOBj.16215$...
    >
    > "pipo" <> wrote in message
    > news:...
    >> 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?
    >
    >
     
    pipo, Mar 12, 2008
    #4
  5. Durango2008

    Durango2008 Guest

    "pipo" <> wrote in message
    news:%...
    > What do you mean by 'run it again'??
    >

    I meant postback sorry for confusion.
     
    Durango2008, Mar 12, 2008
    #5
  6. Durango2008

    pipo Guest

    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)
    {}
    }





    "Durango2008" <0m> schreef in bericht
    news:67VBj.20421$...
    >
    > "pipo" <> wrote in message
    > news:%...
    >> What do you mean by 'run it again'??
    >>

    > I meant postback sorry for confusion.
    >
    >
     
    pipo, Mar 13, 2008
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. TN Bella
    Replies:
    1
    Views:
    2,478
    Edward
    Jun 18, 2004
  2. Teep
    Replies:
    2
    Views:
    767
    Larry Marburger
    Jun 21, 2004
  3. John
    Replies:
    1
    Views:
    402
    John C. Bollinger
    Aug 19, 2004
  4. =?Utf-8?B?Q2hyaXM=?=

    Help Help. I really need some help with this

    =?Utf-8?B?Q2hyaXM=?=, Jan 31, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    568
    =?Utf-8?B?SmFzb24gVmVybWlsbGlvbg==?=
    Jan 31, 2007
  5. ElementX
    Replies:
    9
    Views:
    497
    RedGrittyBrick
    Oct 1, 2008
Loading...

Share This Page