0

I have a Telerik RadGrid within a .ascx (user control) that requires a *_NeedDataSource method to populate the grid's DataSource.

The same user control is doing some user authentication, simply checking HttpContext.Current.User.Identity.IsAuthenticated and doing a Response.Redirect(url, false) if it is false.

I'm seeing exceptions being raised on our live system due to the *_NeedDataSource method being called when the user is not logged in, this seems to be possible because I pass false as the value of the endResponse parameter to Response.Redirect (which allows all subsequent page events to execute). The original author coded it this way as I assume he was going on the guidance of the community:

  1. Using endResponse in a Response.Redirect
  2. https://blogs.msdn.microsoft.com/tmarq/2009/06/25/correct-use-of-system-web-httpresponse-redirect/

I've been trying to work out a way that I can reliably prevent execution of *_NeedDataSource without reverting to an endResponse value of true.

I can think of some ways which involve flag setting and checking but that seems inelegant and subsequent developers could forget to implement the same pattern. I've also tried implementing a base control type to derive from which overrides OnPreRender (or whatever), similar to the response from Alexander Manekovskiy to this question: ASP.NET Redirect and End Page but I cannot work out the method to override that will prevent execution of *_NeedDataSource.

Can anyone recommend an approach that would work in this situation and wouldn't require too much cognitive overhead?

Community
  • 1
  • 1
A. Murray
  • 2,761
  • 5
  • 27
  • 40
  • It seems like [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). What is the underlying Business Logic of not wanting to trigger ***NeedDataSource*** event? Easiest way is you could make ***RadGrid.Visible=false*** or you can put logic inside ***NeedDataSource***. – Win Jan 05 '17 at 14:17
  • As stated in the question, I don't want NeedDataSource to execute because when the user is not logged in it causes exceptions. – A. Murray Jan 05 '17 at 14:45
  • As I said, you can check the business logic inside ***NeedDataSource***, and ***return;*** if user is not authenticated. There are so many ways you can handle the issue based on the need, but EndResopnse is nothing to do with NeedDataSource. – Win Jan 05 '17 at 14:51

1 Answers1

1

In the code which calls/accesses *_NeedDataSource, check the status code of the current Response. If the status code is 302, then a Response.Redirect() has been called earlier in the request handling:

if (Response.StatusCode != 302)
{
    // do something here
}
user1429080
  • 9,086
  • 4
  • 31
  • 54
  • I could implement that solution but I don't know what method I need to override to conditionally prevent execution of `*_NeedDataSource`. If I could work that out I would implement Alexander Manekovskiy's answer to this question: http://stackoverflow.com/questions/25473110/asp-net-redirect-and-end-page – A. Murray Jan 05 '17 at 11:58
  • I see.. Not sure if it will work, since I'm not really familiar with `Telerik` controls, but one thing you could try is to set the `RadGrid` control´s `Enabled` property to false right before redirecting. That *might* cause it to stop processing it's events. On the other hand that is not much different from flag setting that you already mentioned... – user1429080 Jan 05 '17 at 12:27