![](/Images/OutliningIndicators/ContractedBlock.gif)
遍历获取控件上的值#region 遍历获取控件上的值
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// 字符串首字母大写
/// </summary>
private static string ToProperCase(string s)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string revised = "";
if (s.Length > 0)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
revised = s.Trim();
revised = revised.Substring(0, 1).ToUpper() + revised.Substring(1).ToLower();
}
return revised;
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 设置对象属性的值
/// </summary>
private static void SetProperty(object obj, string name, object value)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
PropertyInfo propertyInfo = obj.GetType().GetProperty(name);
object objValue = Convert.ChangeType(value, propertyInfo.PropertyType);
propertyInfo.SetValue(obj, objValue, null);
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 获取对象属性的值
/// </summary>
private static object GetProperty(object obj, string name)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
PropertyInfo propertyInfo = obj.GetType().GetProperty(name);
return propertyInfo.GetValue(obj, null);
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 设置控件的值
/// </summary>
public static void SetControlValue(Control panel, string str, object obj)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
foreach (Control content in panel.Controls)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (content.ID != null)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string contentID = content.ID.ToLower();
if (contentID.Replace(str, "") != contentID.ToLower())
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (content.GetType() != typeof(DropDownList))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
SetProperty(content, "Text", GetProperty(obj, ToProperCase(contentID.Replace(str, ""))));
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
DropDownList ddl = (DropDownList)content;
ddl.SelectedIndex = -1;
ddl.Items.FindByValue(
(string)GetProperty(obj, ToProperCase(contentID.Replace(str, "")))
).Selected = true;
}
}
}
}
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 设置对象的值
/// </summary>
public static void SetObjectValue(Control panel, string str, ref object obj)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
foreach (Control content in panel.Controls)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//if (content.Controls.Count > 0) //将控件都放在了一个Panel里面所以就不用这句了
//{
// SetObjectValue(content, str, obj);
//}
if (content.ID != null)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string contentID = content.ID.ToLower();
if (contentID.Replace(str, "") != contentID.ToLower())
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
SetProperty(obj, ToProperCase(contentID.Replace(str, "")),
GetProperty(content, "Text")
);
}
}
}
}
#endregion
![](/Images/OutliningIndicators/ContractedBlock.gif)
测试用的类#region 测试用的类
public class TestClass
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
public TestClass()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_aa = "1";
_bb = "2";
_cc = "3";
_dd = 4;
_ee = 5;
_ff = DateTime.Now;
_gg = 7;
}
![](/Images/OutliningIndicators/InBlock.gif)
private string _aa;
![](/Images/OutliningIndicators/InBlock.gif)
public string Aa
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _aa; }
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _aa = value; }
}
private string _bb;
![](/Images/OutliningIndicators/InBlock.gif)
public string Bb
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _bb; }
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _bb = value; }
}
private string _cc;
![](/Images/OutliningIndicators/InBlock.gif)
public string Cc
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _cc; }
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _cc = value; }
}
private int _dd;
![](/Images/OutliningIndicators/InBlock.gif)
public int Dd
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _dd; }
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _dd = value; }
}
private int _ee;
![](/Images/OutliningIndicators/InBlock.gif)
public int Ee
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _ee; }
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _ee = value; }
}
private DateTime _ff;
![](/Images/OutliningIndicators/InBlock.gif)
public DateTime Ff
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _ff; }
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _ff = value; }
}
private decimal _gg;
![](/Images/OutliningIndicators/InBlock.gif)
public decimal Gg
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _gg; }
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _gg = value; }
}
![](/Images/OutliningIndicators/InBlock.gif)
}
#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="获取"/>
![](/Images/OutliningIndicators/None.gif)
protected void Button1_Click(object sender, EventArgs e)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
object test = new TestClass();
WebUtility.SetObjectValue(this.Panel1, "txt_", ref test);
Response.Write( ((TestClass)test).Aa );
}