有时根据选择不同的数据表,要生成不同的输入页面,这些数据表的结构各不相同,这时就要动态输出控件组成输入页面了,把这些表的结构放在一个专门的TYPE表里,如中英文字段名称,类型,大小,中英文表名称等。
首先在页面上放置一个TABLE控件,放置一个DropDownList控件,DropDownList绑定数据库中要生成输入页面的表名称。
生成页面代码如下:
cnn.open();
int cellw;
string type;
string Sql ="select CHINANAME,HOW,TYPE from Type where OWNER ='" + DropDownList1.SelectedValue +"'";
OleDbDataAdapter myDa =new OleDbDataAdapter();
myDa.SelectCommand =new OleDbCommand(Sql,cnn);
DataSet myDs =new DataSet();
myDa.Fill(myDs,"Type");
for(int i=0; i<myDs.Tables[0].Rows.Count; i++)
{
TableRow newrow=new TableRow();
TableCell newcell1=new TableCell();
TableCell newcell2=new TableCell();
TableCell newcell3=new TableCell();
Label myLab =new Label();
myLab.ID = "Labe" + i.ToString();
myLab.Text = ""+myDs.Tables[0].Rows[i].ItemArray.GetValue(0).ToString()+":";
myLab.EnableViewState =true;
myLab.Width = 100;
TextBox myTxt =new TextBox();
myTxt.ID = "Txt" + i.ToString();
myTxt.EnableViewState=true;
type = myDs.Tables[0].Rows[i].ItemArray.GetValue(2).ToString();
cellw = int.Parse(myDs.Tables[0].Rows[i].ItemArray.GetValue(1).ToString());
if(cellw<=50)
{
myTxt.Height = 24;
myTxt.Width = 400;
}
if(cellw>50&&cellw<=100)
{
myTxt.Height = 50;
myTxt.Width = 400;
myTxt.TextMode = TextBoxMode.MultiLine;
}
if(cellw>100&&cellw<=200)
{
myTxt.Height = 60;
myTxt.Width = 400;
myTxt.TextMode = TextBoxMode.MultiLine;
}
if(cellw>200&&cellw<=500)
{
myTxt.Height = 80;
myTxt.Width = 400;
myTxt.TextMode = TextBoxMode.MultiLine;
}
newcell1.Controls.Add(myLab);
newrow.Cells.Add(newcell1);
newcell2.Controls.Add(myTxt);
newrow.Cells.Add(newcell2);
if(type=="decimal")
{
RegularExpressionValidator rev =new RegularExpressionValidator();
rev.ID = "rev" + i.ToString();
rev.ErrorMessage = "本行只能填写数字,请从新输入!" ;
rev.ControlToValidate = "Txt"+i.ToString()+"";
rev.ValidationExpression = @"\d+(\.\d+)?$";
rev.Width = 250;
newcell3.Controls.Add(rev);
newrow.Cells.Add(newcell3);
}
Table1.Rows.Add(newrow);
}
myDs.Clear();
cnn.Close();
把生成页面输入的数据保存到数据库:
string strsql ="insert into "+bm+" (";
try
{
for(int i=0; i<myDs.Tables[0].Rows.Count; i++)
{
strsql = strsql + myDs.Tables[0].Rows[i].ItemArray.GetValue(1).ToString() +",";
}
strsql = strsql.Substring(0,strsql.Length-1) +") values (";
for(int i=0; i<myDs.Tables[0].Rows.Count; i++)
{
TextBox myTxt = (TextBox)Table1.FindControl("Txt"+i.ToString());
strsql = strsql + "'" + myTxt.Text.Replace("'","''") + "',";
}
strsql = strsql.Substring(0,strsql.Length-1) +")";
OleDbCommand myCommand=new OleDbCommand(strsql,cnn);
myCommand.ExecuteNonQuery();
}
catch
{
}
本文地址:http://com.8s8s.com/it/it8326.htm