开发者

How can I load the Same ASP Grid View with different conditions?

I have a DDL and a ASP .net Grid view in my aspx page. I have two methods getALLProgram and getProgramBy name, both are working fine. My problem is: when the page is loaded for the first time, I want to call the getAllprogram method, after that if a User selects a program from DDL I want my getprogramByname method to be called.

How here is my code:

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindProgramDDL();
            BindGrid();
        }
        //BindProgramDDL();
    }

    protected void BindGrid()
    {
        string strProgramCode = DDLProgram.SelectedIndex.ToString();
        List<FormGridEntity> gridEntities = new List<FormGridEntity>();

        GridForResult.DataSource = gridEntities;
        GridForResult.DataBind();

        //throw new NotImplementedException();
    }

    protected void BindProgramDDL()
    {
        List<CcProgramEntity> programEntities = FormSaleSubmit_BAO.GetAllPrograms();

        DDLProgram.DataSource = programEntities;
        DDLProgram.DataTextField = "Shortname";
        DDLProgram.DataValueField = "Id";
        DDLProgram.DataBind();
        string programCode = programEntities[DDLProgram.SelectedIndex].Code;
    }

    protected void OnDDLProgramChanged(object sender, EventArgs e)
    {
        List<CcProgramEntity> programEntities = FormSaleSubmit_BAO.GetAllPrograms();
        string programCode = programEntities[DDLPro开发者_开发百科gram.SelectedIndex].Code;
    }

The Code is incomplete. i am still working on it. But I not getting the logic How will I make this happen that I have told you here. I hope I made my question clearly, if it confusing, please let me know what else I should provide here.


You should check in your BindGrid if any program has been selected or not and route the call as per that. For example,

protected void BindGrid()
{
   ...
   if (DDLProgram.SelectedIndex >= 0)
   {
      // program selected
      var programCode = DDLProgram.SelectedValue;
      data = GetProgramByName(programCode);
   }
   else
   {
      // get all programs
      data = GetAllPrograms();
   }

   // bind data with grid
}

You can either call BindGrid in page_load unconditionally (i.e. in post-back scenarios also) or invoke it on your DDL change.


how about writing getProgramByname on a selected index changed event of a drop down list and getALLProgram on page load event ?

I hope, I was clear on what your doubt and the above mentioned suggestion did helped.


Just change these 2 things

    protected void BindGrid()
    {
        List<FormGridEntity> gridEntities = (DDLProgram.SelectedIndex==-1)
          ?FormSaleSubmit_BAO.GetAllPrograms()
          :FormSaleSubmit_BAO.GetProgramByName(DDLProgram.SelectedValue);

        GridForResult.DataSource = gridEntities;
        GridForResult.DataBind();
    }

    protected void OnDDLProgramChanged(object sender, EventArgs e)
    {
        BindGrid();
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜