Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Problem with Placeholder control [modified]

Problem with Placeholder control [modified]

Scheduled Pinned Locked Moved ASP.NET
helpcomdesignquestion
24 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E eyeseetee

    I am adding controls to a placeholder on the fly. I could have any number of controls in there but its usually about 5. I am adding a label and a textbox to the placeholder. Now when I loop the placeholder I have the following code: The textbox value works fine it finds the correct value, but I cant find the label value, why is this? Any help would be great. thanks

    PlaceHolder placeHolder1 = (PlaceHolder)FindControl("PlaceHolder2");
    foreach (Control c in PlaceHolder2.Controls)
    {
    if (c.GetType().ToString().Equals("System.Web.UI.WebControls.TextBox"))
    {
    Label lb2 = (Label)c;
    TextBox tb2 = (TextBox)c;

                   SqlCommand cmdinsertliftunion = new SqlCommand("INSERT INTO table(field1, field2) VALUES ('" + lb2.Text + "', '" + tb2.Text + "')", con);
                   cmdinsert.ExecuteNonQuery();
                   }
             }
    

    Deliver yesterday, code today, think tomorrow. "http://www.heuse.com/cphumor.htm"

    modified on Wednesday, October 8, 2008 5:39 AM

    J Offline
    J Offline
    J4amieC
    wrote on last edited by
    #10

    .netman wrote:

    if (c.GetType().ToString().Equals("System.Web.UI.WebControls.TextBox"))

    The above line is awful use the is (or as) keyword.

    if(c is TextBox)
    {
    }

    or

    TextBox tb = c as TextBox
    if(tb != null)
    {
    }

    Also, as was correctly stated above, you need to check if 'c' is a Label as well. Note that these 2 controls both implement ITextControl which has a .Text property that you can use. This will be my final answer to your questions. see above response for why.

    modified on Wednesday, October 8, 2008 8:38 AM

    1 Reply Last reply
    0
    • E eyeseetee

      The problem appears to be with the if statement. If I use label in the if statement and hardcode the textbox value it works and if I use the textbox in the if statement and hardcode the label it also works. it seems that I cant find the label and textbox values if Im using the if statement below, is there a way around it? thanks so far if (c.GetType().ToString().Equals("System.Web.UI.WebControls.TextBox"))

      Deliver yesterday, code today, think tomorrow. "http://www.heuse.com/cphumor.htm"

      T Offline
      T Offline
      thomasa
      wrote on last edited by
      #11

      if you know the ID of the label and the textbox, the best solution will probably be this:

      Lable lb2 = (Lable)PlaceHolder2.FindControl("lb2");
      TextBox tb2 = (TextBox)PlaceHolder2.FindControl("tb2");
      if(lb2 != null && tb2 != null)
      {
      SqlCommand cmdinsertliftunion = new SqlCommand("INSERT INTO table(field1, field2) VALUES ('" + lb2.Text + "', '" + tb2.Text + "')", con);
      cmdinsert.ExecuteNonQuery();
      }

      If you have multiple lables and textboxes in the placeholder and you don't know how many pairs of them you have. Then you probably need to make all of this in another way. I would recommand that you add a DataGrid to the placeholder and in the DataGrid you add pairs of lable and textbox. Then the code would look something like this.

      DataGrid dg = (DataGrid)PlaceHolder2.FindControl("dgTheDataGrid");
      foreace(DataGridItem dgi in dg.Items)
      {
      Lable lb2 = (Lable)dgi.FindControl("lb2");
      TextBox tb2 = (TextBox)dgi.FindControl("tb2");
      if(lb2 != null && tb2 != null)
      {
      SqlCommand cmdinsertliftunion = new SqlCommand("INSERT INTO table(field1, field2) VALUES ('" + lb2.Text + "', '" + tb2.Text + "')", con);
      cmdinsert.ExecuteNonQuery();
      }
      }

      modified on Wednesday, October 8, 2008 9:20 AM

      E 2 Replies Last reply
      0
      • J J4amieC

        Quite why this response got downvoted I dont know, but im voting it 4 as a (reasonably) good answer. To .netman: this is not the first time ive seen good responses to your questions downvoted. Last time you said it wasnt you that downvoted, but this is too much of a coincidence! I suggest you are downvoting answers where you either dont like or dont understand the answer given. Therefore, for the time being at least I will personally not be answering any of your questions.

        E Offline
        E Offline
        eyeseetee
        wrote on last edited by
        #12

        J4amieC wrote:

        Last time you said it wasnt you that downvoted, but this is too much of a coincidence!

        Someone has downvoted some of my responses so I suggest you find out who that is and then you will find out who downvoted the other post.

        J4amieC wrote:

        I suggest you are downvoting answers where you either dont like or dont understand the answer given.

        You can suggest but you are wrong

        J4amieC wrote:

        Therefore, for the time being at least I will personally not be answering any of your questions.

        Considering your answer yesterday to one of my posts was a sarcastic 'go and read a beginners tutorial' and then you deleted it once I had quoted from it so frankly I couldnt care what you do

        Deliver yesterday, code today, think tomorrow. "http://www.heuse.com/cphumor.htm"

        J 1 Reply Last reply
        0
        • T thomasa

          if you know the ID of the label and the textbox, the best solution will probably be this:

          Lable lb2 = (Lable)PlaceHolder2.FindControl("lb2");
          TextBox tb2 = (TextBox)PlaceHolder2.FindControl("tb2");
          if(lb2 != null && tb2 != null)
          {
          SqlCommand cmdinsertliftunion = new SqlCommand("INSERT INTO table(field1, field2) VALUES ('" + lb2.Text + "', '" + tb2.Text + "')", con);
          cmdinsert.ExecuteNonQuery();
          }

          If you have multiple lables and textboxes in the placeholder and you don't know how many pairs of them you have. Then you probably need to make all of this in another way. I would recommand that you add a DataGrid to the placeholder and in the DataGrid you add pairs of lable and textbox. Then the code would look something like this.

          DataGrid dg = (DataGrid)PlaceHolder2.FindControl("dgTheDataGrid");
          foreace(DataGridItem dgi in dg.Items)
          {
          Lable lb2 = (Lable)dgi.FindControl("lb2");
          TextBox tb2 = (TextBox)dgi.FindControl("tb2");
          if(lb2 != null && tb2 != null)
          {
          SqlCommand cmdinsertliftunion = new SqlCommand("INSERT INTO table(field1, field2) VALUES ('" + lb2.Text + "', '" + tb2.Text + "')", con);
          cmdinsert.ExecuteNonQuery();
          }
          }

          modified on Wednesday, October 8, 2008 9:20 AM

          E Offline
          E Offline
          eyeseetee
          wrote on last edited by
          #13

          Thanks for the reply, i will try that and then get back to you. For the record I didnt downvote your posts I have actually been responding to you and trying your solutions. Just ignore J4amiec hes just on a power trip

          Deliver yesterday, code today, think tomorrow. "http://www.heuse.com/cphumor.htm"

          1 Reply Last reply
          0
          • T thomasa

            if you know the ID of the label and the textbox, the best solution will probably be this:

            Lable lb2 = (Lable)PlaceHolder2.FindControl("lb2");
            TextBox tb2 = (TextBox)PlaceHolder2.FindControl("tb2");
            if(lb2 != null && tb2 != null)
            {
            SqlCommand cmdinsertliftunion = new SqlCommand("INSERT INTO table(field1, field2) VALUES ('" + lb2.Text + "', '" + tb2.Text + "')", con);
            cmdinsert.ExecuteNonQuery();
            }

            If you have multiple lables and textboxes in the placeholder and you don't know how many pairs of them you have. Then you probably need to make all of this in another way. I would recommand that you add a DataGrid to the placeholder and in the DataGrid you add pairs of lable and textbox. Then the code would look something like this.

            DataGrid dg = (DataGrid)PlaceHolder2.FindControl("dgTheDataGrid");
            foreace(DataGridItem dgi in dg.Items)
            {
            Lable lb2 = (Lable)dgi.FindControl("lb2");
            TextBox tb2 = (TextBox)dgi.FindControl("tb2");
            if(lb2 != null && tb2 != null)
            {
            SqlCommand cmdinsertliftunion = new SqlCommand("INSERT INTO table(field1, field2) VALUES ('" + lb2.Text + "', '" + tb2.Text + "')", con);
            cmdinsert.ExecuteNonQuery();
            }
            }

            modified on Wednesday, October 8, 2008 9:20 AM

            E Offline
            E Offline
            eyeseetee
            wrote on last edited by
            #14

            thomasa wrote:

            Lable lb2 = (Lable)dgi.FindControl("lb2"); TextBox tb2 = (TextBox)dgi.FindControl("tb2");

            But if I use this will I not need to know the ID of the label and textbox? Thats the problem I have is trying to give them an ID as there could be a lot of them which are all created on the fly.

            Deliver yesterday, code today, think tomorrow. "http://www.heuse.com/cphumor.htm"

            T 1 Reply Last reply
            0
            • E eyeseetee

              J4amieC wrote:

              Last time you said it wasnt you that downvoted, but this is too much of a coincidence!

              Someone has downvoted some of my responses so I suggest you find out who that is and then you will find out who downvoted the other post.

              J4amieC wrote:

              I suggest you are downvoting answers where you either dont like or dont understand the answer given.

              You can suggest but you are wrong

              J4amieC wrote:

              Therefore, for the time being at least I will personally not be answering any of your questions.

              Considering your answer yesterday to one of my posts was a sarcastic 'go and read a beginners tutorial' and then you deleted it once I had quoted from it so frankly I couldnt care what you do

              Deliver yesterday, code today, think tomorrow. "http://www.heuse.com/cphumor.htm"

              J Offline
              J Offline
              J4amieC
              wrote on last edited by
              #15

              .netman wrote:

              Considering your answer yesterday to one of my posts was a sarcastic 'go and read a beginners tutorial'

              Far from being sarcastic it was 100% serious. Your inital question on that day indicated you were unsure how to enumerate an arraylist of items. If that is giving you trouble you do indeed need a beginner's book (which is what i suggested). Everyone has to start somewhere.

              E 1 Reply Last reply
              0
              • J J4amieC

                .netman wrote:

                Considering your answer yesterday to one of my posts was a sarcastic 'go and read a beginners tutorial'

                Far from being sarcastic it was 100% serious. Your inital question on that day indicated you were unsure how to enumerate an arraylist of items. If that is giving you trouble you do indeed need a beginner's book (which is what i suggested). Everyone has to start somewhere.

                E Offline
                E Offline
                eyeseetee
                wrote on last edited by
                #16

                An example tutorial for an arraylist closely applied to my situation was what I was looking for. Not a pure beginners book on asp.net which is what you suggested. Oh well this is a pointless arguement as we clearly have totally different opinions on the siutation.

                Deliver yesterday, code today, think tomorrow. "http://www.heuse.com/cphumor.htm"

                J 1 Reply Last reply
                0
                • E eyeseetee

                  thomasa wrote:

                  Lable lb2 = (Lable)dgi.FindControl("lb2"); TextBox tb2 = (TextBox)dgi.FindControl("tb2");

                  But if I use this will I not need to know the ID of the label and textbox? Thats the problem I have is trying to give them an ID as there could be a lot of them which are all created on the fly.

                  Deliver yesterday, code today, think tomorrow. "http://www.heuse.com/cphumor.htm"

                  T Offline
                  T Offline
                  thomasa
                  wrote on last edited by
                  #17

                  Yes, this would requier that you know the id. That's way I suggested that you add a DataGrid where each row(DataGridItem) would contain 1 Label and 1 Textbox

                  T 1 Reply Last reply
                  0
                  • E eyeseetee

                    An example tutorial for an arraylist closely applied to my situation was what I was looking for. Not a pure beginners book on asp.net which is what you suggested. Oh well this is a pointless arguement as we clearly have totally different opinions on the siutation.

                    Deliver yesterday, code today, think tomorrow. "http://www.heuse.com/cphumor.htm"

                    J Offline
                    J Offline
                    J4amieC
                    wrote on last edited by
                    #18

                    .netman wrote:

                    An example tutorial for an arraylist closely applied to my situation was what I was looking for.

                    You seem to be having memory problems, here i'll quote your original question from yesterday: "I have an arraylist which contains a number of items. What is the best way to go through the items in the list. ANy good tutorials on looping through the arraylist." If that isn't screaming "beginning programming book" then its beyond me what is.

                    E 1 Reply Last reply
                    0
                    • T thomasa

                      Yes, this would requier that you know the id. That's way I suggested that you add a DataGrid where each row(DataGridItem) would contain 1 Label and 1 Textbox

                      T Offline
                      T Offline
                      thomasa
                      wrote on last edited by
                      #19

                      Sorry, dident see the dgi. That is acculy not a problem, for in each datagriditem every label would have the id="lb2". as in:

                      DataGrid dg = new DataGrid();
                      dg.ID = "dgTheDataGrid";
                      dg.RunatServer = true;
                      DataGridItem dgi = new DataGridItem();
                      Lable lb2 = new Label();
                      lb2.ID = "lb2";
                      dgi.AddControl(lb2);
                      dg.AddControl(dgi);
                      PlaceHolder2.AddControl(dg);

                      The syntax in this code is probably wrong, but something like this.

                      E 2 Replies Last reply
                      0
                      • J J4amieC

                        .netman wrote:

                        An example tutorial for an arraylist closely applied to my situation was what I was looking for.

                        You seem to be having memory problems, here i'll quote your original question from yesterday: "I have an arraylist which contains a number of items. What is the best way to go through the items in the list. ANy good tutorials on looping through the arraylist." If that isn't screaming "beginning programming book" then its beyond me what is.

                        E Offline
                        E Offline
                        eyeseetee
                        wrote on last edited by
                        #20

                        J4amieC wrote:

                        You seem to be having memory problems,

                        Your're the only problem around here

                        J4amieC wrote:

                        If that isn't screaming "beginning programming book" then its beyond me what is.

                        Yes but your reply(which you deleted to cover your back) just suggested to go get a beginners asp.net book rather than actually suggesting a proper tutorial. You seem to be downvoting my posts as well which makes you a hyporcrite and confirms my suspicions that you are a complete moron.

                        Deliver yesterday, code today, think tomorrow. "http://www.heuse.com/cphumor.htm"

                        J 1 Reply Last reply
                        0
                        • T thomasa

                          Sorry, dident see the dgi. That is acculy not a problem, for in each datagriditem every label would have the id="lb2". as in:

                          DataGrid dg = new DataGrid();
                          dg.ID = "dgTheDataGrid";
                          dg.RunatServer = true;
                          DataGridItem dgi = new DataGridItem();
                          Lable lb2 = new Label();
                          lb2.ID = "lb2";
                          dgi.AddControl(lb2);
                          dg.AddControl(dgi);
                          PlaceHolder2.AddControl(dg);

                          The syntax in this code is probably wrong, but something like this.

                          E Offline
                          E Offline
                          eyeseetee
                          wrote on last edited by
                          #21

                          Thanks for the reply again. Im finding an error with the line DataGridItem dgi = new DataGridItem(); It says that: No overload for method 'DataGridItem' takes '0' arguments

                          Deliver yesterday, code today, think tomorrow. "http://www.heuse.com/cphumor.htm"

                          1 Reply Last reply
                          0
                          • E eyeseetee

                            J4amieC wrote:

                            You seem to be having memory problems,

                            Your're the only problem around here

                            J4amieC wrote:

                            If that isn't screaming "beginning programming book" then its beyond me what is.

                            Yes but your reply(which you deleted to cover your back) just suggested to go get a beginners asp.net book rather than actually suggesting a proper tutorial. You seem to be downvoting my posts as well which makes you a hyporcrite and confirms my suspicions that you are a complete moron.

                            Deliver yesterday, code today, think tomorrow. "http://www.heuse.com/cphumor.htm"

                            J Offline
                            J Offline
                            J4amieC
                            wrote on last edited by
                            #22

                            .netman wrote:

                            Yes but your reply(which you deleted to cover your back)

                            Wrong again chump, go check again its still there. Idiot.

                            E 1 Reply Last reply
                            0
                            • J J4amieC

                              .netman wrote:

                              Yes but your reply(which you deleted to cover your back)

                              Wrong again chump, go check again its still there. Idiot.

                              E Offline
                              E Offline
                              eyeseetee
                              wrote on last edited by
                              #23

                              Deleted Message You really are dumb

                              Deliver yesterday, code today, think tomorrow. "http://www.heuse.com/cphumor.htm"

                              1 Reply Last reply
                              0
                              • T thomasa

                                Sorry, dident see the dgi. That is acculy not a problem, for in each datagriditem every label would have the id="lb2". as in:

                                DataGrid dg = new DataGrid();
                                dg.ID = "dgTheDataGrid";
                                dg.RunatServer = true;
                                DataGridItem dgi = new DataGridItem();
                                Lable lb2 = new Label();
                                lb2.ID = "lb2";
                                dgi.AddControl(lb2);
                                dg.AddControl(dgi);
                                PlaceHolder2.AddControl(dg);

                                The syntax in this code is probably wrong, but something like this.

                                E Offline
                                E Offline
                                eyeseetee
                                wrote on last edited by
                                #24

                                Someone provided me with help on another forum so I have some code which works now: Thanks for all the help though! :)

                                foreach (Control c in PlaceHolder2.Controls)
                                {
                                if (c.GetType().ToString().Equals("System.Web.UI.WebControls.TextBox"))
                                {
                                int textboxId = PlaceHolder2.Controls.IndexOf(c);
                                if (textboxId > 0)
                                {
                                Control cLabel = PlaceHolder2.Controls[textboxId - 1];

                                                                    try
                                                                    {
                                                                        Label lb2 = (Label)cLabel;
                                                                        TextBox tb2 = (TextBox)c;
                                
                                                                        SqlCommand cmdinsert = new SqlCommand("INSERT INTO table(field1, field2) VALUES ('" + lb2.Text + "', '" + tb2.Text + "')", con);
                                            cmdinsert.ExecuteNonQuery(); 
                                                                        cmdinsertliftunion.ExecuteNonQuery();
                                                                    }
                                                                    catch (Exception)
                                                                    { 
                                                                    }
                                                                }
                                                            }
                                

                                Deliver yesterday, code today, think tomorrow. "http://www.heuse.com/cphumor.htm"

                                1 Reply Last reply
                                0
                                Reply
                                • Reply as topic
                                Log in to reply
                                • Oldest to Newest
                                • Newest to Oldest
                                • Most Votes


                                • Login

                                • Don't have an account? Register

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • World
                                • Users
                                • Groups