Domanda

I'm having issues with an ASP.NET GridView.

The GridView is set up to retrieve rows from a stored procedure via a SqlDataSource. The Gridview has a BoundField which fills in a DateTime parameter for the stored procedure from an ASP.NET Textbox on the form.

When I enter an invalid DateTime into the ASP.NET Textbox and then click any column of the GridView to sort it, the GridView throws a FormatException in its PreRender event: "invalid character at position 0"

How can I intercept or stop a user from sorting the GridView while the TextBox has an invalid DateTime in it?

I've already tried form validators but it looks like those don't take GridView clicks into account. One thought I had was to perform the data binding and sorting manually so that I can stop a bad sort in progress. Any thoughts?

È stato utile?

Soluzione

I solved this by extending the Textbox.OnTextChanged event like so:

protected void tbTo_TextChanged(object sender, EventArgs e)
{
    DateTime temp;

    if(DateTime.TryParse(tbTo.Text,out temp)==false)
    {
        tbTo.Text = "";
    }
}

This fires before the GridView sorts! Problem solved.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top