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;
} }
这样的代码很冗余
怎么样才能精简

解决方案 »

  1.   

    try
    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();
    }
    }
      

  2.   

    sorry
    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();
    }
    }
      

  3.   

    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 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(); }
      

  4.   

    public static void DataBindFromXml(object sender,string xmlPath)
    {
    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(); }
      

  5.   

    case "System.Web.UI.WebControls.CheckBoxList":
          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()));
       }
    }
      

  6.   

    谢谢大家xiahouwen(武眉博<活靶子.NET>) 的代码最好!
    aspdotnet2005(红枫(想找兼职)) 的想法不错!
    其它的代码也行