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. Passing Value to a Web User Control

Passing Value to a Web User Control

Scheduled Pinned Locked Moved ASP.NET
9 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.
  • T Offline
    T Offline
    Taurian110
    wrote on last edited by
    #1

    Is there a way to pass a string value to a Web User Control used by a Class to load the Header to WebForm1.aspx Thanks in advance.

    M 1 Reply Last reply
    0
    • T Taurian110

      Is there a way to pass a string value to a Web User Control used by a Class to load the Header to WebForm1.aspx Thanks in advance.

      M Offline
      M Offline
      Mike Ellison
      wrote on last edited by
      #2

      Sure. You could define a string property in your user control to accept the value, then in the Page_Load of your WebForm1.aspx you can locate the user control and set it's string property accordingly.

      T 1 Reply Last reply
      0
      • M Mike Ellison

        Sure. You could define a string property in your user control to accept the value, then in the Page_Load of your WebForm1.aspx you can locate the user control and set it's string property accordingly.

        T Offline
        T Offline
        Taurian110
        wrote on last edited by
        #3

        Thank you for your reply. I am assuming this is how i need to define a string property:

        	private string \_DepartmentName;
        
        	public string DepartmentName
        	{
        		get{return \_DepartmentName;}
        		set{\_DepartmentName = value;}
        	}
        

        But I am not sure how can I locate the control from Webform1.aspx since I am loading control in the BasePage class (WebForm1.aspx is inheriting from BasePage.cs). This is how I am loading the control in my BasePage.cs:

        	protected void LoadHeader()									
        	{
        		//Control Control;
        		Control Header = LoadControl("~/GlobalControls/Header.ascx");
        		Controls.Add(Header);
        	}
        

        Depending on if user has requested HEADER or not I then call this LoadHeader() to load my control when I am writing other stuff

        		this.Controls.AddAt( 0, new LiteralControl( @"" + Environment.NewLine + 
        		"" + Environment.NewLine +
        		"	" + Title + @"" + Environment.NewLine +
                "" + Environment.NewLine +
        		""
                ));
        		// Check to see if Header was requested.
        		if(AddHeader)
        		{
        				LoadHeader();
        		}
        

        So I guess my question is, how can I call Header.ascx in my WebForm1.aspx and set its property? Once again thanks a lot for helping me with this.

        M 1 Reply Last reply
        0
        • T Taurian110

          Thank you for your reply. I am assuming this is how i need to define a string property:

          	private string \_DepartmentName;
          
          	public string DepartmentName
          	{
          		get{return \_DepartmentName;}
          		set{\_DepartmentName = value;}
          	}
          

          But I am not sure how can I locate the control from Webform1.aspx since I am loading control in the BasePage class (WebForm1.aspx is inheriting from BasePage.cs). This is how I am loading the control in my BasePage.cs:

          	protected void LoadHeader()									
          	{
          		//Control Control;
          		Control Header = LoadControl("~/GlobalControls/Header.ascx");
          		Controls.Add(Header);
          	}
          

          Depending on if user has requested HEADER or not I then call this LoadHeader() to load my control when I am writing other stuff

          		this.Controls.AddAt( 0, new LiteralControl( @"" + Environment.NewLine + 
          		"" + Environment.NewLine +
          		"	" + Title + @"" + Environment.NewLine +
                  "" + Environment.NewLine +
          		""
                  ));
          		// Check to see if Header was requested.
          		if(AddHeader)
          		{
          				LoadHeader();
          		}
          

          So I guess my question is, how can I call Header.ascx in my WebForm1.aspx and set its property? Once again thanks a lot for helping me with this.

          M Offline
          M Offline
          Mike Ellison
          wrote on last edited by
          #4

          How about allowing the Header to be returned through a property of the BasePage? e.g. in your BasePage class, have a private variable called _header that is set through the LoadHeader method. Then have a readonly public property BasePage.Header that would return it.

          T 1 Reply Last reply
          0
          • M Mike Ellison

            How about allowing the Header to be returned through a property of the BasePage? e.g. in your BasePage class, have a private variable called _header that is set through the LoadHeader method. Then have a readonly public property BasePage.Header that would return it.

            T Offline
            T Offline
            Taurian110
            wrote on last edited by
            #5

            Thanks again. As you can tell I dont have a lot of experience in .net:sigh:. From my WebForm1.aspx I can call pass the value of DepartmentName to my BasePage class using the way you suggested. The thing that is still not clear to me is how I would pass this value to my header.ascx control? Like when I load it I use following code:

            protected void LoadHeader()
            {
            Control MyHeader = LoadControl("~/GlobalControls/Header.ascx");
            Controls.Add(lssHeader);
            /* Here how can I pass my BasePage.Header variable's value to my lssHeader
            Something like MyHeader.DepartNameProperty or maybe MyHeader.Add.PropertyName or something.... I just can't seem to figure it out:((.
            }

            Thanks a lot for your help though, I really really appreciate it. Best Regards, Taurian110 -- modified at 23:11 Friday 6th January, 2006

            A 1 Reply Last reply
            0
            • T Taurian110

              Thanks again. As you can tell I dont have a lot of experience in .net:sigh:. From my WebForm1.aspx I can call pass the value of DepartmentName to my BasePage class using the way you suggested. The thing that is still not clear to me is how I would pass this value to my header.ascx control? Like when I load it I use following code:

              protected void LoadHeader()
              {
              Control MyHeader = LoadControl("~/GlobalControls/Header.ascx");
              Controls.Add(lssHeader);
              /* Here how can I pass my BasePage.Header variable's value to my lssHeader
              Something like MyHeader.DepartNameProperty or maybe MyHeader.Add.PropertyName or something.... I just can't seem to figure it out:((.
              }

              Thanks a lot for your help though, I really really appreciate it. Best Regards, Taurian110 -- modified at 23:11 Friday 6th January, 2006

              A Offline
              A Offline
              Agha net
              wrote on last edited by
              #6

              Yes ... I have done the same thing.... but instead of text i was passing string from the Base container or parent page. Since I am VB fan i use to write my code in vb.net just to give you idead. Check the following code : 'Page load of You Base Page Private Sub Page_Load(byval sender as blabla , byval e as blabla) 'Loading Control in a variable called Con Dim Con as Control = LoadControl("~/Controls/MyGrid.ascx") 'I am just converting the type of Control. Coz notice the above line the control which we are loading is actually of "Control" type we are converting this to MyGrid which contain the property Ctype(Con,MyGrid).query = "Select * from tbl_Emp" Page.Control.add(Con) End Sub I wish the above code will help you out ... if you do the same thing in any other way do let me know .... :) Best Regards,

              T 1 Reply Last reply
              0
              • A Agha net

                Yes ... I have done the same thing.... but instead of text i was passing string from the Base container or parent page. Since I am VB fan i use to write my code in vb.net just to give you idead. Check the following code : 'Page load of You Base Page Private Sub Page_Load(byval sender as blabla , byval e as blabla) 'Loading Control in a variable called Con Dim Con as Control = LoadControl("~/Controls/MyGrid.ascx") 'I am just converting the type of Control. Coz notice the above line the control which we are loading is actually of "Control" type we are converting this to MyGrid which contain the property Ctype(Con,MyGrid).query = "Select * from tbl_Emp" Page.Control.add(Con) End Sub I wish the above code will help you out ... if you do the same thing in any other way do let me know .... :) Best Regards,

                T Offline
                T Offline
                Taurian110
                wrote on last edited by
                #7

                I tried to convert the VB.NET code to C# from a utility and I got this: private void Page_Load(blabla sender, blabla e) { Control Con = LoadControl("~/Controls/MyGrid.ascx"); ((MyGrid)(Con)).query = "Select * from tbl_Emp"; Page.Control.add(Con); } but this did not do the trick. :(( Thanks for your help though.

                A 1 Reply Last reply
                0
                • T Taurian110

                  I tried to convert the VB.NET code to C# from a utility and I got this: private void Page_Load(blabla sender, blabla e) { Control Con = LoadControl("~/Controls/MyGrid.ascx"); ((MyGrid)(Con)).query = "Select * from tbl_Emp"; Page.Control.add(Con); } but this did not do the trick. :(( Thanks for your help though.

                  A Offline
                  A Offline
                  Agha net
                  wrote on last edited by
                  #8

                  Well i guess you have simple copy paste the above line dear private void Page_Load(blabla sender, blabla e) this line is paassing FAKE parameters that is why the trick is not working :) Private Sub BindData(ByVal Customer_ID As Long) Try Dim dt As DataTable = ObjProject.GetProjectByCustomerID(Customer_ID) Dim con As Control = LoadControl("/Controls/ProjectViewCon.ascx") CType(con, Controls.ProjectViewCon).DataTable = dt PlaceHolder1.Controls.Add(con) Catch ex As CustomExceptions.Exception_NoRecordFound Lbl_Error.Text = "You have not posted any project as yet [click here](/dynamic/PostProject.aspx/) to post project." Catch ex As Exception Throw ex End Try End Sub This is a function call this function in Your Load method. it will work and please do some necesaary alteration.

                  T 1 Reply Last reply
                  0
                  • A Agha net

                    Well i guess you have simple copy paste the above line dear private void Page_Load(blabla sender, blabla e) this line is paassing FAKE parameters that is why the trick is not working :) Private Sub BindData(ByVal Customer_ID As Long) Try Dim dt As DataTable = ObjProject.GetProjectByCustomerID(Customer_ID) Dim con As Control = LoadControl("/Controls/ProjectViewCon.ascx") CType(con, Controls.ProjectViewCon).DataTable = dt PlaceHolder1.Controls.Add(con) Catch ex As CustomExceptions.Exception_NoRecordFound Lbl_Error.Text = "You have not posted any project as yet [click here](/dynamic/PostProject.aspx/) to post project." Catch ex As Exception Throw ex End Try End Sub This is a function call this function in Your Load method. it will work and please do some necesaary alteration.

                    T Offline
                    T Offline
                    Taurian110
                    wrote on last edited by
                    #9

                    Sorry for the misconception. I did make necessary changes... I was just posting that converted text like i got from that website to show what I had in C#. Anyway I will try it again in the morning and let you know. THanks for your help though:).

                    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