在.net中回发时重复下拉列表中的项目

时间:2014-01-21 09:43:49

标签: c# .net drop-down-menu

我有一个包含6个项目的下拉列表。当在下拉列表中选择一个项目时,一个gridview与一些数据绑定。它正常工作。但是在回发时,下拉列表中的项目会重复。在page_load事件中编写的代码。

 string m = DateTime.Now.ToString("MMMM");
        string y = DateTime.Now.Year.ToString();
        DropDownList1.Items.Add(m + " " + y);
        if (DateTime.Now.AddMonths(1).Month.ToString().Equals("January"))
            y = DateTime.Now.AddYears(1).Year.ToString();
        DropDownList1.Items.Add(DateTime.Now.AddMonths(1).ToString("MMMM") + " " + y);
        if (DateTime.Now.AddMonths(2).Month.ToString().Equals("January"))
            y = DateTime.Now.AddYears(1).Year.ToString();
        DropDownList1.Items.Add(DateTime.Now.AddMonths(2).ToString("MMMM") + " " + y);
        if (DateTime.Now.AddMonths(3).Month.ToString().Equals("January"))
            y = DateTime.Now.AddYears(1).Year.ToString();
        DropDownList1.Items.Add(DateTime.Now.AddMonths(3).ToString("MMMM") + " " + y);
        if (DateTime.Now.AddMonths(4).Month.ToString().Equals("January"))
            y = DateTime.Now.AddYears(1).Year.ToString();
        DropDownList1.Items.Add(DateTime.Now.AddMonths(4).ToString("MMMM") + " " + y);
        if (DateTime.Now.AddMonths(5).Month.ToString().Equals("January"))
            y = DateTime.Now.AddYears(1).Year.ToString();
        DropDownList1.Items.Add(DateTime.Now.AddMonths(5).ToString("MMMM") + " " + y);
         string date = DropDownList1.SelectedItem.Text;
        connect con = new connect(date);
        IList<connect.Session> records = con.getToken();
        GridView1.DataSource = records;
        GridView1.DataBind();

任何帮助表示感谢。

2 个答案:

答案 0 :(得分:0)

使用Page.IsPostBack - 属性检查页面是否第一次加载。否则,由于启用DataBind,您无需再次GridView ViewState

所以在Page_Load

if(!Page.IsPostBack)
{
   // code above 
}

答案 1 :(得分:0)

Page.IsPostBack的经典用法是数据绑定/控件初始化。

if(!Page.IsPostBack)
{
   //Control Initialization
   //Databinding
}

不需要在每次回发时重新创建ViewState和ControlState上持久存储的内容,因此您需要检查此条件以避免执行不必要的代码。

另一个经典用法是获取和处理Querystring参数。你不需要在回发时这样做。

Load(){ //Page load
if(!Page.IsPostBack){ // skips the execution of below lines during the next time.
 string m = DateTime.Now.ToString("MMMM");
        string y = DateTime.Now.Year.ToString();
        DropDownList1.Items.Add(m + " " + y);
        if (DateTime.Now.AddMonths(1).Month.ToString().Equals("January"))
            y = DateTime.Now.AddYears(1).Year.ToString();
        DropDownList1.Items.Add(DateTime.Now.AddMonths(1).ToString("MMMM") + " " + y);
        if (DateTime.Now.AddMonths(2).Month.ToString().Equals("January"))
            y = DateTime.Now.AddYears(1).Year.ToString();
        DropDownList1.Items.Add(DateTime.Now.AddMonths(2).ToString("MMMM") + " " + y);
        if (DateTime.Now.AddMonths(3).Month.ToString().Equals("January"))
            y = DateTime.Now.AddYears(1).Year.ToString();
        DropDownList1.Items.Add(DateTime.Now.AddMonths(3).ToString("MMMM") + " " + y);
        if (DateTime.Now.AddMonths(4).Month.ToString().Equals("January"))
            y = DateTime.Now.AddYears(1).Year.ToString();
        DropDownList1.Items.Add(DateTime.Now.AddMonths(4).ToString("MMMM") + " " + y);
        if (DateTime.Now.AddMonths(5).Month.ToString().Equals("January"))
            y = DateTime.Now.AddYears(1).Year.ToString();
        DropDownList1.Items.Add(DateTime.Now.AddMonths(5).ToString("MMMM") + " " + y);
         string date = DropDownList1.SelectedItem.Text;
        connect con = new connect(date);
        IList<connect.Session> records = con.getToken();
        GridView1.DataSource = records;
 }}