如下:info.是实体类,txtcustomData1就是TextBox文本了,下面的意思是如果TextBox显示了,就用TextBox里的值赋值到info里,如果TextBox是隐藏的,就用DropDownList的值赋给info.现在有差不多100个这样的判断,请问大家有什么好方法解决没?for里能写吗?开始用 for写但不管用.谢谢大家了,帮忙解决下,会的把代码贴出来.被这折磨死了!!问题解决了再加分!!!!!!!!!!!!!!!!!!!!!!1
if (txtcustomData1.Visible == true)
{
info.CustomData1 = this.txtcustomData1.Text.Trim();
}
else
{
info.CustomData1 = this.DropDownList1.SelectedValue.Trim();
}
if (txtcustomData2.Visible == true)
{
info.CustomData2 = this.txtcustomData2.Text.Trim();
}
else
{
info.CustomData2 = this.DropDownList2.SelectedValue.Trim();
}
if (txtcustomData3.Visible == true)
{
info.CustomData3 = this.txtcustomData3.Text.Trim();
}
else
{
info.CustomData3 = this.DropDownList3.SelectedValue.Trim();
}
if (txtcustomData4.Visible == true)
{
info.CustomData4 = this.txtcustomData4.Text.Trim();
}
else
{
info.CustomData4 = this.DropDownList4.SelectedValue.Trim();
}
if (txtcustomData1.Visible == true)
{
info.CustomData1 = this.txtcustomData1.Text.Trim();
}
else
{
info.CustomData1 = this.DropDownList1.SelectedValue.Trim();
}
if (txtcustomData2.Visible == true)
{
info.CustomData2 = this.txtcustomData2.Text.Trim();
}
else
{
info.CustomData2 = this.DropDownList2.SelectedValue.Trim();
}
if (txtcustomData3.Visible == true)
{
info.CustomData3 = this.txtcustomData3.Text.Trim();
}
else
{
info.CustomData3 = this.DropDownList3.SelectedValue.Trim();
}
if (txtcustomData4.Visible == true)
{
info.CustomData4 = this.txtcustomData4.Text.Trim();
}
else
{
info.CustomData4 = this.DropDownList4.SelectedValue.Trim();
}
解决方案 »
- 服务器上出现Microsoft Jet 数据库引擎打不开文件''。 它已经被别的用户以独占方式打开,或没有查看数据的权限。
- asp.NET 前台一个function 后台怎么调用
- checkbox小问题(new)
- 浮动窗口怎样浮动在下拉框上面
- accessdatasource控件获得一个textbox空间里面的text然后insert进一个表怎么做?还有,用向导配置accessdatasource的时候,没有select语句就无法点下一步,为什么?
- 查询反馈速度慢,如何解决?
- 大家帮忙挑毛病
- 怎样排名?
- 在asp.net中怎样将登陆名存入cookie?
- 求助:实现分权限登录
- DES加密、MD5加密比较
- JavaScript中怎么获得GridView中的值
textbox 是否在父控件内?
Control.ControlCollection cons = form1.Controls; ;
foreach(Control con in cons)
{
if (con.GetType() == (new TextBox()).GetType())
{ }
}我觉得这样可以遍历所有的textBox
或者更简单的,用vb的CallByName但是... info真的有100个属性吗?考虑修改下设计吧...
{
if (control is System.Windows.Forms.TextBox)
{
System.Windows.Forms.TextBox tb = (System.Windows.Forms.TextBox)control ;
tb.Text = String.Empty ;
}
}
{
if ((this.form1.Controls[i] is TextBox) && (this.form1.Controls[i] as TextBox).Visible)
{
Response.Write((this.form1.Controls[i] as TextBox).Text);
}
}
试试
FOR (int i=0,i<1000000000,i++)
{
string stra = "txtcustomData" + i.ToString;
string strb = "DropDownList" + i.ToString;
string strc = "CustomData" + i.ToString;
TextBox TextBoxa = (TextBox)this.FindControl(stra);
DropDownList DropDownLista = (DropDownList)this.FindControl(strb);if (TextBoxa.Visible == true)
{
info.strc = this.TextBoxa.Text.Trim();
}
else
{
info.strc = this.DropDownLista.SelectedValue.Trim();
}}
for (int i=0,i<100,i++)
{
string stra = "txtcustomData" + i.ToString();
string strb = "DropDownList" + i.ToString();
string strc = "CustomData" + i.ToString();
TextBox TextBoxa = (TextBox)this.FindControl(stra);
DropDownList DropDownLista = (DropDownList)this.FindControl(strb);if (TextBoxa.Visible == true)
{
info.strc = this.TextBoxa.Text.Trim();
}
else
{
info.strc = this.DropDownLista.SelectedValue.Trim();
}}
传递参数的为数组 如果属性类型不一致就对象数组。赋值的时候用for循环
类似这样
TextBox txt;
DropDownList ddl;
object [] obj=new object[10];
for(int i=0;i<10;i++)
{
txt=FindControl("txtcustomData"+i.ToString()) as TextBox ;
//默认TextBox 控件加载在页面上,如果加载在别的地方 调用相应的FindControl方法
if(txt!=null)
{
if(txt.Visible == true)
{
obj[i]=txt.Text.Trim();
}
else
{
ddl = FindControl("DropDownList"+i.ToString()) as DropDownList;
if(ddl!=null)
{
obj[i]= ddl.SelectedValue.Trim();
}
}
}
}
info.SetValues(obj);info.SetValues方法 你可以这样写public void SetValues (object [] obj)
{
info.CustomData1 =obj[0];
info.CustomData2 =obj[1];
info.CustomData3 =obj[2];
...}
for (int i=0,i<100,i++)
{
string stra = "txtcustomData" + i.ToString();
string strb = "DropDownList" + i.ToString();
string strc = "CustomData" + i.ToString();
TextBox TextBoxa = (TextBox)this.FindControl(stra);
DropDownList DropDownLista = (DropDownList)this.FindControl(strb);
if (TextBoxa!=null && DropDownLista!=null)
{
if (TextBoxa.Visible == true)
{
info.strc = this.TextBoxa.Text.Trim();
}
else
{
info.strc = this.DropDownLista.SelectedValue.Trim();
}
}
}
string strc = "CustomData" + i.ToString();
info.strc
你真有创意~ 至少你出现在专家列表里,别想当然好么
{
Request.Form["txtName" + i].ToString().Trim();
}
{
string ASP_ID;
/// <summary>
/// ID
/// </summary>
public string ASP_ID1
{
get { return ASP_ID; }
set { ASP_ID = value; }
} string ASP_NAME;
/// <summary>
/// 名字
/// </summary>
public string ASP_NAME1
{
get { return ASP_NAME; }
set { ASP_NAME = value; }
}
}info.SetValues(obj); 这错误提示,info.里不包含SetValues的定义,..info.SetValues方法 你可以这样写public void SetValues (object [] obj)
{
info.CustomData1 =obj[0].ToString();//后面得写.ToString()了``呵呵``
info.CustomData2 =obj[1].ToString();
info.CustomData3 =obj[2].ToString();
...}
Control.ControlCollection cons = form1.Controls; ;
foreach(Control con in cons)
{
if (con.GetType() == (new TextBox()).GetType())
{ }
}人力资源
C# code
for (int i=0,i<100,i++)
{
string stra = "txtcustomData" + i.ToString();
string strb = "DropDownList" + i.ToString();
string strc = "CustomData" + i.ToString();<-------对象的对应属性字符 TextBox TextBoxa = (TextBox)this.FindControl(stra);
DropDownList DropDownLista = (DropDownList)this.FindControl(strb);
if (TextBoxa!=null && DropDownLista!=null)
{
string value=TextBoxa.Visible?TextBoxa.text:DropDownLista.SelectedValue
info.GetType().GetProperty(strc).SetValue(info,value,null) <----------------反射字符为属性赋值
}
}
和风的想法一样 他估计失误忘记反射了
另外,界面上放 100 个这样的 TextBox 或者 label 吗?这个对用户可能也是比较灾难的情况。
{
TextBox tb=FindControl("txtcustomData1"+"i") as TextBox;
}
得到TEXTBOX实例你就可以随便操作了