Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 获取要在UI中显示的动态添加的子控件_C#_Asp.net_Webforms - Fatal编程技术网

C# 获取要在UI中显示的动态添加的子控件

C# 获取要在UI中显示的动态添加的子控件,c#,asp.net,webforms,C#,Asp.net,Webforms,我试图创建一个RadioButtonListWithOther类来扩展RadoButtonList,但是我无法在页面上呈现“Other”文本框。当我在调试时单步执行时,我可以在父控件的控件集合中看到该控件,但它仍然不会呈现。你知道我做错了什么吗 public class RadioButtonListWithOther : RadioButtonList { private TextBox _otherReason; public RadioButtonListWithOthe

我试图创建一个RadioButtonListWithOther类来扩展RadoButtonList,但是我无法在页面上呈现“Other”文本框。当我在调试时单步执行时,我可以在父控件的控件集合中看到该控件,但它仍然不会呈现。你知道我做错了什么吗

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;
    }
}
以下是将此控件的实例添加到WebForm的代码:

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();
}
protectedoverride void CreateChildControls()受保护的覆盖
{
var whyMentorOptions=新字典();
添加(“选项1”、“1”);
添加(“选项2”、“2”);
添加(“选项3”、“3”);
添加(“其他”、“其他”);
导师为什么=新RadioButtonListWithOther
{
数据源=whymentorptions
};
this.mentorWhy.DataTextField=“Key”;
this.mentorWhy.DataValueField=“Value”;
this.modentwhy.DataBind();
Form.Controls.Add(为什么);
base.CreateChildControls();
}
该类在呈现时完全忽略其子控件(它只对其集合的内容感兴趣)

您必须自己呈现文本框:

protected override void Render(HtmlTextWriter writer)
{
    base.Render(writer);
    _otherReason.RenderControl(writer);
}

这会显示文本框,但当我选中它的_otherReason.Text属性时,即使网页中的框中有文本,它也始终为空。我假设我也需要手动处理子控件的控件状态。有什么好办法吗?@jjr2527,您应该能够使用
Request[\u otherReason.UniqueID]
从HTTP请求中获取文本框值。