【问题标题】:Repeating items in a dropdownlist on postback in .net在.net中回发的下拉列表中重复项目
【发布时间】:2014-01-21 09:43:48
【问题描述】:

我有一个包含 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();

任何帮助表示赞赏。

【问题讨论】:

  • 不回发时包装所有代码。 if(!Page.Ispostback){//Your code}
  • DropDownList1AutoPostBack="true" 吗?

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


【解决方案1】:

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

所以在Page_Load:

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

【讨论】:

    【解决方案2】:

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

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

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

    另一个经典用法是获取和处理查询字符串参数。您无需在回发时执行此操作。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-26
      • 1970-01-01
      • 2014-07-09
      • 1970-01-01
      相关资源
      最近更新 更多