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.