0

我有一个自定义控件,它在 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 找到文本中呈现的文本框,或者其他方法?

谢谢

纳特

4

1 回答 1

0

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

那是对的。您必须使用其他一些控件,例如文本框。

于 2010-05-04T14:37:26.640 回答