如何通过arraylist来切换语句

时间:2011-04-07 16:14:44

标签: asp.net viewstate switch-statement

我想将arraylist传递给switch语句,下面是代码:

 protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
{

     ArrayList myList = new ArrayList();
    foreach (ListItem listitem in CheckBoxList1.Items)
    {
        if (listitem.Selected)
            myList.Add(listitem.Value);

    }

    ViewState["myList"] = myList;

    CurrentPage = 0;
    BindGrid();


}
 private void BindGrid()
{

    DataTable dt = null;

    switch (ViewState["myList"]) //gives an error
    { case myList[1]:  dt = caravans.GetSelectedFilter(myList); break;
    default:  dt = caravans.GetAllCaravans(); break;

    pds.DataSource = dt.DefaultView;
    pds.AllowPaging = true;
    pds.PageSize = 12;//add the page index when item exceeds 12     //Convert.ToInt16(ddlPageSize.SelectedValue);
    pds.CurrentPageIndex = CurrentPage;
    DataList1.RepeatColumns = 4; // 4 items per line
    DataList1.RepeatDirection = RepeatDirection.Horizontal;

    DataList1.DataSource = pds;
    DataList1.DataBind();

 //   lnkbtnNext.Enabled = !pds.IsLastPage;
    ImageButton2.Enabled = !pds.IsLastPage;
   // lnkbtnPrevious.Enabled = !pds.IsFirstPage;

    doPaging();

}

1 个答案:

答案 0 :(得分:0)

来自MSDN:

switch语句的控制类型由switch表达式建立。如果switch表达式的类型是sbyte,byte,short,ushort,int,uint,long,ulong,char,string或enum-type,那么这就是switch语句的管理类型。否则,从switch表达式的类型到以下可能的控制类型之一,必须存在一个用户定义的隐式转换(第6.4节):sbyte,byte,short,ushort,int,uint,long,ulong,char,string 。 如果不存在此类隐式转换,或者存在多个此类隐式转换,则会发生编译时错误。

The Switch Statement