0

I am using ASP.Net 4.5 , and facing an issue regarding with data binding for grid view using 'Select method'.

The grid is able to bind the data on custom paging too.

The issue lies in filtering the data. I have a asp button, By clicking on this i am able to get filtered data using stored proc. but this time grid is not able to call the method which i mentioned in aspx page for that grid in SelectMethod.

<asp:GridView ID="gEmployee" runat="server"

   AutoGenerateColumns="False"

   OnRowCommand="gEmployee_Command" CellPadding="4"

   OnRowDataBound="gEmployee_RowDataBound"

   AllowCustomPaging="true" PageSize="10" PagerSettings-Visible="false"

   CssClass="mGrid"

   AllowSorting ="true"

   AlternatingRowStyle-CssClass="alt"

ItemType="ModelEntityLayer.ClsEmployee"  SelectMethod="BindGrid" 

   OnSorting ="gEmployee_OnSorting"  HeaderStyle-Wrap="false" DataKeyNames="PropClockNumber" >

code above shows the properties which i have set for grid view.

public IQueryable<ClsEmployee> BindGrid()


{

    int intTotalPages = 0;

    objBALEmployee = new ClsEmployeeBAL();

    List<ClsEmployee>  employee = null;

    if ((string)Session["QueryResult"] != null)

    {

        employee = objBALEmployee.GetPageWiseData(gEmployee.PageIndex, gEmployee.PageSize, ViewState["SortExpression"] != null ? ViewState["SortExpression"].ToString() : ViewState["ClockNumber"].ToString(),  ViewState["SortOrder"] != null ? ViewState["SortOrder"].ToString() : "asc", ref intTotalPages, ((string)Session["QueryResult"]));

        Session.Remove("QueryResult");

    }

    else

    {

        if (Convert.ToInt32(ViewState["IsEditSearch"]) != 1)

        {

            employee = objBALEmployee.GetPageWiseData(gEmployee.PageIndex, ViewState["SortExpression"] != null ? ViewState["SortExpression"].ToString() : ViewState["ClockNumber"].ToString(),  ViewState["SortOrder"] != null ? ViewState["SortOrder"].ToString() : "asc", gEmployee.PageSize, ref intTotalPages);

        }

        else

        {

            employee = objBALEmployee.GetPageWiseData(gEmployee.PageIndex, gEmployee.PageSize, ViewState["SortExpression"] != null ? ViewState["SortExpression"].ToString() : ViewState["ClockNumber"].ToString(),  ViewState["SortOrder"] != null ? ViewState["SortOrder"].ToString() : "asc", ref intTotalPages, dlEmployee.SelectedValue+" = '"+txtSearch.Text.Trim()+"'");

        }

    }

    var lstEmployee = employee.AsQueryable();

    if (intTotalPages > 0)

    {

        CustomPager.TotalPages =  intTotalPages %gEmployee.PageSize == 0 ? intTotalPages /gEmployee.PageSize : intTotalPages / gEmployee.PageSize + 1;

        CustomPager.Visible = true;

        lblNoRecordsFound.Visible = false;

        NoRecords.Visible = false;

    }

    else

    {

        CustomPager.Visible = false;

        lblNoRecordsFound.Visible = true;

        NoRecords.Visible = true;

    }

    ExportData1.DisableBtn(gEmployee);

    ExportData.ExportCheck = false;

    return lstEmployee;

}

The above code shows the bindGrid method which i have set for the selectMethod in grid.

protected void btnGo_Click(object sender, EventArgs e)

{

    ViewState["IsEditSearch"] = "1";

    hiddenSearchField.Value = dlEmployee.SelectedValue;

    hiddenSearchValue.Value = txtSearch.Text.Trim();

    gEmployee.SelectMethod = "BindGrid";

}

The above code is written for button click.

Though i have written gEmployee.SelectMethod = "BindGrid"; once again on click code, BindGrid is not getting fired. So i am not able to bind my newly filtered data to grid.

Please guide me on this.

Thanks.

HemChe
  • 2,249
  • 1
  • 21
  • 33
user2206336
  • 29
  • 1
  • 4

1 Answers1

2

I think you'll need to add a line at the end of btnGo_Click that explictly calls DataBind() on the grid:

protected void btnGo_Click(object sender, EventArgs e)
{
    ViewState["IsEditSearch"] = "1";
    hiddenSearchField.Value = dlEmployee.SelectedValue;
    hiddenSearchValue.Value = txtSearch.Text.Trim();
    gEmployee.SelectMethod = "BindGrid";

    gEmployee.DataBind();
}
Merenzo
  • 5,326
  • 4
  • 31
  • 46
  • Ya! It works ! Thanks a lot. But one more question can you just give me a clue while page is getting post back why it doesn't calling Bind grid method automatically beacause excluding this event every other time even i don't sequred gEmployee.SelectMethod = "BindGrid"; too. thats why i am wondering about it why this time i need to make explicit call for it ? but thanks once again ! :) – user2206336 Mar 26 '13 at 06:03
  • Glad I could help. :) If this has answered your original question, you can [accept it as the answer](http://meta.stackexchange.com/questions/5234/). Your "why" question is a good candidate for a separate question :) ...but to attempt an answer: I'm not surprised that an explicit call is required, but I **am** surprised that an explicit call wasn't required in your other scenarios. – Merenzo Mar 26 '13 at 06:41