DataList继续显示以前加载的数据列表 [英] DataList keep displaying previously loaded datalist

查看:60
本文介绍了DataList继续显示以前加载的数据列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个目录功能,用户可以通过单击单选按钮过滤其选择.例如,通过选择Dining单选按钮,将显示所有与Dining相关的软件包.

而且我使用DataList1.Items.Count方法来计算数字搜索结果.我已经在page_load中实现了此方法,但是datalist的数量值仍显示以前加载的datalist.

这是我的代码:

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;

       }

   }

推荐答案

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



再次绑定之前,请先清除SQLDataSource.



谢谢
Ashish



Clear your SQLDataSource before binding it again.



Thanks
Ashish


很难说出您要实现的目标,但是问题可能是在Page_Load DataList1期间没有绑定"到新的查询结果.
只有调用DataList1.DataBind()之后,您才能正确获得新计数.
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.


这篇关于DataList继续显示以前加载的数据列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆