Tuesday, 4 June 2013

Configuring Commands and attaching to the Keyboard and Mouse events in Windows Presenation Foundation.



Commands:


In WPF world, Commands are excuted on events triggered by mouse or keyboard.


They are a loosely coupled way to bind the UI to the logic that performs the action. 


Commands give central Architecure for executing certain tasks from multiple actions taken by User on UI across application.

for example Application.Find command can be excuted upon click on menu or button if this command are binded to that menu or button.

Principle of implementing Command in WPF.

1.Command.It can be ApplicationCommands  for example Find ,copy , paste or Custom commands derived from ICommand interface .

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
namespace WPFCommands.Commands
{
   public  class AddCommand
    {
       static RoutedUICommand _addcommands;
          static AddCommand()
        {
            InputGestureCollection _gesturecol = new InputGestureCollection();
            KeyGesture _key = new KeyGesture(Key.A, ModifierKeys.Control );
           
             MouseGesture _mouseclick = new MouseGesture(MouseAction.LeftClick);
             _gesturecol.Add(_key);
            _gesturecol.Add(_mouseclick);
            _addcommands = new RoutedUICommand("Addcommands", "Addcommands", typeof(AddCommand), _gesturecol);
             

        }
        public static RoutedUICommand Addcommands
        {
            get { return _addcommands; }


2.CommandBinding: To associate a command with source control and handler upon invoking of command.
Add namespace  in Xaml where AddCommand has been defined
<Window 
        xmlns:Local="clr-namespace:WPFCommands.Commands">
    <Window.CommandBindings>
       Add CommandBindings
            <CommandBinding Command="Local:AddCommand.Addcommands"  Executed="_bindings_Executed"  >
                </CommandBinding>
    </Window.CommandBindings>

3.Command source: UI elements for example Button,menu item etc or gesture for example Mouse click or  pressing keyboard keys defined in InputGestureCollection.


    <StackPanel Background="Aquamarine"  HorizontalAlignment="Left" Height="158" Margin="125,105,0,0" VerticalAlignment="Top" Width="358" OpacityMask="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}">
            <TextBlock Background="White"   TextWrapping="Wrap" Text="TextBlock"   Height="37" Width="358"/>
            <Button Height="50px" Background="BlanchedAlmond"  Command="Local:AddCommand.Addcommands"   Content="Add"   />
        </StackPanel>
4.Command Handler: a CommandHandler is method and it is excuted when command is invoked.



    public partial class MainWindow : Window
    {
...
        void _bindings_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            MessageBox.Show("Hello");

        }

        
    }


 This is a much simple example of using command but power of command come in use when   UI code/presentation logic is placed into another class called ViewModal, a MVVM pattern. having Modal and UI logic handling UI events and properties.

Wednesday, 22 February 2012

Invalid postback or callback argument. Event validation is enabled using in configuration or <%@ Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data ...

Invalid postback or callback argument.  Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page.  For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them.  If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data

If you are using repeater control, make sure you have set  EnableViewState ="false"


<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound"  EnableViewState ="false" >


There is no need to change the web.config file.

 

 

Tuesday, 8 November 2011

Tuesday, 11 October 2011

ASMX web service is not returning JSON data in IIS7


This code was perfectly working and returning json data when was being called from Jquery in IIS 5-6.However when it was deployed to IIS7, it was constantly returning xml response.


 




[WebService(Namespace = "http://tempuri.org/")] 
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
 
[System.Web.Script.Services.ScriptService]
 public class SomeService : System.Web.Services.WebService
 { public SomeService()
 { //Uncomment the following line if using 
designed components //InitializeComponent(); }
 [WebMethod] [System.Web.Script.Services.ScriptMethod
(ResponseFormat = ResponseFormat.Json)] 
public List&lt;sometype&gt; getlist2() 
{ List&lt;sometype&gt; localtaglist =
 new List&lt;sometype&gt;(); ... foreach (...) 
{ sometype localtag = new sometype(); ... localtaglist.Add(localtag); } 
return localtaglist
 }


Add following lines of code to the web method and it will successfully 
return data in json format.

Context.Response.ContentType = "application/json"; 

JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
 Context.Response.ContentType = "application/json"
Context.Response.Write( jsonSerializer.Serialize(localtaglist));

Thursday, 9 June 2011

HTTP Error 404.11 - Not Found The request filtering module is configured to deny a request that contains a double escape sequence.IIS7



 


<system.webServer>
        <security>
            <requestFiltering allowDoubleEscaping="true">
            </requestFiltering>
        </security>
</system.webserver>
If your SEO friendly URL contains special characters as + or - and running under IIS7 , you 'll get
this error.
 
to solve this issue  , set allowDoubleEscaping to true in web.config file.






Thursday, 10 February 2011

Intelligencia UrlRewriter and 404 page not found Error

If you are getting this Error with Intelligencia.UrlRewriter and IIS 5/6,Follow these simple steps.
 1-Run->inetmgr.
2-Select web site you are working on.
3-Right click on it and select properties.
4-Click on "Configuration" button .
Now we need to tell IIS that aspnet_isapi.dll   should also execute those pages which has no aspx extension.



5-From Application Mapping tab , click Add button,in application mapping pop window,browse  
,C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll in executable .
6.type .* in extension.

7.Uncheck the"Check that the file exists".

 
Here we go.........................Run the Site.

Note all web.config configuration should also be done according to rewriter configuration.

Tuesday, 12 October 2010

fix for The state information is invalid for this page and might be corrupted Error

This error suggests that  the Viewstate information between postback are not same after first postback so when you click on a page's any server control , it will give this error.
Problem :You might have embedded a page which contains "runat= "server"  within a Masterpage.
For example f you have a placed jquery tabs in master page and jquery tabs pointing to an aspx page with
  attribute it will give this error.
Workaround:
Just remove "runat=server" from
tag in calling page .It will fix the error.











Friday, 8 October 2010

Fix for URL Rewriting With Intelligencia dll and IIS 5.1 Problem


If  a rewritten URL contains GUID( where Guid is encrypted in this example) and extra under  Intelligencia rewriter  and  IS 5.1 or IIS 6 ,  Request.QueryString["guid"]   will always read guid+ extra querystring.
For Example we have a following  rewritten URL.


Though URL have been  precisely configured  in web.config as
<rewrite url="/mysite/Preview/(.+)" to="/mysite/Preview.aspx? guid =$1">rewrite>
However, Request.QueryString["guid”] will always read this URL as


To fix this problem so that Request.QueryString["guid"] gets only Guid part of the URL, place .aspx extension with URL in web.config.

<rewrite url="/mysite/Preview/(.+).aspx" to="/mysite/Preview.aspx? guid =$1">rewrite>


And in code,  place.aspx extension with URL.


 this will generate URL as

and now Request.QueryString["guid”] will read only guid







Sunday, 18 July 2010

Books for MONO Cross platform development using C#

I just came across this book and found it interesting.The book is about cross platform development using open source MONO and C#.

# Practical Mono

Practical Mono

# Quick-> Concise->Practical Mono

Mono sams

Applications can also be developed for iphone.

Wednesday, 14 April 2010

ASP.NET Forms Authentication:Padding is Invalid and can not be removed Error.

I was getting this error using

FormsAuthenticationTicket ticket =

FormsAuthentication.Decrypt(authCookie.Value);

.I was trying Forms authentication to pass encrypted ticket between two web applications.I left out section with following configurations:

<machineKey

validationKey="AutoGenerate,IsolateApps"

decryptionKey="AutoGenerate,IsolateApps"

validation="AES"

decryption="Auto" />

This configuration means to create different key for each web applications thus it failed as

encrypted key was not same in two web applications.

so how to get same key for forms authentication. I resolved this issue by setting static values in section in both web.config files.

<machineKey validationKey='B5D752F96C1196D2A98014A3EF96F35192183FA47D467ACF0969F3687EC3C6BA3A959CD85BD3C282F2390B220ACD742568A8BC36BDBFBF9306ED807E6B090D56' decryptionKey='4BE38697FCD33A2C61D8FC93754FA668CEE4B5467B6B81F6' validation='SHA1'/>

Validation and decryption were generated using online machinekey generator tool.

Saturday, 24 October 2009

"Control can not be created because visual studio can not find the controls 's type in control assembly"

This error message was suddenly popping up in visual studio when I tried to drag my custom web control on a web page then tried to compile the code and found two main error messages in error list. Some thing like
"only one section per config file is allowed" since I had just added ajax configuration to web.config file like

<httpModules >

<add type="URLRewrite" name="URLRewrite" />

<httpHandlers>

<add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>

<add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false"/>

>

<httpModules>

<add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>


httpmodule tag has been declared twice in web.config.

Solution:

merge two httpModules tags in one tag.

<httpModules>

<add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>

<add type="URLRewrite" name="URLRewrite" />

These two sections of httpmodules in web.config were causing this error.