从控件引用HTML输入文本

时间:2017-07-04 18:35:03

标签: html asp.net visual-studio-2013

如果是输入文本,有没有办法引用当前控件的值?

foreach (Control c in line1.Controls)
{
    if (c.GetType().ToString() == "System.Web.UI.HtmlControls.HtmlInputText")
    {
        c.ID.Value() = "test";
    }
}

我有一个HTML页面,我想循环控制,并在循环中设置它们的值。我错了吗?我找不到从控件中引用HTmlInputText的方法吗?

1 个答案:

答案 0 :(得分:2)

您可以使用is

检查控件
foreach (Control c in line1.Controls)
{
    //check if the control is a textbox
    if (c is TextBox)
    {
        //cast it back to a textbox to access its properies
        TextBox tb = c as TextBox;
        tb.Text = "TextBox found";

        //or set the id
        c.ID = "test";
    }
}

或通用控件

foreach (Control c in line1.Controls)
{
    //check if the control is a HtmlInputControl
    if (c is HtmlInputControl)
    {
        //cast it back to a HtmlInputControl to access its properies
        HtmlInputControl hic = c as HtmlInputControl;
        hic.Value = "HTML TextBox found";

        //or set the id
        c.ID = "test";
    }
}