Question

In my current design approach I am running into a case where I'm having to perform an extra Database read and Databind on controls, only to overwrite them later on in the page lifecycle. The end result to the user is the same, but behind the scenes I'm doing two unnecessary and costly operations.

The search button is on the Master page. All the display controls (gridviews/tables/labels/etc) are on the content pages. The database call and databinding must be done in the Page_Init() method (most of the controls being used must be bound here).

When the user searches for an item, I save his search input in the session to persist it as being actively viewed across all the other content pages. So that when any content page is initialized, I check to see if they are actively viewing an item, and if so display his details.

// Master Page
protected void BtnSearch_OnClick(object sender, EventArgs e)
{
   MySession.Current.ItemName = TxtItem.Text.Trim();
   Server.Transfer("~/default.aspx");
}

// Content Page
protected void Page_Init(object sender, EventArgs e)
{
   // If they're actively viewing an item, display its info
   bool HasActiveItem = string.IsNullOrEmpty(MySession.Current.ItemName) ? false : true;
   if (HasActiveItem)
   {
      // Makes one DB call to get all info; 
      // Binds all that info to GridViews/tables/labels on the page
      BindAllDataControls(MySession.Current.ItemName);

      // Display
      DisplayItemDetails();
   }
}

Here's the Issue: Say the user is currently viewing an Item = Boots. This value is saved in the session. Now the user searches for Item = Shirts and clicks the search button.

When the content page loads, he checks if there's anything in the session, there is but it's Item = Boots. It performs the unnecessary database/databind calls. Then, the BtnSearch click event triggers, we load the new Item = Shirts value into the session, and start the life cycle over. This lifecycle is good to go.

How can I get rid of that extra processing? Is the approach wrong?

I've thought about performing Page.IsPostback() and Page.IsCallback() logic during the content page initialization, but there are multiple other controls that cause postbacks and callbacks in the real web application. Therefore, I don't think I can get enough info from this to make the determination to skip or not. I've thought about wrapping the entire part of the page that contains all the GridViews/tables/labels/etc. in an ajax Callback, but I don't think that's a good approach. I've thought about sending an ajax call back to the server during the BtnSearch click that sets a flag. Then during load, we read that flag to skip or not, but there's no guarentee that that ajax call will process before the search BtnClick event.

Any ideas? Or should I just eat these extra calls and be done with it? There's got to be another way.

Was it helpful?

Solution

The most simple solution I see here is to make the check of search text change direct on PageInit and not on the button call.

// Master Page
protected void BtnSearch_OnClick(object sender, EventArgs e)
{
   // remove that lines
   // MySession.Current.ItemName = TxtItem.Text.Trim();
   // Server.Transfer("~/default.aspx");
}

// Content Page
protected void Page_Init(object sender, EventArgs e)
{
   // direct add here your variable on session
   var vSearchText = TxtItem.Text.Trim();

   if(!string.IsNullOrEmpty(vSearchText))
       MySession.Current.ItemName = vSearchText ;

   // ------------ rest of your code ---------------    
   // If they're actively viewing an item, display its info
   bool HasActiveItem = string.IsNullOrEmpty(MySession.Current.ItemName) ? false : true;
   if (HasActiveItem)
   {
      // Makes one DB call to get all info; 
      // Binds all that info to GridViews/tables/labels on the page
      BindAllDataControls(MySession.Current.ItemName);

      // Display
      DisplayItemDetails();
   }
}

Now to tell you the truth, the Server.Transfer is not good idea, what I do is that I use parameter in the URL, to include the search string from the input of the user, so when the user is add something for search I create the url as:

http://www.mydomain.com?q=test

and then I read the q and use it to fill the search box and make the search. This way you also have a SEO friendly search, the user can save his search and you avoid the server transfer that have other issues.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top