0

ユーザーがラジオボタンをクリックして選択をフィルタリングできるカタログ機能があります。たとえば、[ダイニング]ラジオボタンを選択すると、ダイニングに関連するすべてのパッケージが表示されます。

そして、DataList1.Items.Countメソッドを使用して、検索結果の数をカウントします。このメソッドをpage_loadに実装しましたが、データリストの数の値は、以前にロードされたデータリストを表示し続けます。

これが私のコードです:

 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 に答える