读书人

怎么查询页面上所有textbox并赋值

发布时间: 2012-02-17 17:50:42 作者: rapoo

如何查询页面上所有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++)
{
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 = "";
}
}

读书人网 >asp.net

热点推荐