【问题标题】:Data not loading on pageload with postback使用回发的页面加载时数据未加载
【发布时间】:2017-11-09 08:41:40
【问题描述】:

我遇到了问题。页面加载后数据不会加载。我必须在下拉列表中选择一个项目才能加载。甚至在我选择下拉列表中的任何项目之前,我都需要加载它。

这是我的代码。

 protected void ddlPeriodStamp_SelectedIndexChanged(object sender, System.EventArgs e)
{
    string selectedGroup = string.Empty;
    DropDownList ddlItemGroup = (DropDownList)sender;

    if (ddlItemGroup.SelectedValue != null)

        TreatmentGroup = ddlItemGroup.SelectedValue;
        ApplyGridFilter(ddlItemGroup.SelectedValue);


}

protected void ApplyGridFilter(string TreatmentGroup)
{
    string selectedGroup = string.Empty;
    DBDataSource1.State.BusinessObject.DataPump.FormFilters.Clear();
    DBDataSource1.State.BusinessObject.DataPump.FormFilters.Add("TreatmentGroup", TreatmentGroup);
    DBDataSource1.State.BusinessObject.Fill(null);
    MedicalSchemeDetailGrid.DataBind();
}
protected void Page_LoadComplete(object sender, System.EventArgs e)
{

    if (!this.IsPostBack)
            ApplyGridFilter(string.Empty);
}

【问题讨论】:

    标签: c#


    【解决方案1】:

    ispostBack 为假时调用ApplyGridFilter(string.Empty);,当ispostBack 为真时调用ApplyGridFilter(ddlItemGroup.SelectedValue);

    protected void Page_LoadComplete(object sender, System.EventArgs e)
     {
    
       if (this.IsPostBack)
       {
         ApplyGridFilter(ddlItemGroup.SelectedValue);// it will hit on first time page load 
       }
       else
       {
         ApplyGridFilter(string.Empty);// it will hit while you change the dropdown items, But you should set true for **IsAutoPostBack** property on dropsownlist.    
       }
     }
    

    【讨论】:

    • 仍然无法正常工作..我已将自动回发设置为 true
    【解决方案2】:

    您需要在 Page_Load 事件中填写数据,而不是在 Page_LoadComplete 事件中。根据 MSDN:

    LoadComplete 事件发生在所有回发数据和视图状态之后 数据被加载到页面中并且在 OnLoad 方法之后 调用页面上的所有控件。

    protected void Page_Load(object sender, System.EventArgs e)
    {
        if (!this.IsPostBack)
            ApplyGridFilter(string.Empty);
    }
    

    【讨论】:

    • 我已将其添加到页面加载事件中,但页面加载时仍然没有加载数据
    • 在Page_Load中编写代码后,您是否通过放置断点检查了ApplyGridFilter是否被调用?如果它被调用,您是否检查过从 DB 返回的数据?
    • 它没有被调用
    猜你喜欢
    • 1970-01-01
    • 2018-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-10
    • 1970-01-01
    相关资源
    最近更新 更多