locked
Combo Box OnChange and Captcha Refresh RRS feed

  • Question

  • User-1766242239 posted

    I am having an issue that I'm not sure how to resolve.  I have a user registration page that has a combo box for the type of user account they are signing up for.  There is also a captcha at the bottom of the form.  Here is the problem, if the original captcha loads as abc123 and then the user requests that the captcha be refreshed to a different value so say now its zzz321.  Then the user changes the value in the combo box for the user type.  At that point the captcha at the bottom changes back to abc123 (the original captcha that was on the page).  It is probably important to note how the captcha is refreshed and the combo box OnChange is addressed.  The captcha is initially loaded through an asp.net c# handler file and the value is stored in a session variable.  The refresh of the captcha is done by clicking on an asp:Image that has an OnClick call to a javascript function that requests the image.src to be updated through the handler file.  I had been having trouble getting that to work and found a suggestion online to add a "NavigateUrl='#tag'" to the asp:Image tag and that solved the problem to get that working.  I don't know if that might have something to do with it as maybe the OnClick event is refreshing the page and then causing the image.src to be reverted back to the original captcha value.  

    As for the combo box, the source data comes from the database.  There is an OnSelectedIndexChanged field in the asp:DropDownList tag that checks to see the value of the selected user type and displays a message for certain user types.  Upon changing the value of the combo box is when I encounter the issue.  

    Here is some of the code:

    Captcha Form Code:

    <div class="nonMain-div" style="width:260px; float:left;" id="backHome">
    <img src="Handler.ashx" id="imgCaptcha" />
    </div>
    <div class="nonMain-div" style="width:640px; float:left;" id="Div1">
    <asp:Image ID="refreshImg" ImageUrl="Images/refresh.jpg" runat="server" Text="Refresh" ImageAlign="AbsMiddle" OnClick="javascript:Captcha();" NavigateUrl="#backHome" />
    </div>

    Javascript Captcha():

    <script type="text/javascript">
    function Captcha() {
    var newImg = document.getElementById("imgCaptcha");
    newImg.src = "Handler.ashx?captcha=" + Math.random();
    }

    </script>

    Combo Box Code:

    <asp:DropDownList ID="userTypes" OnSelectedIndexChanged="userTypes_SelectedIndexChanged" AutoPostBack="true" runat="server" AppendDataBoundItems="True" DataTextField="Types" DataValueField="TypeID" DataSourceID="myDataSource" BorderColor="#6e6e70" BorderWidth="1px" BorderStyle="Groove">
    <asp:ListItem Value="0">Please Select</asp:ListItem>
    </asp:DropDownList>

    OnSelectedIndexChanged Code:

    protected void userTypes_SelectedIndexChanged(object sender, EventArgs e)
    {
    if (Convert.ToInt32(userTypes.SelectedValue) == NumberValue)
    {
    userChanged.Visible = true;
    }
    else
    {
    userChanged.Visible = false;
    }
    }

    Any help would be greatly appreciated!

    Friday, July 18, 2014 6:28 PM

Answers

  • User-417640953 posted

    Hi RonnieF,

    Thanks for your post.

     

    Here is the problem, if the original captcha loads as "abc123" and then the user requests that the captcha be refreshed to a different value so say now its "zzz321".

    From your description, I see page will do a postback when you change the selected item in the comboBox.

    Then the img tag will request the url "Handler.ashx" again.

    <div class="nonMain-div" style="width:260px; float:left;" id="backHome">
    <img src="Handler.ashx" id="imgCaptcha" />
    </div>

    So I think what cause your img tag show the old Captcha is the browser cache, please disable browser cache when in the

    OnSelectedIndexChanged Code:

    // Stop Caching in IE
    Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
    
    // Stop Caching in Firefox
    Response.Cache.SetNoStore();

    http://stackoverflow.com/questions/914027/disabling-browser-caching-for-all-browsers-from-asp-net

    And similar issue you can refer to below.

    http://stackoverflow.com/questions/728616/disable-cache-for-some-images

    Hope this helps, thanks.

    Best Regards!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 23, 2014 11:03 PM

All replies

  • User-417640953 posted

    Hi RonnieF,

    Thanks for your post.

     

    Here is the problem, if the original captcha loads as "abc123" and then the user requests that the captcha be refreshed to a different value so say now its "zzz321".

    From your description, I see page will do a postback when you change the selected item in the comboBox.

    Then the img tag will request the url "Handler.ashx" again.

    <div class="nonMain-div" style="width:260px; float:left;" id="backHome">
    <img src="Handler.ashx" id="imgCaptcha" />
    </div>

    So I think what cause your img tag show the old Captcha is the browser cache, please disable browser cache when in the

    OnSelectedIndexChanged Code:

    // Stop Caching in IE
    Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
    
    // Stop Caching in Firefox
    Response.Cache.SetNoStore();

    http://stackoverflow.com/questions/914027/disabling-browser-caching-for-all-browsers-from-asp-net

    And similar issue you can refer to below.

    http://stackoverflow.com/questions/728616/disable-cache-for-some-images

    Hope this helps, thanks.

    Best Regards!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 23, 2014 11:03 PM
  • User-1766242239 posted

    Sorry for the delay, the email didn't come through that you replied.  Maybe I'm doing something wrong, but I can't get that to work either.  It is still doing the same thing.  I have used classic ASP for many years and I am new to ASP.NET so I'm not sure if this is accurate or not.  Someone told me that ASP.NET stores the session variables in an encrypted string on the page that is rendered on the users browser.  Their thought was that when I am updating the session variable in the code behind, it is still stored in the user's rendered page and that I need to re-write that session variable as well.  Does that seem accurate?  Thank you!

    Ronnie

    Monday, August 4, 2014 9:51 PM