使用findcontrol在literalcontrol中查找控件

时间:2010-05-04 14:28:33

标签: c# .net findcontrol

我有一个自定义控件,可以在createchildcontrols方法中呈现许多文字控件。如下所示(还有其他一些文字控件添加,我没有在这里删除)

this.Controls.Add(new LiteralControl(string.Format("<input type=\"text\" style=\"display:none\" value=\"{0}\" id=\"{1}\" name=\"{1}\" />", MediaId.ToString(), this.UniqueID)));

然后我尝试通过在类的顶部添加[ValidationProperty(“myprop”)]来验证此文本框。 基本上我需要验证输入到文本框中的值。 myprop属性如下

public string myprop
    {
        get
        {
            Control ctrl = this.FindControl(this.UniqueID);
            string txt = ((TextBox)ctrl).Text;

            try
            {
                MediaId = Convert.ToInt32(txt);
            }
            catch { MediaId = 0; }
            return txt;
        }
    }

不幸的是,findcontrol根本没有找到文本框,我认为因为.net关注的是文字控制,而不是文本框

现在肯定我可以更改createchildcontrols来执行此操作

        TextBox tb = new TextBox();
        tb.Text = this.MediaId.ToString();
        tb.ID = this.UniqueID;
        this.Controls.Add(tb);

但由于我正在做的其他限制,我将不得不在其他地方更改更多的东西..

有没有找到findcontrol来查找文字内部或其他方法中呈现的文本框?

谢谢

NAT

1 个答案:

答案 0 :(得分:0)

  

不幸的是,findcontrol没有   我认为,根本找到文本框   因为就.net而言   它是一个字面控件,而不是文本框   所有

这是正确的。您必须使用其他控件,如文本框。