尝试根据下拉列表中的选定日期从网格视图中获取记录计数,但不能

本文关键字:记录 获取 视图 网格 但不能 日期 下拉列表 | 更新日期: 2023-09-27 18:30:41

问题在于,在下拉列表中选择日期后,它将给出LAST选择的计数,而不是当前。我是新手,我唯一能想到的是某种类型的回发问题?网格视图使用DDL选择的记录填充良好,因此我无法理解为什么呈现的计数是以前的选择。

protected void ddlClassDate_SelectedIndexChanged(object sender, EventArgs e)
{
        lblRecordCounter.Text = "";
        SqlConnection conn;
        SqlCommand comm;
        SqlDataReader reader;
        string connectionString = ConfigurationManager.ConnectionStrings["gescdb"].ConnectionString;
        conn = new SqlConnection(connectionString);
        comm = new SqlCommand("SELECT (*) FROM gescdb" +
            "WHERE ClassDate=" + ddlClassDate.Text, conn);
        try
        {
            conn.Open();
            reader = comm.ExecuteReader();                
            GridRegistrants.DataSource = reader;
            GridRegistrants.DataBind();                
            reader.Close();
        }
        catch
        {
        }
        finally
        {              
            conn.Close();
            lblRecordCounter.Text = GridRegistrants.Rows.Count.ToString();
        }

尝试根据下拉列表中的选定日期从网格视图中获取记录计数,但不能

在你的Page_Load方法中,你也这样做,对吧?您必须将该代码移动到以下块中:

if (!IsPostBack){
    //Your code is here
}

您的下拉列表:

<asp:DropDownList ID="ddlClassDate" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlClassDate_SelectedIndexChanged" />

您的代码隐藏:

protected void Page_Load(object sender, EventArgs e){
    if (!IsPostBack){
        BindDDL();
        BindGrid(ddlClassDate.SelectedValue);
    }
}
protected void BindDDL(){
    //Bind Your dropdownlist here
}
protected void BindGrid(string ddate){
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["gescdb"].ConnectionString);
    SqlCommand comm = new SqlCommand("select * from gescdb where ClassDate = @date", conn);
    comm.Parameters.Add("@date", SqlDbType.VarChar).Value = ddate;
    try
    {
        conn.Open();
        SqlDataAdapter sda = new SqlDataAdapter(comm);
        DataSet ds = new DataSet();
        sda.Fill(ds);                
        GridRegistrants.DataSource = ds;
        GridRegistrants.DataBind();
    }
    catch
    {
        //...
    }
    finally
    {              
        conn.Close();
    }
}
protected void ddlClassDate_SelectedIndexChanged(object sender, EventArgs e){
    BindGrid(ddlClassDate.SelectedValue);
}