동적으로로드하는 사용자 정의 컨트롤에 기본 TextBox가 있습니다. 이 TextBox에 값을 전달하는 방법을 알아 냈습니다. 내가 겪고있는 문제는이 TextBox에서 입력 된 값을 가져올 수 없다는 것입니다.동적으로로드 된 사용자 정의 컨트롤에서 게시물 값에 액세스하는 방법
동적으로로드 된 컨트롤에 변수를 전달하는 클래스를 만들어야했습니다. PBUserControl이라고했습니다.
public class PBUserControl : UserControl
{
public IList<NVP> NameValuePairs { get { return _NameValuePairs; } }
public class NVP
{
public NVP() { }
public NVP(string name, string value)
{
this.Name = name;
this.Value = value;
}
public string Name { get; set; }
public string Value { get; set; }
}
}
이것은 사용자 정의 컨트롤로 데이터를 전송하는 데는 문제가없는 것 같습니다.
내 동적으로로드 된 사용자 컨트롤 :
public partial class PageAddEdit : PageBase
{
private PBUserControl _PBUserControl;
protected void Page_Load(object sender, EventArgs e)
{
// Must always desigante the control here else it dissapears.
Control TemplateUserControl = LoadControl("~/Administrator/PageBuilder/Templates/Basic.ascx");
_PBUserControl = (PBUserControl)TemplateUserControl;
_PBUserControl.AddNameValuePair("TestContent", "It is working!!!!!!!!!");
_PBUserControl.AddNameValuePair("Test", "Some test text.");
Template.Controls.Add(TemplateUserControl);
}
protected void MenuGroupRadMenu_Clicked(object sender, EventArgs e)
{
IList<PBUserControl.NVP> nvp = _PBUserControl.NameValuePairs;
}
}
나 여기 무슨 일이 생겼는지 설명 : 여기에
public partial class Basic : PBUserControl
{
protected void Page_Load(object sender, EventArgs e)
{
if (this.IsPostBack == false)
{
this.InitializeControl();
}
else
{
this.ProcessSubmit();
}
}
private void InitializeControl()
{
DynamicLiteral.Text = "Some Dynamic Content. " + GetNameValue("TestContent");
TestTextBox.Text = GetNameValue("Test");
}
private void ProcessSubmit()
{
AddNameValuePair("TestContent", "Passing back some value... "+ DynamicLiteral.Text);
AddNameValuePair("Test", TestTextBox.Text);
}
}
는이 동적으로로드 제어를 포함하는 페이지의 호출이다. 메인 페이지에서 PBUserControl의 값을 설정할 수 있습니다. 동적으로로드 된 사용자 정의 컨트롤로 완벽하게 전달됩니다. 그것이 무엇을 해야하는지 않습니다. 멋지게 작동합니다.
그러나 포스트 백 변수가 처리되기 전에 동적으로로드 된 컨트롤 내의 page_load 이벤트가 시작됩니다. 그래서 그 시점에서 TextBox의 내용은 아직 읽혀지지 않습니다. 나는 연구를 통해 이것을 발견했다. 또한 동적으로로드 된 컨트롤 내에 Pre_Rendered 이벤트를 넣으면 디버거로 코드를 단계별로 실행할 때 TextBox 값을 볼 수 있습니다. 또한, 디버거를 사용하면 TextBox가 Page_Load 이벤트 중에 읽히지 않는다는 것을 알 수 있습니다. 내 연구 결과는 다음과 같습니다. 1. Page_Init 이벤트는 입니다. Page_Load 이벤트는 입니다. 3. 페이지 양식 변수가 읽히고 처리됩니다 (어떤 이벤트인지 모르겠 음). 4. 사용자 이벤트는 다음과 같습니다. OnClick 5 Pre_Rendered 이벤트가 호출됩니다.
3 단계 이후와 4 단계 전 또는 도중에 호출 할 이벤트를 만들 수 있어야합니다. 그러면 TextBox 값을 캡처하여 PBUserControl.NVP 변수 내에 값을 설정하여 읽을 수 있도록 할 수 있습니다 메인 페이지. PBUserControl.NVP 변수에 수동으로 값을 설정하면 주 페이지에서 볼 수 있으며 잘 읽습니다.
미리 감사드립니다.
꽤 맵시를! 잘 했어 – Andrew