DataList继续显示以前加载的datalist

时间:2012-07-24 10:32:30

标签: c# asp.net user-controls datalist

我有一个目录功能,用户可以通过单击radiobutton来过滤他们的选择。例如,通过选择Dining radiobutton,将显示与用餐相关的所有包裹。

我使用DataList1.Items.Count方法来计算数字搜索结果。我在page_load中实现了这个方法,但是datalist的数量值继续显示以前加载的datalist。

这是我的代码:

 protected void Page_Load(object sender, EventArgs e)
    {  
        DataList1.DataSourceID = "SqlDataSource3";
        Label1.Text = DataList1.Items.Count.ToString(); 
    }


    private SqlDataReader getReader()
    {
        //get connection string from web.config
        string strConnectionString = ConfigurationManager.ConnectionStrings["ASPNETDBConnectionString1"].ConnectionString;
        SqlConnection myConnect = new SqlConnection(strConnectionString);

        string strCommandText = "SELECT CategoryID, CatName  from Category";

        SqlCommand cmd = new SqlCommand(strCommandText, myConnect);
        myConnect.Open();

        //DataList1.DataSource = reader; 

        DataList1.DataBind();

        // CommandBehavior.CloseConnection will automatically close connection
        SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
        return reader;
    }

    protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
    { 
        DataList1.DataSourceID = "SqlDataSource1";


        if (RadioButtonList1.SelectedIndex == 0)
        {
            Session["catID"] = 1;

        }
        else if (RadioButtonList1.SelectedIndex == 1)
        {
            Session["catID"] = 2;

        }
        else if (RadioButtonList1.SelectedIndex == 2)
        {
            Session["catID"] = 3; 

        }
        else if (RadioButtonList1.SelectedIndex == 3)
        {
            Session["catID"] = 4;

        }
        else if (RadioButtonList1.SelectedIndex == 4)
        {
            Session["catID"] = 5;

        }

        else
        {
            Session["catID"] = 8;

        }

    } 

我曾尝试将Item.Count放在此代码的其他位置,但同样的问题仍然存在..

1 个答案:

答案 0 :(得分:0)

 Try this,
protected void Page_Load(object sender, EventArgs e)  
 {  
     if(!Page.IsPostBack) 
     {           
         DataList1.DataSourceID = "SqlDataSource3";
         Label1.Text = DataList1.Items.Count.ToString();   
     } 
 } 
相关问题