我的代码如下(asp.net2005)
For Each c As Control In Page.Controls
            If TypeOf c Is TextBox Then
                Select Case c.ClientID
                    Case "TextBox1"
                        DirectCast(c, TextBox).Text = "a1"
                    Case "TextBox2"
                        DirectCast(c, TextBox).Text = "a2"
                    Case "TextBox3"
                        DirectCast(c, TextBox).Text = "a3"
                End Select
            End If
        Next不知道哪里出错,就是不能赋值。请各路高手指点

解决方案 »

  1.   

    For Each c As Control In Page.Controls 
    =====
    For Each c As Control In Page.Form.Controls 
      

  2.   

    foreach (Control cc in Page.Form.Controls)
                {
                    if (cc.GetType() ==typeof(TextBox))
                    {
                        ((TextBox)cc).Text = "aa";
                    }
                }
      

  3.   


    For Each c As Control In Me.Controls 
                If c Is TextBox Then 
                    Select Case c.ClientID 
                        Case "TextBox1" 
                            DirectCast(c, TextBox).Text = "a1" 
                        Case "TextBox2" 
                            DirectCast(c, TextBox).Text = "a2" 
                        Case "TextBox3" 
                            DirectCast(c, TextBox).Text = "a3" 
                    End Select 
                End If 
            Next 
      

  4.   

    string id = string.Empty;
                string controlID = string.Empty;
                for (int i = 0; i < this.Controls.Count; i++)
                {
                    foreach (System.Web.UI.Control control in this.Controls[i].Controls)
                    {
                        if (control is TextBox)
                        {
                            controlID = (control as TextBox).ID;
                            string[] textBoxHeads = textBoxHead.Split(',');
                            for (int j = 0; j < textBoxHeads.Length; j++)
                            {
                                if (controlID.IndexOf(textBoxHeads[j]) == 0)
                                {
                                    id = controlID.Substring(textBoxHeads[j].Length);
                                    if (dt.ContainCol(id))
                                    {
                                        //try
                                        //{
                                        (control as TextBox).Text = dt.GetValue(id).Trim();
                                        //}
                                        //catch
                                        //{
                                        //}
                                    }
                                }
                            }                    }
                        else if (control is HtmlInputHidden)
                        {
                            controlID = (control as HtmlInputHidden).ID;
                            string[] valueListHeads = hiValueHead.Split(',');
                            for (int j = 0; j < valueListHeads.Length; j++)
                            {
                                if (controlID.IndexOf(valueListHeads[j]) == 0)
                                {
                                    id = controlID.Substring(valueListHeads[j].Length);
                                    if (dt.ContainCol(id))
                                    {
                                        (control as HtmlInputHidden).Value = dt.GetValue(id).Trim();
                                    }
                                }
                            }
                        }
                        else if (control is HtmlInputText)
                        {
                            controlID = (control as HtmlInputText).ID;
                            string[] valueListHeads = hiValueHead.Split(',');
                            for (int j = 0; j < valueListHeads.Length; j++)
                            {
                                if (controlID.IndexOf(valueListHeads[j]) == 0)
                                {
                                    id = controlID.Substring(valueListHeads[j].Length);
                                    if (dt.ContainCol(id))
                                    {
                                        (control as HtmlInputText).Value = dt.GetValue(id).Trim();
                                    }
                                }
                            }
                        }
                        else if (control is DropDownList)
                        {
                            controlID = (control as DropDownList).ID;
                            string[] dropDownListHeads = dropDownListHead.Split(',');
                            for (int j = 0; j < dropDownListHeads.Length; j++)
                            {
                                if (controlID.IndexOf(dropDownListHeads[j]) == 0)
                                {
                                    id = controlID.Substring(dropDownListHeads[j].Length);
                                    if (dt.ContainCol(id))
                                    {
                                        (control as DropDownList).SelectedValue = dt.GetValue(id).Trim();
                                    }
                                }
                            }
                        }
                        //(control as TextBox).Text = "";
                    }
                }