public static void DataBindFromXml(object sender,string xmlPath){
DataSet ds=new DataSet();
ds.ReadXml(System.Web.HttpContext.Current.Server.MapPath(xmlPath));
string tableName=ds.Tables[0].TableName;
switch(sender.GetType().ToString())
{
case "System.Web.UI.WebControls.CheckBoxList":
CheckBoxList cbl=(CheckBoxList)sender;
cbl.DataSource=ds.Tables[0].DefaultView;
cbl.DataTextField="text";
cbl.DataValueField="value";
cbl.DataBind();
break;
case "System.Web.UI.WebControls.RadioButtonList":
RadioButtonList rbl=(RadioButtonList)sender;
rbl.DataSource=ds.Tables[0].DefaultView;
rbl.DataTextField="text";
rbl.DataValueField="value";
rbl.DataBind();
break;
case "System.Web.UI.WebControls.DropDownList":
DropDownList ddl=(DropDownList)sender;
ddl.DataSource=ds.Tables[0].DefaultView;
ddl.DataTextField="text";
ddl.DataValueField="value";
ddl.DataBind();
break;
default:
break;
} }
这样的代码很冗余
怎么样才能精简
DataSet ds=new DataSet();
ds.ReadXml(System.Web.HttpContext.Current.Server.MapPath(xmlPath));
string tableName=ds.Tables[0].TableName;
switch(sender.GetType().ToString())
{
case "System.Web.UI.WebControls.CheckBoxList":
CheckBoxList cbl=(CheckBoxList)sender;
cbl.DataSource=ds.Tables[0].DefaultView;
cbl.DataTextField="text";
cbl.DataValueField="value";
cbl.DataBind();
break;
case "System.Web.UI.WebControls.RadioButtonList":
RadioButtonList rbl=(RadioButtonList)sender;
rbl.DataSource=ds.Tables[0].DefaultView;
rbl.DataTextField="text";
rbl.DataValueField="value";
rbl.DataBind();
break;
case "System.Web.UI.WebControls.DropDownList":
DropDownList ddl=(DropDownList)sender;
ddl.DataSource=ds.Tables[0].DefaultView;
ddl.DataTextField="text";
ddl.DataValueField="value";
ddl.DataBind();
break;
default:
break;
} }
这样的代码很冗余
怎么样才能精简
解决方案 »
- buy的调试问题和技巧!
- win7 下IIS很奇怪的问题!救命呀,急。。。。。
- 救命呀,谁能告诉我JQuery Upload 批量上传不显示BROWSE 按钮问题!
- gridview 编辑按钮 事件
- Response.write()用法
- 无法将类型为“SYSTEM.WEB.UI.WEBCONTROLS.GRIDVIEW”的对象强制转换为"SYSTEM.WEB.UI.BUTTON"
- 使用vs2005,c#做的,读取Oracle数据库时为乱码,在plsql里记取的是正常的中文
- 请教,WebUserControl,DataGrid,DropDownList的问题
- asp.net基础问题(Convert,Parse,ToString类型转换),请高手详细说明下
- .Net SqlClient 数据提供程序不支持 CommandType.TableDirect
- 问几个html比较简单的问题啊
- C#.net 读取数据库连接字符串问题
public static void DataBindFromXml(object sender,string xmlPath)
{
DataSet ds=new DataSet();
ds.ReadXml(System.Web.HttpContext.Current.Server.MapPath(xmlPath));
string tableName=ds.Tables[0].TableName;
ListBoxControl cbl = sender as ListBoxControl ;
if(cbl != null)
{
cbl.DataSource=ds.Tables[0].DefaultView;
cbl.DataTextField="text";
cbl.DataValueField="value";
cbl.DataBind();
}
}
public static void DataBindFromXml(object sender,string xmlPath)
{
DataSet ds=new DataSet();
ds.ReadXml(System.Web.HttpContext.Current.Server.MapPath(xmlPath));
string tableName=ds.Tables[0].TableName;
ListControl cbl = sender as ListControl ;
if(cbl != null)
{
cbl.DataSource=ds.Tables[0].DefaultView;
cbl.DataTextField="text";
cbl.DataValueField="value";
cbl.DataBind();
}
}
DataSet ds=new DataSet();
ds.ReadXml(System.Web.HttpContext.Current.Server.MapPath(xmlPath));
string tableName=ds.Tables[0].TableName;
switch(sender.GetType().ToString())
{
case "System.Web.UI.WebControls.CheckBoxList":
CheckBoxList ddl=(CheckBoxList)sender;
break;
case "System.Web.UI.WebControls.RadioButtonList":
RadioButtonList ddl=(RadioButtonList)sender;
break;
case "System.Web.UI.WebControls.DropDownList":
DropDownList ddl=(DropDownList)sender; break;
default:
break;
}
ddl.DataSource=ds.Tables[0].DefaultView;
ddl.DataTextField="text";
ddl.DataValueField="value";
ddl.DataBind(); }
{
DataSet ds=new DataSet();
ds.ReadXml(System.Web.HttpContext.Current.Server.MapPath(xmlPath));
DataView dv = ds.Tables[0].DefaultView;
ListControl ls ;
switch(sender.GetType().ToString())
{
case "System.Web.UI.WebControls.CheckBoxList":
ls =(CheckBoxList)sender;
break;
case "System.Web.UI.WebControls.RadioButtonList":
ls=(RadioButtonList)sender;
break;
case "System.Web.UI.WebControls.DropDownList":
ls =(DropDownList)sender;
break;
default:
break;
}
ls.DataSource=dv;
ls.DataTextField="text";
ls.DataValueField="value";
ls.DataBind(); }
ItemBind(((CheckBoxList)sender).Items);
break;
case "System.Web.UI.WebControls.RadioButtonList":
ItemBind(((RadioButtonLIst)sender).Items);
break;
................
ItemBind(ListItemCollection items)
{
foreach(DataRow row in ds.Tables.Rows)
{
items.Add(new ListItem(row["text"].ToString(),row["value"].ToString()));
}
}
aspdotnet2005(红枫(想找兼职)) 的想法不错!
其它的代码也行