2
我正在嘗試創建一個擴展RadoButtonList的RadioButtonListWithOther類,但我無法獲取要在頁面上呈現的「其他」文本框。當我在調試的時候經過,我可以看到父控件的控件集合中的控件,但它仍然不呈現。任何想法我在這裏做錯了嗎?獲取動態添加的子控件以在UI中顯示
public class RadioButtonListWithOther : RadioButtonList
{
private TextBox _otherReason;
public RadioButtonListWithOther()
{
_otherReason = new TextBox();
_otherReason.TextMode = TextBoxMode.MultiLine;
_otherReason.Rows = 6;
_otherReason.Width = Unit.Pixel(300);
_otherReason.Visible = true;
}
protected override void CreateChildControls()
{
this.Controls.Add(_otherReason);
this.EnsureChildControls();
base.CreateChildControls();
}
protected override void OnSelectedIndexChanged(EventArgs e)
{
_otherReason.Enabled = false;
if (OtherSelected())
{
_otherReason.Enabled = true;
}
base.OnSelectedIndexChanged(e);
}
public override string Text
{
get
{
if (OtherSelected())
{
return _otherReason.Text;
}
return base.Text;
}
set
{
base.Text = value;
}
}
public override bool Visible
{
get
{
return base.Visible;
}
set
{
//Push visibility changes down to the children controls
foreach (Control control in this.Controls)
{
control.Visible = value;
}
base.Visible = value;
}
}
private bool OtherSelected()
{
if (this.SelectedItem.Text == "Other")
{
return true;
}
return false;
}
}
這裏是我的代碼,該控件的一個實例添加到Web窗體:
protected override void CreateChildControls()
{
var whyMentorOptions = new Dictionary<string, string>();
whyMentorOptions.Add("Option 1", "1");
whyMentorOptions.Add("Option 2", "2");
whyMentorOptions.Add("Option 3", "3");
whyMentorOptions.Add("Other", "Other");
mentorWhy = new RadioButtonListWithOther
{
DataSource = whyMentorOptions
};
this.mentorWhy.DataTextField = "Key";
this.mentorWhy.DataValueField = "Value";
this.mentorWhy.DataBind();
Form.Controls.Add(mentorWhy);
base.CreateChildControls();
}
這讓文本框中顯示,但是當我檢查它的它總是空的,即使有文本框中的網頁_otherReason.Text財產頁。那麼我假設我需要手動處理子控件的控件狀態。有沒有一個好的方法來做到這一點? – jjr2527 2010-12-20 12:55:18
@ jjr2527,您應該可以使用Request [_otherReason.UniqueID]從HTTP請求中獲取文本框的值。 – 2010-12-20 13:02:15