Click here to Skip to main content
16,018,318 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have a catalog function whereby user can filter their selection by clicking radiobutton. For example, by selecting the Dining radiobutton, all packages related to dining would appear.

And i using DataList1.Items.Count method to count the number search result. I had implement this method in the page_load, however the value of the number of datalist keep displaying previously loaded datalist.

Here is my code :

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.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;

       }

   }
Posted

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


Clear your SQLDataSource before binding it again.



Thanks
Ashish
 
Share this answer
 
Hard to say what you're trying to achieve, but problem is probably that during Page_Load DataList1 is not 'databound' to new query results.
Only after you call DataList1.DataBind() you'll get new count correctly.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900