call today Call Today 320.281.0605

Search Results – Webpage has expired on back button

December 8, 2009

search

I’ve been asked this a few times now so I thought I would blog my solution to point others to this next time I am asked.

Often times people will create a simple search on their site. The most obvious way to do this is with a simple textbox and button.  You then handle the search when the button is clicked in the code-behind.

It may look something like this:

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        if (!String.IsNullOrEmpty(txtSearch.Text.Trim()))
        {
            BindSearch(txtSearch.Text.Trim());
        }
    }

Pretty straight forward.  The problem here is you’re doing a postback to the same page.  You won’t have a history of the previous searches in your browser. After doing a search and you want to see what you had for results from the previous search and simply hit back in your browser you’ll see this

expired

So what I have always done to avoid this is to simply do a Response.Redirect to the page instead of just calling the BindSearch() method and put the search term in the query string.

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        if (!String.IsNullOrEmpty(txtSearch.Text.Trim()))
        {
            Response.Redirect("/Search.aspx?search=" + txtSearch.Text.Trim());
        }
    }

This is a pretty simple solution, does anyone else have a different way to handle this?

  • Share/Bookmark

Controls not being registered in designer.cs

August 3, 2009

I’ve encountered this a few times now.

When dealing with a webform with an extraordinary amount of controls, ie. textboxes, dropdownlists, panels, etc. I’ve noticed that after x amount of controls have been added they are no longer registered in the designer.cs file. 

I notice this by first the control not showing up in intellisense when trying to use it in code-behind.  If I manually type it out I will still get a build error. 

So, at this point I have to manually register any new controls in the designer.cs file.  At the top of the file it says,

//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.

However it seems that they are never lost and now everything works fine.  I am not sure if this is normal behavior and this is what you are suppose to do, but so far it has worked for me.

Has anyone else seen this, does anyone know the exact amount of controls that you can add to a page before they are no longer registered in the designer.cs?

  • Share/Bookmark

Linq to Sql- AutoID not working

July 31, 2009

Using Linq to Sql Auto IDs do not work by default. 

Lets say you have a table “Property” with a primary key of “PropertyId”.  In the table definition it is set as a unqueidentifier and by default it is assigned a NewID().

Now go to your Linq to Sql and create your dbml, drag your Property table into it.

In order to have the PropertyId generated using the Default Value go  to the properties of the PropertyId field and set Auto Generate Value to True and Auto-Sync to OnInsert.

I am sure many already know this, but I hope it can help save some time for others. 

  • Share/Bookmark

asp.net dynamic meta and title tags

July 1, 2009

Using a Master page.

Problem for SEO is it really scews up the formatting and positioning of the tags.

First get the data there.

If you’re not overly concerned about formatting of the title tag, in the content page:

Page.Title = “The title of your page”;

// Add the keywords meta tag
HtmlMeta metaKeywords = new HtmlMeta();
metaKeywords.Name = "keywords";
metaKeywords.Content = nseCase.MetaKeywords;
Page.Header.Controls.AddAt(1, metaKeywords);
Page.Header.Controls.AddAt(1, new LiteralControl("\n"));

// Add the description meta tag
HtmlMeta metaDescription = new HtmlMeta();
metaDescription.Name = "description";
metaDescription.Content = nseCase.Description;               
Page.Header.Controls.AddAt(1,metaDescription);
Page.Header.Controls.AddAt(1, new LiteralControl("\n"));

What is happening is the meta tags are being added to the first line in the head tag (below the title tag). Think of it like a stack, that is why it is being done in reverse order in which you would think.

If you want to do a little more work to remove the spacing and line breaks from the title tag you need to take this approach.

In the Master page remove the runat=”server” from the head tag. Now you can create literal tags for each desired tag in the <head> tag.

<head>
<asp:Literal ID=”litTitle” runat=”server” />
<asp:Literal ID=”litMetaDescription” runat=”server” />
<asp:Literal ID=”litMetaKeywords” runat=”server” />
</head>

Now in your content page you can access these literal controls by:

Literal title = (Literal)Master.FindControl("litTitle");
title.Text = String.Format("<title>{0}</title>", “Your dynamic title”);

Literal metaDescription = (Literal)Master.FindControl(“litMetaDescription”);
metaDescription.Text = String.format(“<meta name=\”{0}\” content=\”{1}\” />”, “description”, “here is your page description”);

Literal metaKeywords = (Literal)Master.FindControl(“litMetaKeywords”);
metaKeywords.Text = String.format(“<meta name=\”{0}\” content=\”{1}\” />”, “keywords”, “here, are, your, pages, keywords”);

The first approach is quicker to do and still allows you to put a default title in it. You may not want to set the title and meta tags for each page.

The second approach requires that you set the title and meta tags on every page if you want them. I suppose you could use a contentplaceholder for many of the pages which is quicker than doing it in the code-behind for pages that you don’t need the tags to be dynamic.

My SEO guy is finally happy that I came up with a solution for this.  I hope this helps you get an SEO guy off your back. :)

  • Share/Bookmark

DropDownList value lost on PostBack

June 10, 2009

I had two dropdownlists. One that was hard-coded in the .aspx page and one that was being dynamically populated in the code-behind.  I had a requiredfieldvalidator on both.. 

When I submit it would get past the client side validation.  Server side validation
If(Page.IsValid) was failing.

It would come back to the page saying that I needed to choose a value for my dynamically populated ddl.

For the life of me I couldn’t figure it out.

Finally I noticed at the top in the page directive:

EnableViewState="false"

I set this to true and it worked.

Conclusion: if you’re finding values aren’t making it to the code-behind check your ViewState.

I hope this saves someone else the time I lost debugging it.

  • Share/Bookmark
Agent Cody Banks 2 Destination London full lenght movie download Afghan Knights download movie Inside the Smiths download movie Beverly Hills Chihuahua download movie Permanent Vacation download movie Indiana Jones and the Kingdom of the Crystal Skull download movie Afghan Knights download movie Inside the Smiths download movie Beverly Hills Chihuahua download movie Permanent Vacation download movie Indiana Jones and the Kingdom of the Crystal Skull download movie