我把对应表中的字段名放在(textbox/dropdownlist/checkbox/radiobutton....)的ID属性里,保存或显示数据时自动把控件的值放到ID对应的字段名里。我的意思就是对界面上不同的控件判断,得出它的值,保存到对应自身ID的字段名中。显示也是同理。比如:有表custom(cu_code,cu_name,cu_type,cu_tel),在界面中有三textbox一dropdownlist:
<asp:TextBox ID="cu_code" runat="server" size=16/>
<asp:TextBox ID="cu_name" runat="server" size=16/>
<asp:DropDownList ID="cu_type" runat="server">
<asp:ListItem value="种类一">种类一</asp:ListItem>
<asp:ListItem value="种类二">种类二</asp:ListItem>
<asp:ListItem value="种类三">种类三</asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="cu_tel" runat="server" size=16/>
,自动查找界面上的所有控件,判断控件的类型(多种类型,我这里只举例两种),取出它们页面上的值,保存到对应ID表示的字段名中去(ID所代表的字段名在指定的表中有,比如这个界面是客户资料的,我指明是表CUSTOM)。
可查看http://community.csdn.net/Expert/topic/4448/4448404.xml?temp=.6515009,这也是我发的贴子,只是还没解决这个问题。
<asp:TextBox ID="cu_code" runat="server" size=16/>
<asp:TextBox ID="cu_name" runat="server" size=16/>
<asp:DropDownList ID="cu_type" runat="server">
<asp:ListItem value="种类一">种类一</asp:ListItem>
<asp:ListItem value="种类二">种类二</asp:ListItem>
<asp:ListItem value="种类三">种类三</asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="cu_tel" runat="server" size=16/>
,自动查找界面上的所有控件,判断控件的类型(多种类型,我这里只举例两种),取出它们页面上的值,保存到对应ID表示的字段名中去(ID所代表的字段名在指定的表中有,比如这个界面是客户资料的,我指明是表CUSTOM)。
可查看http://community.csdn.net/Expert/topic/4448/4448404.xml?temp=.6515009,这也是我发的贴子,只是还没解决这个问题。
public string GetInsertSql(System.Web.UI.ControlCollection formControls,string TableName)
{
string sql1=" ";
string sql2=" ";
foreach (Control ctrl in formControls)
{ if((ctrl is HtmlInputText || ctrl is TextBox) && ctrl.ID.IndexOf("tx_")==0)
{
sql1 +=ctrl.ID.Substring(5)+",";
if(ctrl is HtmlInputText)
{
if(ctrl.ID.IndexOf("tx_i_")==0)
if(((HtmlInputText)ctrl).Value.Trim()==string.Empty)
sql2 +="null,";
// sql2 +="1,";
else
sql2 +=((HtmlInputText)ctrl).Value +",";
else
sql2 +="'" + ((HtmlInputText)ctrl).Value +"',";
} else
{
if(ctrl.ID.IndexOf("tx_i_")==0)
if(((TextBox)ctrl).Text.Trim()==string.Empty)
sql2 +="null,";
// sql2 +="1,";
else
sql2 +=((TextBox)ctrl).Text +",";
else
sql2 +="'" + ((TextBox)ctrl).Text +"',";
}
}
else
if(ctrl is DropDownList && ctrl.ID.IndexOf("ddl_")==0)
{
sql1 +=ctrl.ID.Substring(6)+","; if(ctrl.ID.IndexOf("ddl_i_")==0)
{
if(((DropDownList)(ctrl)).SelectedValue=="")
sql2 +="null,";
else
sql2 +=((DropDownList)(ctrl)).SelectedValue +",";
}
else
sql2 +="'"+((DropDownList)(ctrl)).SelectedValue +"',";
} } if(sql1.Length>1)
{
sql1 = sql1.Substring(0,sql1.Length-1);
sql2 = sql2.Substring(0,sql2.Length-1);
} return "INSERT INTO " +TableName + "("+ sql1 + ") VALUES(" + sql2 + ")";
}