asp.netvb.netcachingwebforms

How to stop caching for a ASPX.net/VB webforms website page


I have an internal website that we use for device status lookups. And on this webpage we start at a search screen that looks like the following

enter image description here

Once a user searches by a type it pulls info that looks like the following

" Address: Some address Serv Loc: Some Srvloc Device: the device name Upstream Device: Some upstream device "

Since we are pulling from several separate databases there are cases where a given device is missing information like the upstream device, or srvloc. And in those cases when a user had looked up a device like "AP4444" that has full info on the upstream device, but then searches "GT654" that is missing information on the upstream device. The page instead of displaying blank information, displays the AP4444 upstream device information.

My personal dream is that it displays blank when there is no info like a normal website. So far I have attempted quite a few fixes but none have done anything.

Steps I have already taken to try and fix it:

1) Adding the following code to the web.config

<location path="File_IN_QUESTION.aspx"> <system.webServer> 
<caching enabled="false" enableKernelCache="false" /> 
 </system.webServer> </location>

This did not seem to do anything. But maybe I'm adding the code in the wrong spot in the web.config file?

2) Adding the following directive line to the aspx writeup file <%@ OutputCache Duration="1" VaryByParam="productId" %> This one I could not get to run without errors I think because either it needs to be implemented into the existing directive line above, or OutputCache needs to be defined somewhere, I'm not sure.

3) Adding in the following code to the PageLoad sub so it

gets activated every time we do a search.  
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load 
Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache) Response.CacheControl = "No-cache" ... 

This also did not seem to do anything.

4) Adding in the following code to the Page_Load sub so it gets activated every time we do a search.

Response.ClearHeaders() Response.AppendHeader("Cache-Control", "no-cache, no-store, must-revalidate")
 Response.AppendHeader("Pragma", "no-cache")
 Response.AppendHeader("Expires", "0")

I added this in the same spot as number 3 in the Page_Load and it also did nothing.

5) Adding in the following code to the Page_Load sub so it gets activated every time we do a search.

Response.Cache.SetExpires(DateTime.Now.AddSeconds(-1)) 
Response.Cache.SetCacheability(HttpCacheability.NoCache) 
Response.Cache.SetNoStore() 
Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches)

This also did not seem to do anything.

6) Added the following code to the search button click protected sub. HttpResponse.RemoveOutputCacheItem("/File_IN_QUESTION.aspx")

This also did not do anything. With all but one the code runs, but just nothing happens. I'm not sure if there are settings somewhere in Visual Studio where there is a cache setting I can disable. I would be totally ok with disabling the cache for all the webpages

The issue is present on all browsers btw


Solution

  • As noted in the comments, when you pull data from a database, say a local text file, or whatever?

    Then of course if that pulling of data fails, or the requested data does not exist?

    Then of course your code can have to detect if such a request for data does not exist, and if the data does not, then you simply in that SAME code then correctly update your controls on the form to reflect that fact.

    Say I have a simple text box where the user enters the id of some hotel.

    If that id does not exist, then I STILL HAVE to update the controls on the web page to reflect that fact.

    Say this markup:

            <h3>Enter hotel ID</h3>
            <asp:TextBox ID="txtID" runat="server"></asp:TextBox>
            <asp:Button ID="cmdLoadHotel" runat="server" 
                Text="Get Hotel"
                OnClick="cmdLoadHotel_Click"
                style="margin-left:20px"
                />
    
            <br />
            <h3>Hotel infomration</h3>
    
            Hotel name:<br />
            <asp:TextBox ID="txtHotel" runat="server"></asp:TextBox>
            <br />
            <br />
            City:<br />
            <asp:TextBox ID="txtCity" runat="server"></asp:TextBox>
            <br />
            <br />
    
            Description: <br />
            <asp:TextBox ID="txtDes" runat="server"
                TextMode="MultiLine" Height="110px" Width="375px"
                ></asp:TextBox>
    

    And now code behind:

    Protected Sub cmdLoadHotel_Click(sender As Object, e As EventArgs)
    
        If txtID.Text = "" Then
            ' non search, empty controls and exit
            txtHotel.Text = ""
            txtCity.Text = ""
            txtDes.Text = ""
            Return ' exit, don't run search code
        End If
    
        ' value in ID text box, search for hotel
        Dim strSQL As String =
                "SELECT * FROM tblHotelsA WHERE ID = @ID"
    
        Dim cmdSQL As New SqlCommand(strSQL)
        cmdSQL.Parameters.Add("@ID", SqlDbType.Int).Value = txtID.Text
    
        Dim dtHotel As New DataTable
        dtHotel = MyRstP(cmdSQL)
    
        If dtHotel.Rows.Count > 0 Then
            ' we have a hotel, load and update controls.
    
            With dtHotel.Rows(0)
                txtHotel.Text = .Item("HotelName")
                txtCity.Text = .Item("City")
                txtDes.Text = .Item("Description")
            End With
        Else
            ' data not found, blank out controls
            txtHotel.Text = ""
            txtCity.Text = ""
            txtDes.Text = ""
    
        End If
    
    End Sub
    

    So, now if we enter a hotel ID that does not exist, then we STILL MUST clear out the controls from the prevous search. Such controls thus under your code simply has to clear out the prevous values.

    Above thus looks like this:

    enter image description here

    So, in above, the text box controls had values from the previous search. However, we then enter a hotel ID value that does not exist (hence, like YOUR example, no data is returned, but we STILL HAVE to clear out the controls based on failing to retrieve no data, and hence the code has to clear out the old values in the controls. If our code does not check for no data, then the old previous values of the controls on the web page will remain in view, and hence it is a simple matter to clear out the controls based on no data being returned. Your code is to do the same, and when no data is returned, then the code has to clear out the older values in the controls on that web page.