public string FormatWebFormTextBox(System.Web.UI.Page pg,string FormatValue)
{
string ResultString = "";
foreach (System.Web.UI.Control Con in pg.Controls)
{
ResultString = Con.ToString();
}
return ResultString;
}
{
string ResultString = "";
foreach (System.Web.UI.Control Con in pg.Controls)
{
ResultString = Con.ToString();
}
return ResultString;
}
不好意思,你说的得到的结果是下面的,是不正确的!不好意思!"System.Web.UI.LiteralControl"
{
string ResultString = "";
foreach (System.Web.UI.Control Con in pg.Controls)
{
//if(Con.)
ResultString = Con.ToString();
if(ResultString=="System.Web.UI.HtmlControls.HtmlForm")
{
foreach (System.Web.UI.Control txt in Con.Controls)
{
//txt.Text="your value";
Response.Write(txt.ToString());
}
}
Response.Write(ResultString+"<br>");
}
return ResultString;
}
{
遍历ojbj参数内的控件,如果是一个TextBox的话,TextBox.Text = FormatValue
}这时显示在WEB页上面的TextBox显示FormatValue的值!
我明白你的意思,如果用想遍历页面中包含的所有控件,为了方便,应该是把这些控件放在一个runat server的form中,也就是一个HtmlForm中,看如下代码是否满足你的要求?
另(你是想写一个通过的页面存取类吧?)
/// <summary>
/// 查找page中所有在form中的控件,并格式化其值
/// 如:FormatWebFormTextBox(this.page)
/// </summary>
/// <param name="pg">page对象</param>
public void FormatWebFormTextBox(System.Web.UI.Page pg)
{
string ResultString = "";
foreach (System.Web.UI.Control Con in pg.Controls)
{
//判断是否form控件
if(Con.GetType()==System.Type.GetType("System.Web.UI.HtmlControls.HtmlForm"))
{
//遍历form内的所有控件,并格式化其值
foreach (System.Web.UI.Control formCon in Con.Controls)
{
//Response.Write(formCon.ToString());
System.Type sType=formCon.GetType();
switch(sType.ToString())
{
case "System.Web.UI.WebControls.TextBox":
((System.Web.UI.WebControls.TextBox)formCon).Text="your format value";
break;
case "System.Web.UI.WebControls.ListBox":
((System.Web.UI.WebControls.ListBox)formCon).SelectedItem.Value="your format value";
break;
case "System.Web.UI.WebControls.HyperLink":
((System.Web.UI.WebControls.HyperLink)formCon).Text="your format value";
((System.Web.UI.WebControls.HyperLink)formCon).NavigateUrl="www.china.com";
break;
case "System.Web.UI.HtmlControls.HtmlInputCheckBox":
((System.Web.UI.HtmlControls.HtmlInputCheckBox)formCon).Checked=true;
break;
default :
((System.Web.UI.WebControls.TextBox)formCon).Text="your format value";
break;
} //end switch
} //end for
} //end if
} //end for
} //end method
说的对,还是你聪明呀,终于明白偶的意思了!