adsense

Monday, December 28, 2015

Asp.net textbox allow < charactor to be saved

Normally, saving special characters such as < or > require special configuration settings to be enabled in web.config file and in the page itself.


1) Disable request validation in the page

<%@ Page title="test" .... ValidateRequest="false" %>

2) Set requestValidationMode in Web.config


      



This solution could expose unexpected security threads.  Jquery provides a nice workaround to overcome this issue by escaping < and > characters as shown below.

 function validateTxt() {
    $("textarea, input[type='text']").change(function () { //replaces in all text areas and textboxes
      html = $(this).val(); //get the value
      //.replace("a" , "b")  works only on first occurrence of "a"
      html = html.replace(/< /g, "<"); //before: if there's space after < remove
      html = html.replace(/
      $(this).val(html); //set new value
   });
}

$(function () {
      validateTxt();
});
More discussion regarding the topic can be found here.

Cheers,
Samitha

 

Friday, November 20, 2015

IE11 compatibility mode off doPostBack and PageRequestManager undefined

There seem to be a bug in the browser definition files that came with .NET 2.0 and .NET 4.

This issue can be fixed by installing following hot fix for .net framework 4.0.

If you are deploying the asp.net application this should be installed in the machine where the application is deployed.

Regards,
Samitha

Wednesday, November 18, 2015

SQL Search

Searching for a specific text used in SQL objects can be made easy with the use of Redgate's SQL Search.

Even though SQL server provides queries for finding texts or columns this tool provides you much more flexibility.

Read following link for more information and download.

Cheers,
Samitha

Friday, October 30, 2015

Detect IE compatibility mode and version using JavaScript

Even though it is not recommended, following Javascript can be used to detect the IE compatibility mode.


// Create new object

var ieUserAgent = {
init: function () {
 
var ua = navigator.userAgent;
this.compatibilityMode = false;

    if(ua.indexOf("MSIE") == -1){
        this.version = 0;
        return 0;
    }

    if(ua.indexOf("compatible") == -1){
        this.compatibilityMode = false;
        return 0;

    }else{
        this.compatibilityMode = true;
        return 0;
    }
}
};

// Initialize the ieUserAgent object
ieUserAgent.init();

Read this thread for more details of the script.

Cheers,
Samitha


Thursday, September 10, 2015

Scroll to a specified target using jQuery

There may be situations where you need to scroll a page or element to a specified target. jQuery provides .scrollTo() method to achieve the desired functionality.

This method is built in from jQuery v 1.9.X. If you want to use it in an earlier version of jQuery, you can use an extension method.

Read here for more information.

Cheers,
Samitha

Friday, August 14, 2015

Remove all HTML Tags in text

There mey be situations where you need to remove all the HTML tags in a text. You can use Javascript Regex to to achove the desired result as displayed below.


<html>
<head>
<script type="text/javascript">
function replacetags(){
var html = /(<([^>]+)>)/gi;
for (i=0; i < arguments.length; i++)
arguments[i].value=arguments[i].value.replace(html, "")
}
</script>
</head>
<body>
       <form name="form1">
<textarea class="comment"  name="comments" id ="comments"  rows=5 cols=50></textarea><br>
<input type="button" value="Remove Tags" onClick="replacetags(this.form.comments)">
</form>
</body>
</html>





Monday, July 20, 2015

Visual Studio 2015 Relased

Microsoft finally released VS 2015 on July 20th. It has exiting new features including

  • Live code analysis (Light Bulbs),
  • Cross-platform debugging support, 
  • Visual Studio Graphics Diagnostics ,
  • Universal Windows apps for any Windows 10 device,
  • Cross-platform mobile games in C# with Unity and many more

Reade more and download

Samitha

Wednesday, June 24, 2015

ASP.Net load Browser specific CSS

When developing Webpages you might have come across browser specific styling issues.
When dealing with IE related CSS issues conditional statements can be used as displayed below.


< !--[if IE]>
    <link rel="stylesheet" type="text/css" 
href="ie-specific.css" />
<![endif]-->
 
There will be situations where you will want to
target on different browser.
 E.g. Chrome, firefox
The code given below can be used to check the
browser type and load the 
CSS programatically.
 

 Dim Browser As HttpBrowserCapabilities = Request.Browser
        'Attach a CSS style sheet accordingly  
 Dim cs As ClientScriptManager = Nothing

If Browser.Type.StartsWith("IE") Then 'Target All IE
     cs = Page.ClientScript
     cs.RegisterClientScriptBlock(Me.GetType(), "CSSLink", "")
ElseIf Browser.Type = "Firefox" Then 'Actual version of the
                                 ' browser can be specified
     cs = Page.ClientScript
    cs.RegisterClientScriptBlock(Me.GetType(), "CSSLink", "")
 End If
 
Cheers
Samitha 

Wednesday, March 25, 2015

Disable the Asp.Net linkbutton

When you working with the Asp.Net link button, you might have noticed that even though you have disabled it (Enabled = false) the user still can click the link.

It is not as straightforward as you might have expected to completely disable the link button. Use the following code snippet achieve the desired effect.


lnkBtn.Attributes.Remove("href")
lnkBtn.Attributes.CssStyle(HtmlTextWriterStyle.Color) = "gray"
lnkBtn.Attributes.CssStyle(HtmlTextWriterStyle.Cursor) = "default"
lnkBtn.Font.Underline = False ' used to remove the underline effect when mouse-hover

Cheers
Samitha

Wednesday, February 18, 2015

Using jQuery validate plugin in Asp .Net to validate dropdownlist

The jQuery validate plugin can be used in HTML page validations. It can be easily integrated to ASP.Net forms validation as described below.

1. In the header (or in the content page if you are using master pages) put references for jQuery and jQery validator plugin.

2.In general it is recommended that you wire form validation in the DOM ready event of the jQuery. But if you are using update panels you should wire up these events in the pageLoad. In this example I am validation a dropdown list with a default value "(not set)".  This is achieved using the jQuery.validator.addMethod  as shown below.

        
              jQuery.validator.addMethod("defaultInvalid", function (value, element) {
                switch (element.value) {
                    case "(not set)":
                        if (element.name == "<%= ddlCountry.UniqueID %>") return false;
                        break;
                    default:
                        return true;
                        break;
                }
            });

3. Next include the controls to be validated in the form validate method. Remember to include the defaultInvalid: true attribute to validate the default value.

 $("#form1").validate({

                rules: {
                    "<%= ddlCountry.UniqueID %>": {
                        required: true,
                        defaultInvalid: true
                    }
                },
                messages: {
                    "<%= ddlCountry.UniqueID %>": "* Required"
                }
            });
4. Finally define the IsValid() that is called on Update click,

 function IsValid() {
            var isValid = $("#form1").valid();

            if (isValid) {
                return true;
            }
            return false;
   }
               
The trick is to call the IsValid function on the OnClientClick event of the Update button.


Sunday, February 8, 2015

visual studio 2010/2012/2013 nuget error "The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel"

I recently came across this issue when trying to launch the nuget package manager. It seems this issue is common to all  VS2010,2012 and 2013 versions. Follow these steps to solve the issue.

Go to VS2010/2012/2013  -> Tools -> Library Package Manager -> Package Manager Settings
Choose Package Manager -> Package Sources.
1. Add a new package source as:
Name= NugetSrc
Source= http://packages.nuget.org/v1/FeedService.svc/
2. Move Up the newly added package source to the first position.
3. UnCheck existing "Nuget official package source"
4. Restart VS2010/2012/2013

cheers,
Samitha

Monday, February 2, 2015

Find and replace text in a file in c#

The easiest way to find and replace text in a file is to use the Replace method of string as shown below.

string content= File.ReadAllText("FileName.txt");
text =
content.Replace("find text", "replace text");
File.WriteAllText("
FileName.txt", text);

You can use the Regex.Replace method for more complex replacements

Cheers,
Samitha 

Thursday, January 22, 2015

Asp.net Closing child windows from JavaScript

These maybe situations where we use some of the additional information displayed  using window.open() method. It can cause issue if we keep these open widows when dealing with different user logins within the system.

The solution is to store all opened windows in an Javascript array and close them on logout or navigation to other links or menu options.

 var openWindows = new Array();

function OpenWindow(url){
          
            openWindows.push(window.open(url));
        }


We can use the window.onbeforeunload event to close the opened windows as shown below.

   window.onbeforeunload = function () {
            closeWindows();
        };


 function closeWindows() {
            for (i = 0; i < openWindows .length; i++) if (openWindows [i] && !openWindows[i].closed)
                openWindows[i].close();
        }

Cheers
Samitha

Friday, January 16, 2015

SQL Server check nemeric

There can be situations where you want to check whether a field is a numeric or not. SQL Server provides ISNUMERIC() function to achieve that purpose as shown below.


SELECT case when IsNumeric(cmp) = 1
       then cast(cmp as int)
       else 0 end   FROM Transactions)

This can be easily included in a where clause as follows

select * from Transactions 
where (case when IsNumeric(cmp) = 1
       then cast(
cmp as int)
       else 0 end) >= 7

 
Cheers
Samitha