AutoPostBack的下拉列表未加载最近更改的值

时间:2014-09-05 10:11:13

标签: c# asp.net-2.0

下面是我的dropdownlist(drpdwnshowitems)的Page_Load和SelectedIndexChanged事件的代码,其中我从数据库中绑定了数据,并且AutoPostBack为true,因为我需要这样做。我将最近选择的值(项目)存储到名为“NoOfShowItem”的cookie中,并且我在页面加载中请求该值并且想要使用但是只提供前一个值。要看到我还在javascript消息框中显示了同样值。在逐步执行后,我看到在加载(Postback)页面后,selectedIndexChanged事件被触发。我搜索了这么多,但我找不到确切的解决方案。请帮助我。在此先感谢。

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI; 
using System.Web.UI.WebControls;
using System.Data;

public partial class Client_Category : System.Web.UI.Page
{

float countshowitem;
protected void Page_Load(object sender, EventArgs e)
{
//to see recently selected value
Response.Write("<script language=\"javascript\">alert('"+
drpdwnshowitems.SelectedValue + "')</script>");
if (!IsPostBack)
    {
        DataTable dtno = ShowItemsFunctions.Select_All_Numbers();
        DataRow dr = dtno.NewRow();
        dr["NoOfProduct"] = "--Select One--";
        dtno.Rows.InsertAt(dr, 0);
        drpdwnshowitems.DataSource = dtno;
        drpdwnshowitems.DataTextField = "NoOfProduct";
        drpdwnshowitems.DataValueField = "NoOfProduct";
        drpdwnshowitems.DataBind();

    }

    try
   {
     countshowitem = float.Parse(Request.Cookies["NoOfShowItem"].Value.ToString());
     drpdwnshowitems.SelectedValue = countshowitem.ToString();

    }
    catch (Exception ex)
    {
       countshowitem = 9;
       drpdwnshowitems.SelectedValue = countshowitem.ToString();
    }
}

protected void drpdwnshowitems_SelectedIndexChanged(object sender, EventArgs e)
{
    countshowitem = int.Parse(drpdwnshowitems.SelectedValue.ToString());
    Response.Cookies["NoOfShowItem"].Value = drpdwnshowitems.SelectedValue.ToString();
    Response.Cookies["NoOfShowItem"].Expires = System.DateTime.Now.AddHours(2);
}
}

1 个答案:

答案 0 :(得分:0)

我不确定你可以在Page_Load的末尾动态设置SelectedIndexChanged属性吗?

drpdwnshowitems.SelectedIndexChanged += drpdwnshowitems_SelectedIndexChanged;

由于属性是在SelectedValue之后设置的,因此第一次事件可能不会触发......