kazim mehdi

  Home  |   Contact  |   Syndication    |   Login
  20 Posts | 1 Stories | 19 Comments | 0 Trackbacks

News

Article Categories

Archives

Post Categories

Friday, January 13, 2012 #

Stylecop great tool from Microsoft to enforce code style rules. Sometimes we like to break 1 or more of its rules and very much like Complier should not complain about it anymore.

its easy by adding following attribute in  front of element that is going to break the Rule. In the following code its class.

 

[SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1402:FileMayOnlyContainASingleClass", Justification = "its ok here")]

Major problem that I faced is getting correct “CheckId” of the rule. If compiling code by commenting the SuppressMessage Attribute you will get

Rule violation warring in the Error List Window e.g.


SA1402: A C# document may only contain a single class at the root level unless all of the classes are partial and are of the same type.

SuppressMessage is not working on the message that we receive in the Error List Window, it requires CheckId. So how to get Stylecop rules checkId.

Its Easy open Stylecop setting from right clicking on project

ProjectProperties

> StylecopSetting

Deselect all of the settings and save it by clicking “OK”.

then open up the “Settings.StyleCop” that you will find in the Project directory that contains *.csproj file.

open it in any Editor , Here you can find Category Id and RuleId of all the Rules.

Below is the complete list of the Rules. AnalyzerId is the CategoryId

		<StyleCopSettings Version="105">
		
<GlobalSettings>
</GlobalSettings>
<Analyzers>
<Analyzer AnalyzerId="StyleCop.CSharp.DocumentationRules">
<Rules>
<Rule Name="InheritDocMustBeUsedWithInheritingClass">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="FileHeaderFileNameDocumentationMustMatchTypeName">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
</Rules>
<AnalyzerSettings />
</Analyzer>
<Analyzer AnalyzerId="StyleCop.CSharp.LayoutRules">
<Rules>
<Rule Name="CurlyBracketsForMultiLineStatementsMustNotShareLine">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="StatementMustNotBeOnSingleLine">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="ElementMustNotBeOnSingleLine">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="CurlyBracketsMustNotBeOmitted">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="AllAccessorsMustBeMultiLineOrSingleLine">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="OpeningCurlyBracketsMustNotBeFollowedByBlankLine">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="ElementDocumentationHeadersMustNotBeFollowedByBlankLine">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="CodeMustNotContainMultipleBlankLinesInARow">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="ClosingCurlyBracketsMustNotBePrecededByBlankLine">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="OpeningCurlyBracketsMustNotBePrecededByBlankLine">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="ChainedStatementBlocksMustNotBePrecededByBlankLine">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="WhileDoFooterMustNotBePrecededByBlankLine">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="SingleLineCommentsMustNotBeFollowedByBlankLine">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="ClosingCurlyBracketMustBeFollowedByBlankLine">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="ElementDocumentationHeaderMustBePrecededByBlankLine">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="SingleLineCommentMustBePrecededByBlankLine">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="ElementsMustBeSeparatedByBlankLine">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="CodeMustNotContainBlankLinesAtStartOfFile">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="CodeMustNotContainBlankLinesAtEndOfFile">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
</Rules>
<AnalyzerSettings />
</Analyzer>
<Analyzer AnalyzerId="StyleCop.CSharp.MaintainabilityRules">
<Rules>
<Rule Name="AccessModifierMustBeDeclared">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="FieldsMustBePrivate">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="CodeAnalysisSuppressionMustHaveJustification">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="DebugAssertMustProvideMessageText">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="DebugFailMustProvideMessageText">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="FileMayOnlyContainASingleClass">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="FileMayOnlyContainASingleNamespace">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="StatementMustNotUseUnnecessaryParenthesis">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="ArithmeticExpressionsMustDeclarePrecedence">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="ConditionalExpressionsMustDeclarePrecedence">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="RemoveDelegateParenthesisWhenPossible">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="AttributeConstructorMustNotUseUnnecessaryParenthesis">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="RemoveUnnecessaryCode">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
</Rules>
<AnalyzerSettings />
</Analyzer>
<Analyzer AnalyzerId="StyleCop.CSharp.NamingRules">
<Rules>
<Rule Name="ElementMustBeginWithUpperCaseLetter">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="ElementMustBeginWithLowerCaseLetter">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="InterfaceNamesMustBeginWithI">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="ConstFieldNamesMustBeginWithUpperCaseLetter">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="NonPrivateReadonlyFieldsMustBeginWithUpperCaseLetter">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="FieldNamesMustNotUseHungarianNotation">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="FieldNamesMustBeginWithLowerCaseLetter">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="AccessibleFieldsMustBeginWithUpperCaseLetter">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="VariableNamesMustNotBePrefixed">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="FieldNamesMustNotContainUnderscore">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
</Rules>
<AnalyzerSettings />
</Analyzer>
<Analyzer AnalyzerId="StyleCop.CSharp.OrderingRules">
<Rules>
<Rule Name="SystemUsingDirectivesMustBePlacedBeforeOtherUsingDirectives">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="UsingAliasDirectivesMustBePlacedAfterOtherUsingDirectives">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="UsingDirectivesMustBeOrderedAlphabeticallyByNamespace">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="UsingAliasDirectivesMustBeOrderedAlphabeticallyByAliasName">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
</Rules>
<AnalyzerSettings />
</Analyzer>
<Analyzer AnalyzerId="StyleCop.CSharp.ReadabilityRules">
<Rules>
<Rule Name="CommentsMustContainText">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="DoNotPrefixCallsWithBaseUnlessLocalImplementationExists">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="OpeningParenthesisMustBeOnDeclarationLine">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="ClosingParenthesisMustBeOnLineOfLastParameter">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="ClosingParenthesisMustBeOnLineOfOpeningParenthesis">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="CommaMustBeOnSameLineAsPreviousParameter">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="ParameterListMustFollowDeclaration">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="ParameterMustFollowComma">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="SplitParametersMustStartOnLineAfterDeclaration">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="ParametersMustBeOnSameLineOrSeparateLines">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="ParameterMustNotSpanMultipleLines">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="QueryClauseMustFollowPreviousClause">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="QueryClausesMustBeOnSeparateLinesOrAllOnOneLine">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="QueryClauseMustBeginOnNewLineWhenPreviousClauseSpansMultipleLines">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="QueryClausesSpanningMultipleLinesMustBeginOnOwnLine">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="DoNotPlaceRegionsWithinElements">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="CodeMustNotContainEmptyStatements">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="CodeMustNotContainMultipleStatementsOnOneLine">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="BlockStatementsMustNotContainEmbeddedComments">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="BlockStatementsMustNotContainEmbeddedRegions">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="UseStringEmptyForEmptyStrings">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="UseBuiltInTypeAlias">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="UseShorthandForNullableTypes">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
</Rules>
<AnalyzerSettings />
</Analyzer>
<Analyzer AnalyzerId="StyleCop.CSharp.SpacingRules">
<Rules>
<Rule Name="KeywordsMustBeSpacedCorrectly">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="CommasMustBeSpacedCorrectly">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="SemicolonsMustBeSpacedCorrectly">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="SymbolsMustBeSpacedCorrectly">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="DocumentationLinesMustBeginWithSingleSpace">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="SingleLineCommentsMustBeginWithSingleSpace">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="PreprocessorKeywordsMustNotBePrecededBySpace">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="OperatorKeywordMustBeFollowedBySpace">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="OpeningParenthesisMustBeSpacedCorrectly">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="ClosingParenthesisMustBeSpacedCorrectly">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="OpeningSquareBracketsMustBeSpacedCorrectly">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="ClosingSquareBracketsMustBeSpacedCorrectly">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="OpeningCurlyBracketsMustBeSpacedCorrectly">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="ClosingCurlyBracketsMustBeSpacedCorrectly">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="OpeningGenericBracketsMustBeSpacedCorrectly">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="ClosingGenericBracketsMustBeSpacedCorrectly">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="OpeningAttributeBracketsMustBeSpacedCorrectly">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="ClosingAttributeBracketsMustBeSpacedCorrectly">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="NullableTypeSymbolsMustNotBePrecededBySpace">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="MemberAccessSymbolsMustBeSpacedCorrectly">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="IncrementDecrementSymbolsMustBeSpacedCorrectly">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="NegativeSignsMustBeSpacedCorrectly">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="PositiveSignsMustBeSpacedCorrectly">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="DereferenceAndAccessOfSymbolsMustBeSpacedCorrectly">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="ColonsMustBeSpacedCorrectly">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="CodeMustNotContainMultipleWhitespaceInARow">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="CodeMustNotContainSpaceAfterNewKeywordInImplicitlyTypedArrayAllocation">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
</Rules>
<AnalyzerSettings />
</Analyzer>
</Analyzers>
</StyleCopSettings>

Thursday, September 15, 2011 #

There are two ways that I found to this but I found the second one better for readability.

1. Test Run Configuration file

2.  DeploymentItemAttribute

I am interesting in the “DeploymentItemAttribute” because its more readable gives flexibility to add either on the Test Class level so that more than 1 test can get the files

or at Test Method level.

 

I have a solution contains multiple project one of the project requires xsds to be there with the assembly. So

1. select the all the files scattered in multiple folders

SelectingMultipleFiles

 

2. Press F4 to view properties and select Copy to Output Directory to “Copy Always” | “Copy if newer”

CopyToOutputFolder

 

 

3. goto the MSTest project Test Method and add the DeploymentItemAttribute

 

[TestMethod]
[DeploymentItem(@"TestProject\Schemas", "Schemas")]
public void TestSchemaPath()
{
}

Where "TestProject" is the project included in the solution where MsTest project exist , "Schemas" is the folder needed to be created in the test run folder .

if we don’t specify the second parameter the Schemas to the DeploymentItem then the "Schemas" folder will not be created only Common & PreAward  folder will be there.


Thursday, September 08, 2011 #

DECLARE @RegLoc VARCHAR(100)
select @RegLoc='SOFTWARE\Microsoft\Windows NT\CurrentVersion'

EXEC [master].[dbo].[xp_regread]    @rootkey='HKEY_LOCAL_MACHINE', 
                                    @key=@RegLoc, 
                                    @value_name='ProductName'

 

The above code will return the installed version of windows on which the current instance of Sql Server is running.

references:

http://www.mssqlcity.com/Articles/Undoc/UndocExtSP.htm


Monday, August 01, 2011 #

I have to update the windows service.exe and related dll by using console on remote computer.

 

I tried

 

net start <servicename>

 

 

but it not allowed to query or issue command on the remote computer.

 

then I tried sc & PsService from SysInternals.

 

sc \\<ServerName> stop <ServiceName>

 

PsService \\<ServerName> stop <ServiceName>
the problem is both of them issue stop command and returned the control but I want to sure that service is stopped completely so that 
I can update files.
so 
I write the following code to do that.
 
@echo off
set serverName=server1
set serviceName=ervice1
sc \\%serverName% stop %serviceName%

call:check %serviceName%

echo the service %serviceName% on servcer:%serverName% is :stopped
goto:eof

:check
echo serverName: %serverName%
echo seriveName %~1

sc \\%serverName% query %~1 | find /I "State" | find /I "STOPPED"
echo %ERRORLEVEL%
IF ERRORLEVEL 1 (
goto:check %~1
) else (
goto:eof
)

Friday, June 10, 2011 #

SCENARIO

I have page that was hosted inside the iframe and I want to redirect the parent window when a user click a button inside the child page.

For that I added the following code in the Button’s click event as I have to execute some server side code before redirecting.

 

Page.ClientScript.RegisterStartupScript(this.GetType(), RedirectKey, string.Format("window.top.location.href = ‘http://google.com’;", url), true);

 

PROBLEM

Above mentioned code worked great but when the user used the browser’s back button to go back to the previous page. It went there for a moment and

redirect to the location that I put inside the above javascript.

 

SOLUTION

I googled and search specifically on the stackoverflow and found the similar post and thanks peter-bailey he post the solution here. And the solution is using replace method

instead of just setting href.

 

top.location.replace it didn’t produce history.

Page.ClientScript.RegisterStartupScript(this.GetType(), RedirectKey, string.Format("top.location.replace('http://google.com');", url), true);

Thursday, May 12, 2011 #

1. Set EnableScriptGlobalization=true on the script manager

<asp:ScriptManager runat="server" EnableScriptGlobalization="true" />

2. Use the  Sys.CultureInfo.CurrentCulture command either inside the alert or through Firebug console to check the CultureInfo at the client side

 

 

firebug-CurrentCulture-01
firebug-CurrentCulture-02
firebug-CurrentCulture-Details-01
firebug-CurrentCulture-Details-02

 

 

3. use Number.parseLocale instead of pareFloat so that it get correct value according to the current culture.


4. to update the value back with current culture use localeFormat

<script type="text/javascript">
function formatNumber()
{

var tbNumber = $("input[id$='tbNumber']");

var value = tbNumber.val();

var dValue = Number.parseLocale(value);
if (isNaN(dValue))
{
alert("invalid input please enter decimal values only");
}
else
{
var noOfDigitsAfterDecimal = 2;
var pattern = "N" + noOfDigitsAfterDecimal;
var finalValue = dValue.localeFormat(pattern);
tbNumber.val(finalValue);
}
}

</script>

 

Sample to play around

Content for ASPX page.

 

<table border="0" cellpadding="5" cellspacing="5">
<tr>
<td>
Enter number :
</td>
<td>
<asp:TextBox ID="tbNumber" runat="server" onchange="formatNumber();" />
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnLoadDefaultValue" Text="Load default value" runat="server" OnClick="btnLoadDefaultValue_Click" />
</td>
<td>
<asp:Button ID="btnGetValue" Text="Process" runat="server" OnClick="btnGetValue_Click" />
</td>
</tr>
<tr>
<td>
Default value:
</td>
<td>
<asp:Label ID="lblDefaultValue" runat="server" />
</td>
</tr>
<tr>
<td>
Typed value:
</td>
<td>
<asp:Label ID="lblResult" runat="server" />
</td>
</tr>
</table>

 

 

paste the above mentioned javascript in the page

 

Content for Code behind.

protected void Page_Load(object sender, EventArgs e)
{
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("da-DK");
Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture("da-DK");

if (!IsPostBack)
{
double dValue=2.898d;
lblDefaultValue.Text = dValue.ToString();
}
}

protected void btnLoadDefaultValue_Click(object sender, EventArgs e)
{
tbNumber.Text = lblDefaultValue.Text;
}

protected void btnGetValue_Click(object sender, EventArgs e)
{
lblResult.Text = tbNumber.Text;
}

Thursday, April 28, 2011 #

Add add_endRequest handler to the Page request manager as followed

Sys.WebForms.PageRequestManager.getInstance().add_endRequest(<javascript name function>);

Friday, April 01, 2011 #

$p4= "C:\Program Files\Perforce\p4.exe"

# need to be changed

$path="C:\dev\www"

$oldValue="((?<=\<td(.*))align\=""right"")"
$newValue="align=""left"""
# need to be changed

Write-Host "Searching for : $oldValue"
Write-Host "Replaced value: $newValue"


$count=0
dir $path -Recurse -Include *.aspx, *.ascx| ForEach-Object{
$text=Get-Content $_.FullName;
if ($text -match $oldValue)
{
&$p4 edit $_.FullName
$text=$text -replace $oldValue,$newValue
set-content $_.FullName -value $text

$count++
}
}

Write-Host "total files found:$count"

Set the correct path of the “$p4” variable to point to the correct Perforce exe.


I have to replace align=”right” to align=”left” of all <td> in all files in the whole project. Consider the following html

<table>
<tr>
<td align="right" style="border: 1 solid black">
cell1
</td>
<td style="border: 1 solid black" align="right">
cell2
</td>
<td id="td01" align="right" style="border: 1 solid black">
cell3
</tr>
</table>

 

Work breakdown:

  1. How to replace in single file
  2. how to replace in multiple files

 

How to replace in single file

if you look carefully “align” tag is placed at different places. 

To replace align=”right” to align=”left” only possible way is by use of Regular expression.

I need to take care that it should replace only the align attribute that is used in td tag.

To develop and test  the regular expression I used the very good tool called Expresso its free by the way.

To make sure that only td’s align attribute’s value is replaced I have to use regular expression technique called "Look behind” a very good source of learning regular expression is Regular-Expression.info ( I am using it a reference from a long time).

What Look behind do is: it look behind the matching expression.

e.g

((?<=\<td(.*))align\="right")

In the above regular expression (?<=\<td(.*)) is look behind part.

Expression search for the align tag and then look behind and filtered out the matches where look behind satisfied. In the similar way we also have “Look Ahead”.

If it is still not clear why we need to go into look behind fuss is because if we use only this (align\="right") it will replace the value of all the align attributes of all the tags. And if we use ((\<td(.*))align\="right") it will find the right align attribute but when we try replace it will replace the whole tag line instead of just replacing the align attribute value.

If you still have doubts try yourself in Expresso using the sample text provided in the start.

How to replace in multiple files

After I got the tested regular expression in hand I tried Visual studio 2010 find and replace with setting “Use” to Regular expression but it didn’t produce any result.

Then I tried the same in notepad++ that also a great tool in my arsenal. It didn’t able to handle Look behind construct.

My next choice then GrepWin Regular expression based find and replace application. It also don’t know how to handle Look behind construct.

Then I left with only two choices either develop a console application myself or use powershell so I opt the later one.

below in the full powershell code to find and replace the above mentioned goal.

# need to be changed 

$path="C:\dev\pages"

$oldValue="((?<=\<td(.*))align\=""right"")"
$newValue="align=""left"""
# need to be changed

Write-Host "Searching for : $oldValue"
Write-Host "Replaced value: $newValue"


$count=0
dir $path -Recurse -Include *.aspx, *.ascx| ForEach-Object{
$text=Get-Content $_.FullName;
if ($text -match $oldValue)
{
$text=$text -replace $oldValue,$newValue
set-content $_.FullName -value $text

$count++
}
}

Write-Host "total files found:$count"

Friday, February 11, 2011 #

call the parent javascript function using parent.

 

parent.update();

where update() is the name of the fuction that exist on the page that host the iframe.