0

我有一个目录功能,用户可以通过单击单选按钮来过滤他们的选择。例如,通过选择餐饮单选按钮,所有与餐饮相关的包裹都会出现。

我使用 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 放在此代码的其他位置,但同样的问题仍然存在。

4

1 回答 1

0
 Try this,
protected void Page_Load(object sender, EventArgs e)  
 {  
     if(!Page.IsPostBack) 
     {           
         DataList1.DataSourceID = "SqlDataSource3";
         Label1.Text = DataList1.Items.Count.ToString();   
     } 
 } 
于 2012-07-24T11:53:27.583 回答