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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Web Development
  3. ASP.NET
  4. Adding data using a user control.

Adding data using a user control.

Scheduled Pinned Locked Moved ASP.NET
help
6 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.
  • U Offline
    U Offline
    udaykatakam
    wrote on last edited by
    #1

    There are about 10 pages in my project which are used to populate reference tables and each page needs to have text boxes to input data and format (i.e. number of text boxes, format of data etc) So I want to make a user control for data entry and place in all pages so that I dont have repeat the same code. But I not very comfortable with events and delegates and also passing data to and from a user control and webpage. Can someone suggest me code or some links which help me in doing this. Thanks Uday

    Uday

    T 1 Reply Last reply
    0
    • U udaykatakam

      There are about 10 pages in my project which are used to populate reference tables and each page needs to have text boxes to input data and format (i.e. number of text boxes, format of data etc) So I want to make a user control for data entry and place in all pages so that I dont have repeat the same code. But I not very comfortable with events and delegates and also passing data to and from a user control and webpage. Can someone suggest me code or some links which help me in doing this. Thanks Uday

      Uday

      T Offline
      T Offline
      ToddHileHoffer
      wrote on last edited by
      #2

      Any book on ASP.Net 2.0 should have examples of this. I would also suggest that you are making things more difficult than need be. What is wrong with having 10 pages to update 10 tables? That seems like good design to me. Putting code to update 10 tables in one user control seems like a terrible idea. Why take something simple and make it complex like that? Think about this. If you don't touch this application for a couple years and you have to modify one of these reference tables. If you have one page per table, think how easy it will be to maintain! If you have some fancy trickery to put all the code in one user control think about how much more difficult it will be to dive back into the code. Just my two cents...

      I didn't get any requirements for the signature

      U 1 Reply Last reply
      0
      • T ToddHileHoffer

        Any book on ASP.Net 2.0 should have examples of this. I would also suggest that you are making things more difficult than need be. What is wrong with having 10 pages to update 10 tables? That seems like good design to me. Putting code to update 10 tables in one user control seems like a terrible idea. Why take something simple and make it complex like that? Think about this. If you don't touch this application for a couple years and you have to modify one of these reference tables. If you have one page per table, think how easy it will be to maintain! If you have some fancy trickery to put all the code in one user control think about how much more difficult it will be to dive back into the code. Just my two cents...

        I didn't get any requirements for the signature

        U Offline
        U Offline
        udaykatakam
        wrote on last edited by
        #3

        No no I am not trying to put whole code to update 10 tables in same user control's code. Since the html part is same in all the pages I thought of having it in a user control and have a save button on it. When user clicks save then I thought of handling that event on the web page to save the data. Not sure if this is the right way.I am new to .Net It is really simple when I have the html part on all pages but I want have it in one place and reuse it.

        Uday

        T 1 Reply Last reply
        0
        • U udaykatakam

          No no I am not trying to put whole code to update 10 tables in same user control's code. Since the html part is same in all the pages I thought of having it in a user control and have a save button on it. When user clicks save then I thought of handling that event on the web page to save the data. Not sure if this is the right way.I am new to .Net It is really simple when I have the html part on all pages but I want have it in one place and reuse it.

          Uday

          T Offline
          T Offline
          ToddHileHoffer
          wrote on last edited by
          #4

          Sorry I misunderstood. Here are some tips for you. Create a public property for any control in your user control that you want to use in the page. And vice versa. Most books will give you an example of using userControl.FindControl("id"). But I find it cleaner and easier if just create a public property. As for events... In the page_init of the page containing the control is where you set the event. Here is a quick example... Good luck. //user control public partial class WebUserControl : System.Web.UI.UserControl { public Button submitButton { get { return this.Button1; } set { this.Button1 = value; } } protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { } } //page public partial class _Default : System.Web.UI.Page { protected void Page_Init(object sender, EventArgs e) { this.WebUserControl1.submitButton.Click += new EventHandler(submitButton_Click); } protected void Page_Load(object sender, EventArgs e) { } protected void submitButton_Click(object sender, EventArgs e) { Response.Write("event handled"); } }

          I didn't get any requirements for the signature

          U 1 Reply Last reply
          0
          • T ToddHileHoffer

            Sorry I misunderstood. Here are some tips for you. Create a public property for any control in your user control that you want to use in the page. And vice versa. Most books will give you an example of using userControl.FindControl("id"). But I find it cleaner and easier if just create a public property. As for events... In the page_init of the page containing the control is where you set the event. Here is a quick example... Good luck. //user control public partial class WebUserControl : System.Web.UI.UserControl { public Button submitButton { get { return this.Button1; } set { this.Button1 = value; } } protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { } } //page public partial class _Default : System.Web.UI.Page { protected void Page_Init(object sender, EventArgs e) { this.WebUserControl1.submitButton.Click += new EventHandler(submitButton_Click); } protected void Page_Load(object sender, EventArgs e) { } protected void submitButton_Click(object sender, EventArgs e) { Response.Write("event handled"); } }

            I didn't get any requirements for the signature

            U Offline
            U Offline
            udaykatakam
            wrote on last edited by
            #5

            Todd thanks for your response I could access my the controls on a user control this way. Can we also access if the user control is on a master page?

            Uday

            T 1 Reply Last reply
            0
            • U udaykatakam

              Todd thanks for your response I could access my the controls on a user control this way. Can we also access if the user control is on a master page?

              Uday

              T Offline
              T Offline
              ToddHileHoffer
              wrote on last edited by
              #6

              Yes.

              I didn't get any requirements for the signature

              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