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 usercontrol

problem with usercontrol

Scheduled Pinned Locked Moved ASP.NET
helpswiftcomtutorial
4 Posts 2 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.
  • D Offline
    D Offline
    Deepak Nigam
    wrote on last edited by
    #1

    hello friends, In my application i am having a usercontrol py_user, now i want to create its multiple instance at runtime, like if i enter 5, 5 py_use control should be added on the page. Please help me how to do this.

    Deepak Nigam deepak@swiftcybernetics.com Swift Cybernetics

    G 1 Reply Last reply
    0
    • D Deepak Nigam

      hello friends, In my application i am having a usercontrol py_user, now i want to create its multiple instance at runtime, like if i enter 5, 5 py_use control should be added on the page. Please help me how to do this.

      Deepak Nigam deepak@swiftcybernetics.com Swift Cybernetics

      G Offline
      G Offline
      Greg Chelstowski
      wrote on last edited by
      #2

      What I think you're asking for is creating your py_user control dynamically, when certain conditions are met, and also, a specified number of times, which would be user-input defined. I'm only guessing you'll be putting that number in a textbox somewhere on your page. Either way, how you should be doing it, is:

      private void CreatePyUser()
      {
      try
      {
      int pyNum = Convert.ToInt32(textBox1.Text); //assuming you put a number in this textbox,otherwise the try-catch statement goes straight to catch

      for(int py_uNum=0; py_uNum<=pyNum; pu_uNum++)
      {
      py_user py_user1 = new py_user();
      py_user1.ID = "py_user"+py_uNum.ToString(); // assuming you want to access this control later
      this.Controls.Add(py_user1);

      }

      }
      catch
      {
      Response.Write("Please put in a number in the given textbox.");
      }

      }

      If that's not what you wanted, please try explaining what you need in a bit more detail. And perhaps pass some of your own code, sometimes?

      var question = (_2b || !(_2b));

      D 1 Reply Last reply
      0
      • G Greg Chelstowski

        What I think you're asking for is creating your py_user control dynamically, when certain conditions are met, and also, a specified number of times, which would be user-input defined. I'm only guessing you'll be putting that number in a textbox somewhere on your page. Either way, how you should be doing it, is:

        private void CreatePyUser()
        {
        try
        {
        int pyNum = Convert.ToInt32(textBox1.Text); //assuming you put a number in this textbox,otherwise the try-catch statement goes straight to catch

        for(int py_uNum=0; py_uNum<=pyNum; pu_uNum++)
        {
        py_user py_user1 = new py_user();
        py_user1.ID = "py_user"+py_uNum.ToString(); // assuming you want to access this control later
        this.Controls.Add(py_user1);

        }

        }
        catch
        {
        Response.Write("Please put in a number in the given textbox.");
        }

        }

        If that's not what you wanted, please try explaining what you need in a bit more detail. And perhaps pass some of your own code, sometimes?

        var question = (_2b || !(_2b));

        D Offline
        D Offline
        Deepak Nigam
        wrote on last edited by
        #3

        Thanks for your quick reply but i am not able to make an object of the usercontrol ie py_user py_user1 = new py_user(); also this part i have done using Control t = LoadControl("../UserControl/py_user.ascx"); t.ID = "py" + i.ToString(); pPanel1.Controls.Add(t); In my user control there is a textbox that takes an integer value and generates a table with the specified no of rows, Now what happens suppose i have created 3 instance of my usercontrol, again in the first usercontrol i want 3 rows so i put 3 and generate 3 rows till this it is working fine, but when i move to second instance of usercontrol and try to generate 2 rows it generate 2 rows but it clear the rows of first control. i want rows in all the usercontrols, please help me..............:)

        Deepak Nigam deepak@swiftcybernetics.com Swift Cybernetics

        G 1 Reply Last reply
        0
        • D Deepak Nigam

          Thanks for your quick reply but i am not able to make an object of the usercontrol ie py_user py_user1 = new py_user(); also this part i have done using Control t = LoadControl("../UserControl/py_user.ascx"); t.ID = "py" + i.ToString(); pPanel1.Controls.Add(t); In my user control there is a textbox that takes an integer value and generates a table with the specified no of rows, Now what happens suppose i have created 3 instance of my usercontrol, again in the first usercontrol i want 3 rows so i put 3 and generate 3 rows till this it is working fine, but when i move to second instance of usercontrol and try to generate 2 rows it generate 2 rows but it clear the rows of first control. i want rows in all the usercontrols, please help me..............:)

          Deepak Nigam deepak@swiftcybernetics.com Swift Cybernetics

          G Offline
          G Offline
          Greg Chelstowski
          wrote on last edited by
          #4

          I think I know what you mean... As I understand it, you have something like this, in the code-behind of your usercontrol:

          private void CreateTable(int x)
          {
          HtmlTable table = new HtmlTable();
          for (int y = 0; y <= x; y++)
          {
          HtmlTableRow tableRow = new HtmlTableRow();
          table.Controls.Add(tableRow);
          }
          this.Controls.Add(table);

          }

          And this method gets called through some button click on the user control. This is only going to govern this instance of your py_user. It will do a postback and render a table FOR THIS instance. But the remaining 4, or however many py_user controls on that page will not have had that buttonclick triggered for them and therefore they would get rendered as PURE py_user controls (no tables, no rows), if at all. I don't like your design one bit. Sorry there is no easy way out, without adding those controls to the Session, then checking if the session is not null. Honestly, what are you trying to achieve through this?

          var question = (_2b || !(_2b));

          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