下拉列表的AutoPostBack没有加载最近更改的值
本文关键字:最近 AutoPostBack 下拉列表 加载 | 更新日期: 2023-09-27 18:20:55
下面是我的下拉列表(drpdwnshowitems)的Page_Load和SelectedIndexChanged事件的代码,在该事件中,我从数据库绑定了数据,并且有AutoPostBack true,因为我需要这样做。我将最近选择的值(item)存储到名为"NoOfShowItem"的cookie中,我在页面加载中请求该值并希望使用,但它只提供以前的值。为了看到这一点,我还在javascript消息框中显示了同样的值。在一步一步执行之后,我看到selectedIndexChanged事件在加载(Postback)页面后被触发。我找了很多,但找不到确切的解决方案。所以请帮帮我。提前感谢。
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);
}
}
我不确定,但您可以在Page_Load的末尾动态设置SelectedIndexChanged属性吗?
drpdwnshowitems.SelectedIndexChanged += drpdwnshowitems_SelectedIndexChanged;
由于属性设置在SelectedValue之后,因此事件可能不会第一次激发。。。