日期:2014-05-18  浏览次数:20482 次

如何查询页面上所有textbox,并赋值。
我的代码如下(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

不知道哪里出错,就是不能赋值。请各路高手指点

------解决方案--------------------
VB.NET code

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

------解决方案--------------------
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++)