locked
Dynamic User control load get a parameter RRS feed

  • Question

  • User-1798522060 posted
    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" %>

    <%@ Reference Control="~/UserControls/TestSetProject.ascx" %>

    <%@ Register TagPrefix="crm" TagName="SetProject" Src="~/UserControls/TestSetProject.ascx" %>

    <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>

    <crm:SetProject runat="server" id="SetProject" ProjectId="500" />

    </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 = (Control)LoadControl("~/UserControls/TestSetProject.ascx");
            phNewProjectEdit.Controls.Add(setproject);
            UserControl
    
        }
        protected void btnPush_Click(object sender, EventArgs e)
        {
            lblNewProjectEditProjectId.Text = "15855";
        }
    }
     

    What I need, is that the user control will be instansiated (created) when I click on the btnpush button on the page og then takes what I have wrote in the lblNewProjectEditProjectId.Text into the user control (where I then can use it).

    If I do it "the old way" I could do this:
    <crm:SetProject runat="server" id="SetProject" ProjectId="500" />

    and the projectId="500" is passed to the user control........But my problem here is I don't know what the projectId before I hit the button..... 
    Can anybody please help me on this?

    Kind regards,
    simsen

     

    Tuesday, April 15, 2008 7:21 AM

Answers

  • User371668647 posted

    Try:

    TestSetProject setproject = (TestSetProject)LoadControl("~/UserControls/TestSetProject.ascx");
    setProject.ProjectId = 500;
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 15, 2008 2:05 PM