
遍历获取控件上的值#region 遍历获取控件上的值

/**//// 字符串首字母大写
/// </summary>
private static string ToProperCase(string s)

{
string revised = "";
if (s.Length > 0)

{
revised = s.Trim();
revised = revised.Substring(0, 1).ToUpper() + revised.Substring(1).ToLower();
}
return revised;
}


/**//// <summary>
/// 设置对象属性的值
/// </summary>
private static void SetProperty(object obj, string name, object value)

{
PropertyInfo propertyInfo = obj.GetType().GetProperty(name);
object objValue = Convert.ChangeType(value, propertyInfo.PropertyType);
propertyInfo.SetValue(obj, objValue, null);
}


/**//// <summary>
/// 获取对象属性的值
/// </summary>
private static object GetProperty(object obj, string name)

{
PropertyInfo propertyInfo = obj.GetType().GetProperty(name);
return propertyInfo.GetValue(obj, null);
}





/**//// <summary>
/// 设置控件的值
/// </summary>
public static void SetControlValue(Control panel, string str, object obj)

{
foreach (Control content in panel.Controls)

{
if (content.ID != null)

{
string contentID = content.ID.ToLower();
if (contentID.Replace(str, "") != contentID.ToLower())

{
if (content.GetType() != typeof(DropDownList))

{
SetProperty(content, "Text", GetProperty(obj, ToProperCase(contentID.Replace(str, ""))));
}
else

{
DropDownList ddl = (DropDownList)content;
ddl.SelectedIndex = -1;
ddl.Items.FindByValue(
(string)GetProperty(obj, ToProperCase(contentID.Replace(str, "")))
).Selected = true;
}
}
}
}
}



/**//// <summary>
/// 设置对象的值
/// </summary>
public static void SetObjectValue(Control panel, string str, ref object obj)

{
foreach (Control content in panel.Controls)

{
//if (content.Controls.Count > 0) //将控件都放在了一个Panel里面所以就不用这句了
//{
// SetObjectValue(content, str, obj);
//}
if (content.ID != null)

{
string contentID = content.ID.ToLower();
if (contentID.Replace(str, "") != contentID.ToLower())

{
SetProperty(obj, ToProperCase(contentID.Replace(str, "")),
GetProperty(content, "Text")
);
}
}
}
}
#endregion

测试用的类#region 测试用的类
public class TestClass


{
public TestClass()

{
_aa = "1";
_bb = "2";
_cc = "3";
_dd = 4;
_ee = 5;
_ff = DateTime.Now;
_gg = 7;
}

private string _aa;

public string Aa

{

get
{ return _aa; }

set
{ _aa = value; }
}
private string _bb;

public string Bb

{

get
{ return _bb; }

set
{ _bb = value; }
}
private string _cc;

public string Cc

{

get
{ return _cc; }

set
{ _cc = value; }
}
private int _dd;

public int Dd

{

get
{ return _dd; }

set
{ _dd = value; }
}
private int _ee;

public int Ee

{

get
{ return _ee; }

set
{ _ee = value; }
}
private DateTime _ff;

public DateTime Ff

{

get
{ return _ff; }

set
{ _ff = value; }
}
private decimal _gg;

public decimal Gg

{

get
{ return _gg; }

set
{ _gg = value; }
}

}
#endregion
<asp:Panel ID="Panel1" runat="server" Height="150px" Width="181px">
AA:<asp:TextBox ID="txt_aa" runat="server"></asp:TextBox><br />
BB:<asp:TextBox ID="txt_bb" runat="server"></asp:TextBox><br />
CC:<asp:DropDownList ID="txt_cc" runat="server">
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
<asp:ListItem>5</asp:ListItem>
<asp:ListItem>6</asp:ListItem>
<asp:ListItem>7</asp:ListItem>
</asp:DropDownList><br />
DD:<asp:TextBox ID="txt_dd" runat="server"></asp:TextBox><br />
EE:<asp:TextBox ID="txt_ee" runat="server"></asp:TextBox><br />
FF:<asp:TextBox ID="txt_ff" runat="server"></asp:TextBox><br />
GG:<asp:TextBox ID="txt_gg" runat="server"></asp:TextBox>
</asp:Panel>
<br />
<asp:Button ID="Button1" runat="server" Text="保存" OnClick="Button1_Click" /> <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="获取"/>

protected void Button1_Click(object sender, EventArgs e)

{
object test = new TestClass();
WebUtility.SetObjectValue(this.Panel1, "txt_", ref test);
Response.Write( ((TestClass)test).Aa );
}