2012-11-13 2 views
1

동적으로 페이지에 사용자 정의 컨트롤을로드하고 있습니다. 나는 보통 vb.net 사람이고 일반적으로 얻을 수 있지만 이것은 저를 곤란하게합니다. 그것을로드하기 전에 페이지에서 컨트롤로 변수를 전달하려고합니다. 내가 회전 목마 - guards.ascx에 다음과 같은 속성을 넣었습니다페이지에서 제어 할 변수 전달하기

Control webUserControl = (Control)Page.LoadControl("~/controls/carousel-guards.ascx"); 

    phGuardsList.Controls.Add(webUserControl); 

:

public String PostCode 
     { 
      get 
      { 
       return this.PostCode; 
      } 
      set 
      { 
       this.PostCode = value; 
      } 
     } 

하지만이되어 있지 않은 것 같습니다

Heres는 내가 컨트롤을 호출하고있어 어디 나에게 webUserControl.PostCode를 제공하십시오.

어떤 도움이 정말

편집을 이해할 수있을 것이다 - 물론, 내가 컨트롤을 참조해야합니다. 바보 나! 그러나 나를 carousel_guards로 전화를 두지 않을 것 : 당신은 작업 페이지의 클래스 이름을 사용할 필요가 Error 96 The type or namespace name 'carousel_guards' could not be found (are you missing a using directive or an assembly reference?) C:\Development\Guards247\g247 Test\FindGuard.aspx.cs 197 13 g247 Test

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 

namespace g247_Test.controls 
{ 
    public partial class carousel_guards : System.Web.UI.UserControl 
    { 

     protected void Page_Load(object sender, EventArgs e) 
     { 

     } 

     public String PostCode 
     { 
      get 
      { 
       return this.PostCode; 
      } 
      set 
      { 
       this.PostCode = value; 
      } 
     } 
    } 
} 
+0

속성을 작성한 방법에서 문제가 발생하면 자체 참조됩니다. 이것을 피하기 위해 보통 접두사가 '_'인 내부 멤버를 씁니다. – AMember

답변

0

로드 된 컨트롤을 Control으로 전송 중이므로 컨트롤에서 속성에 액세스 할 수 없습니다. 그것을 당신의 컨트롤 타입으로 캐스팅해야합니다. Control 클래스 이름이 CarouselGuards 인 경우 다음을 수행 할 수 있습니다.

CarouselGuards webUserControl = (CarouselGuards)Page.LoadControl("~/controls/carousel-guards.ascx"); 

이 속성에 액세스 할 수 있습니다.

webUserControl.PostCode = "123"; 
+0

고마워요 - 고마워요. 내 질문에 대한 답을 빨리 바꿨다면 – TMB87

+0

@TomBeech, 코드 맨 위에'g247_Test.controls;를 사용하거나'carousel_guards를 오른쪽 클릭해야합니다. '를 선택하고 '해결'을 선택하십시오 – Habib

+0

@TomBeech, 환영합니다 – Habib

1

.

var webUserControl = (carousel_guards)Page.LoadControl("~/controls/carousel-guards.ascx"); 

// now works 
webUserControl.PostCode = "17673"; 
phGuardsList.Controls.Add(webUserControl); 

당신이없는 경우 는, 클래스의 이름이 영문에 선

<%@ Reference Control="~/controls/carousel-guards.ascx" %> 

또는 드래그를 삽입하고 페이지 내에서 드롭 할 수 발견 컨트롤의 참조하지 포함 참조를 만들고 실제 컨트롤을 삭제하려면 동적 컨트롤을 사용하십시오.

0

를 사용하여 객체에 대한 확인 널 제어를 잊지 마세요 보조 노트로 Control

CarouselGuards webUserControl = (CarouselGuards)Page.LoadControl("~/controls/carousel-guards.ascx"); 
webUserControl.PostCode = "XXXX"; 

대신 제어 방식 CarouselGuards 캐스팅.