50% OFF!!!

Sunday, January 31, 2010

Javascript | clear input file value = file url

Included javascript function for clearing INPUT FILE contents!
I did it for disable uploading NON excel file (.xls).
(a message should be include...)

The CODE for ASP.NET:
<asp:FileUpload ID="txtFile" runat="server"
Enabled="true"
onchange="if (this.value.endsWith('.xls') == false) { this.parentNode.innerHTML = this.parentNode.innerHTML; }" />

The CODE HTML:

<input type="file" onchange="if (this.value.endsWith('.xls') == false) { this.parentNode.innerHTML = this.parentNode.innerHTML; }" style="width:216px;" />



Enjoy...
:)

Asp.Net - check/validate cookies support

Here is a code for checking/validating that user device (browser) supports cookies. in other words this code checks if cookies are enabled on the browser.

Here the code implemented in the Login page:
    // Constants:
    protected const string QUERYSTRING_CHECK_COOKIE = "checkcookie";
    protected const string TEST_COOKIE_NAME = "check_cookie";
    protected const string TEST_COOKIE_VALUE = "ok";

    // On-load event, at page load:
    protected override void OnLoad(EventArgs p_eventArgs)
    {
        if (IsPostBack == false)
        {
            // Check if browser support cookies
            if (Request[QUERYSTRING_CHECK_COOKIE] == null)
            {
                // try to insert cookie:
                Response.Cookies.Add(new HttpCookie(TEST_COOKIE_NAME, TEST_COOKIE_VALUE));
                string newUrl = string.Format("Login.aspx?{1}=1", QUERYSTRING_CHECK_COOKIE);
                this.Redirect(newUrl, true);
                return;
            }
            else
            {
                // here, client sould contain test cookie:
                HttpCookie cookie = Request.Cookies[TEST_COOKIE_NAME];
                bool isCookieEnabled = cookie != null && cookie.Value == TEST_COOKIE_VALUE;
                if (isCookieEnabled == false)
                {
                    // cookies are disabled!!! show error message
                    Label1.Text = "Device NOT support Cookies!";
                    return;
                }
            }

        }
    }

You can implement this also in other pages than Login page.
:)