views:

182

answers:

2

I have an asp page (Default.aspx) that displays the diff between two text files. It contains two drop down lists (ID="File1" and "File2" respectively) and a button (ID="Submit").

It also contains a Literal control with ID "Result".

The contents of the list button are populated based on the files present in the physical application path.

Each time the submit button is clicked, I want to give the result of the diff/comparison in the Literal control, So I have an event handler like this:

protected void Submit_Click(object sender, EventArgs e)
{
    Result.Text = CompareFiles(File1.SelectedValue, File2.SelectedValue);
}

The problem with this is that you cannot bookmark the results of the comparison between specific files.

To solve this problem I appended the values of the files chosen for comparison as query string to the URL and performed a redirection. And therefore, the changed the above event handler to look like this:

protected void Submit_Click(object sender, EventArgs e)
        {
            string build1 = DropDownList1.SelectedValue;
            string build2 = DropDownList2.SelectedValue;

            string queryString = 
                String.Format("?file1={0}&file2={1}",HttpUtility.UrlEncode(build1),HttpUtility.UrlEncode(build2));

            string redirectionUrl = Request.Url.AbsolutePath + queryString;

            Response.Redirect(redirectionUrl, true);
        }

Accordingly called the "CompareFiles" method in the "Page_Load" event handler. So now, you can bookmark the results of the comparison and load it again directly because the URL would contain the files to compare.

But the problem is that "IsPostBack" property is false each time the redirection happens. Therefore, the values of the DropDownLists (files present in the physical application directory) gets evaluated each time.

I do not want this to happen if it is a redirection from the same page. How can I do this? I only want it to happen if the page is accessed for the first time or is accessed from some other page.

Is this a bad practice? I mean redirecting to the same page. I can avoid this problem by giving the results in a different page, But I want to do it this way, because I want the DropDownLists and the "Submit" buttons to always be there.

A: 

Promoted comment to answer:

Request.UrlReferrer
Jan Jongboom
I would modify your notes here, it is a new page request every time, he must load the drop down every time.
Mitchel Sellers
If the Page_Init event handler looks like the one given by you, then in the case a bookmarked page is open. Then only the results will be rendered and the rendering and evaluation of the dropdownlists will not be done.In any case, the Page_Init event handler and the Page_Load event handler get called each time the redirection happens. So no matter where I put the code, they will get executed every time I reload.The question boils down to how can I detect a redirection triggered from the same page.
Poulo
`Request.UrlReferrer` ?
Jan Jongboom
Yes. I managed to achieve what I wanted to with Request.UrlReferrer Thanks.
Poulo
+1  A: 

Since you are redirecting the to same page, it is NOT a postback, viewstate is lost, and it must re-bind the contents.

More than likely, just put the values for the DropDownLists into cache, then you can load from cache, and only hit the file system if not in cache.

Mitchel Sellers