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. Dynamic User control load get a parameter

Dynamic User control load get a parameter

Scheduled Pinned Locked Moved ASP.NET
csharpwinformsdesignsysadminsecurity
17 Posts 4 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.
  • C Christian Graus

    Your core issue as far as I can see, is that you are setting your property in page load, which is too early. Trying to do it in the prerender event may solve your issue, but I still don't get why a user control is needed to change a setting on the aspx

    Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

    S Offline
    S Offline
    simsen
    wrote on last edited by
    #8

    I'm sorry I don't explain myself so good (my english is not SO good)... For me it is simple - I have a usercontrol..... Normally i would do this....... <crm:SetProject runat="server" id="SetProject" ProjectId="500" /> on an aspx page to load the usercontrol with the parameter ProjectId = 500 - I then in the user control can use the 500 to do something with this..... My problem is - when I do it this way - the parameter will always be 500 - And I don't know what the parameter is at page load time....... I first know what the parameter is when I hit a button on the page........ So when I hit the button - I need to make an instans of the usercontrol with the parameter ProjectId="9999"

    C 1 Reply Last reply
    0
    • S simsen

      I'm sorry I don't explain myself so good (my english is not SO good)... For me it is simple - I have a usercontrol..... Normally i would do this....... <crm:SetProject runat="server" id="SetProject" ProjectId="500" /> on an aspx page to load the usercontrol with the parameter ProjectId = 500 - I then in the user control can use the 500 to do something with this..... My problem is - when I do it this way - the parameter will always be 500 - And I don't know what the parameter is at page load time....... I first know what the parameter is when I hit a button on the page........ So when I hit the button - I need to make an instans of the usercontrol with the parameter ProjectId="9999"

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #9

      simsen wrote:

      So when I hit the button - I need to make an instans of the usercontrol with the parameter ProjectId="9999"

      So, you set it in code when you create the control. ProjectId must be a public property, or it would not be available from the aspx. Why can't you just set it in code ?

      Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

      S 1 Reply Last reply
      0
      • C Christian Graus

        simsen wrote:

        So when I hit the button - I need to make an instans of the usercontrol with the parameter ProjectId="9999"

        So, you set it in code when you create the control. ProjectId must be a public property, or it would not be available from the aspx. Why can't you just set it in code ?

        Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

        S Offline
        S Offline
        simsen
        wrote on last edited by
        #10

        Now we are getting to my problem........ I don't know how to set it when I create the control..... I tried many things but each time it sais - it dosn't know the SetProject.ProjectId.... I use these to lines to create the control from codebehind....

        Control setproject = (Control)LoadControl("~/UserControls/TestSetProject.ascx");
        phNewProjectEdit.Controls.Add(setproject);

        But when I then try to make a setproject.ProjectId the system cannot find the ProjectId - the error sais something like this: System.Web.UI.Control doesn't contain a definition of 'ProjectId'

        C 1 Reply Last reply
        0
        • S simsen

          Now we are getting to my problem........ I don't know how to set it when I create the control..... I tried many things but each time it sais - it dosn't know the SetProject.ProjectId.... I use these to lines to create the control from codebehind....

          Control setproject = (Control)LoadControl("~/UserControls/TestSetProject.ascx");
          phNewProjectEdit.Controls.Add(setproject);

          But when I then try to make a setproject.ProjectId the system cannot find the ProjectId - the error sais something like this: System.Web.UI.Control doesn't contain a definition of 'ProjectId'

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #11

          simsen wrote:

          Control setproject = (Control)LoadControl("~/UserControls/TestSetProject.ascx"); phNewProjectEdit.Controls.Add(setproject);

          There is your problem. The Control class does not contain this property. You need to cast to the type of your specific control, to be able to access the properties that exist in your control and not the base class.

          Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

          S 1 Reply Last reply
          0
          • C Christian Graus

            simsen wrote:

            Control setproject = (Control)LoadControl("~/UserControls/TestSetProject.ascx"); phNewProjectEdit.Controls.Add(setproject);

            There is your problem. The Control class does not contain this property. You need to cast to the type of your specific control, to be able to access the properties that exist in your control and not the base class.

            Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

            S Offline
            S Offline
            simsen
            wrote on last edited by
            #12

            I don't understand that you say that the control doesn't contain this property..... If you see the ascx.cs you can se I have made a public property?

            using System;
            using System.Data;
            using System.Configuration;
            using System.Collections;
            using System.Web;
            using System.Web.Security;
            using System.Web.UI;
            using System.Web.UI.WebControls;
            using System.Web.UI.WebControls.WebParts;
            using System.Web.UI.HtmlControls;

            public partial class UserControls_TestSetProject : System.Web.UI.UserControl
            {
            private string _ProjectId = "";

            public string ProjectId
            {
                get { return \_ProjectId; }
                set { \_ProjectId = value; }
            }
            
            protected void Page\_Load(object sender, EventArgs e)
            {
                lblEditProjectNameHeadline.Text = ProjectId;
            }
            

            }

            C 1 Reply Last reply
            0
            • S simsen

              I don't understand that you say that the control doesn't contain this property..... If you see the ascx.cs you can se I have made a public property?

              using System;
              using System.Data;
              using System.Configuration;
              using System.Collections;
              using System.Web;
              using System.Web.Security;
              using System.Web.UI;
              using System.Web.UI.WebControls;
              using System.Web.UI.WebControls.WebParts;
              using System.Web.UI.HtmlControls;

              public partial class UserControls_TestSetProject : System.Web.UI.UserControl
              {
              private string _ProjectId = "";

              public string ProjectId
              {
                  get { return \_ProjectId; }
                  set { \_ProjectId = value; }
              }
              
              protected void Page\_Load(object sender, EventArgs e)
              {
                  lblEditProjectNameHeadline.Text = ProjectId;
              }
              

              }

              C Offline
              C Offline
              Christian Graus
              wrote on last edited by
              #13

              You should buy a book on OO and read it then. You are creating an instance of your control, but your local reference, is a reference to a base class. It therefore does not know that the class instance is a specific derived class, it could be any class derived from Control. Until you cast it up to the class thhat contains that property, it will not be visible. If you do this: ((UserControls_TestSetProject )myControl).ProjectId = "Blah"; it will work, assuming your variable is called myControl. For the reasons I have explained.

              Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

              S 1 Reply Last reply
              0
              • C Christian Graus

                You should buy a book on OO and read it then. You are creating an instance of your control, but your local reference, is a reference to a base class. It therefore does not know that the class instance is a specific derived class, it could be any class derived from Control. Until you cast it up to the class thhat contains that property, it will not be visible. If you do this: ((UserControls_TestSetProject )myControl).ProjectId = "Blah"; it will work, assuming your variable is called myControl. For the reasons I have explained.

                Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

                S Offline
                S Offline
                simsen
                wrote on last edited by
                #14

                Thank you very very much...... It was what I needed...... Now I only have to figure out, how I get it without to have to push the button twice (I know it's becauce of the page_load comes before the btnPush_Click and I have to make an instans of the user control each time I load the page) I wil use some times of this problem before I get back to this forum for help.... But again thank you very much for your help. It helped me very much :-)

                C 1 Reply Last reply
                0
                • S simsen

                  I have a problem how I get the user controls parameter from codebehind (when I make a dynamic instans of this) on the page. My user control ascx file:

                  <%@ Control Language="C#" AutoEventWireup="true" CodeFile="TestSetProject.ascx.cs" Inherits="UserControls_TestSetProject" %>

                  <center><h3><asp:Label ID="lblEditProjectHeadline" runat="server" Text="Projektdetaljer for"></asp:Label>
                  <asp:Label ID="lblEditProjectNameHeadline" runat="server" Text=""></asp:Label></h3></center><br />

                  My user control ascx.cs file:

                  using System;
                  using System.Data;
                  using System.Configuration;
                  using System.Collections;
                  using System.Web;
                  using System.Web.Security;
                  using System.Web.UI;
                  using System.Web.UI.WebControls;
                  using System.Web.UI.WebControls.WebParts;
                  using System.Web.UI.HtmlControls;

                  public partial class UserControls_TestSetProject : System.Web.UI.UserControl
                  {
                  private string _ProjectId = "";
                  public string ProjectId
                  {
                  get { return _ProjectId; }
                  set { _ProjectId = value; }
                  }

                  protected void Page\_Load(object sender, EventArgs e)
                  {
                      lblEditProjectNameHeadline.Text = ProjectId;
                  }
                  

                  }

                  My test aspx page:

                  <%@ Page Language="C#" MasterPageFile="~/MPHead.master" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="Content_Test" Title="Untitled Page" %>
                  <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
                  <asp:Label ID="lblNewProjectEditProjectId" runat="server" Text="" Visible="false"></asp:Label><br /><br />
                  <asp:Button ID="btnPush" runat="server" Text="Push me" OnClick="btnPush_Click" />
                  <asp:PlaceHolder ID="phNewProjectEdit" runat="server"></asp:PlaceHolder>
                  </asp:Content>

                  My test aspx.cs file:

                  using System;
                  using System.Data;
                  using System.Configuration;
                  using System.Collections;
                  using System.Web;
                  using System.Web.Security;
                  using System.Web.UI;
                  using System.Web.UI.WebControls;
                  using System.Web.UI.WebControls.WebParts;
                  using System.Web.UI.HtmlControls;

                  public partial class Content_Test : System.Web.UI.Page
                  {
                  protected void Page_Load(object sender, EventArgs e)
                  {
                  Control setproject = LoadControl("~/UserControls/TestSetProject.ascx");
                  phNewProjectEdit.Controls.Add(setproject);

                  }
                  protected void btnPush\_Click(object sender, EventArgs e)
                  {
                      lblNewProjectEditProjectId.Text = "15855";
                  }
                  
                  B Offline
                  B Offline
                  Broken Bokken
                  wrote on last edited by
                  #15

                  You aren't setting the ProjectId property on the control when you create it. Try moving everything to the button's click event, like this: protected void btnPush_Click(object sender, EventArgs e) { UserControls.TestSetProject setproject = (UserControls.TestSetProject)LoadControl("~/UserControls/TestSetProject.ascx"); setproject.ProjectId = "15855" phNewProjectEdit.Controls.Add(setproject); }

                  Broken Bokken http://www.brokenbokken.com

                  S 1 Reply Last reply
                  0
                  • B Broken Bokken

                    You aren't setting the ProjectId property on the control when you create it. Try moving everything to the button's click event, like this: protected void btnPush_Click(object sender, EventArgs e) { UserControls.TestSetProject setproject = (UserControls.TestSetProject)LoadControl("~/UserControls/TestSetProject.ascx"); setproject.ProjectId = "15855" phNewProjectEdit.Controls.Add(setproject); }

                    Broken Bokken http://www.brokenbokken.com

                    S Offline
                    S Offline
                    simsen
                    wrote on last edited by
                    #16

                    Hi Josh When I try this, an then click on a button that is in the user control the control disappear... How can I get the user control to appear again (I have to get the user control from it self)

                    1 Reply Last reply
                    0
                    • S simsen

                      Thank you very very much...... It was what I needed...... Now I only have to figure out, how I get it without to have to push the button twice (I know it's becauce of the page_load comes before the btnPush_Click and I have to make an instans of the user control each time I load the page) I wil use some times of this problem before I get back to this forum for help.... But again thank you very much for your help. It helped me very much :-)

                      C Offline
                      C Offline
                      Christian Graus
                      wrote on last edited by
                      #17

                      simsen wrote:

                      I know it's becauce of the page_load comes before the btnPush_Click and I have to make an instans of the user control each time I load the page)

                      Like I said, call it in the prerender event instead, it comes after the button clicks

                      Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

                      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