views:

309

answers:

0

I've created a objectadasource that returns a datatable, the gridview is binded to this datasource.

the ODS have parameters assigned on the Page_Load event, and the ODS returns the data ok and the gridview shows it fine.

the problem is

i have a textbox with a filter.

first I created a filterexpression in gridview, using the contents of the textbox, worked fine for me.

but now I've enabled the paging in gridview.

then the filterexpression is not useful now, since the ODS returns only the rows of the pagesize of gridview.

I did a new ODS method that select data from parameters page and pagesize according to GridView, and it's OK.

now my filter textbox passes his text property to a parameter of the ods select method, then the ods gets the data based on my filter and shows it in the grid.

on the Page_Load:

ObjectDataSource_Lista.SelectParameters["search"].DefaultValue = filter;
ObjectDataSource_Lista.SelectParameters["id"].DefaultValue = ID.ToString();

but when I change the value of the filter, the grid doesn't refresh. on debugging. I see that the ODS Select Method is refreshed ok, but the GridView don't. So I need to call mannually the Databind() method of the grid, to refresh data.

the problem is, I have a commandbutton on the grid, and if I manually databind(), the command button stops functioning, generating Page ValidateRequest errors.

My question is: how to databind() the grid automatically after the datasource refreshed?

ps: on the ODS Selected event, causes a infinite loop and the debug webserver crashes.

Temporary solution: Created a Variable

private bool wasdatabound;

on the event GridView_Databound, set wasdatabound = true;

on the Page_PreRenderComplete,

if ((GridView1.Visible) && (!databounded)) GridView1.DataBind();
private bool wasdatabound;

protected void Page_Load(object sender, EventArgs e)
{
    string filtro = "AND 'A' = 'A'";
    if (Page.IsPostBack)
    {

        if (txbNome.Text.Trim().Length > 0)
        {
            switch (comboFiltro.SelectedIndex)
            {
                case 0: filtro = string.Format("AND CL.NOME LIKE '%{0}%'", txbNome.Text); break;
                case 1: filtro = string.Format("AND CL.CPF = '{0}'", EngreneUtils.OnlyNumbers(txbNome.Text)); break;
                default: filtro = ""; break;
            }
        }

        GridView1.Visible = true;
    }
    else GridView1.Visible = false;

    ObjectDataSource_Lista.SelectParameters["pesquisa"].DefaultValue = filtro;
    ObjectDataSource_Lista.SelectParameters["idEmpresa"].DefaultValue = _dadosLogin.CodEmpresa.ToString();
    Form.DefaultButton = ImageButton_Buscar.UniqueID;
    Form.DefaultFocus = txbNome.ClientID;
}
protected void Page_PreRenderComplete(object sender, EventArgs e)
{
    if ((GridView1.Visible) && (!wasdatabound))
        GridView1.DataBind();
}


protected void GridView1_DataBound(object sender, EventArgs e)
{
    wasdatabound = true;
}