Thursday, March 21, 2013

ASP.NET WebGrid and Web API

There are two link Get the Most out of WebGrid in ASP.NET MVC http://msdn.microsoft.com/en-us/magazine/hh288075.aspx Introduction to ASP.NET Web API http://www.codeproject.com/Articles/549152/Introduction-to-ASP-NET-Web-API

Multi Key in Dictionary

This is a very simple example, got from google search.
    
        static void Main(string[] args)
        {
            var lista = ListaClienteConta();
            //var chave = Tuple.Create(1, 1);
            //Console.WriteLine("Saldo selecionado é: {0}",
            //            lista[chave].ToString());
            //Console.WriteLine(lista.Keys+":"+lista.Values);
            foreach ( var pair in  lista)
            {
                Console.WriteLine("{0},{1}",pair.Value,pair.Key.ToString());
                Console.WriteLine("{0}",pair.Key.Item1);
            }
            Console.Read();
        }



        public static Dictionary<Tuple<int, int>, string> ListaClienteConta()
        {
            Dictionary<Tuple<int, int>, string> lista =
                                new Dictionary<Tuple<int, int>, string>();
           
            lista.Add(Tuple.Create(1, 2), "aa");
            lista.Add(Tuple.Create(3, 4), "bb");
           
            return lista;
        }

Friday, March 1, 2013

Get Decimal Places (Scale)

Get from google search:




      public double GetPrecision(string s)
        {
            NumberFormatInfo nfi = new NumberFormatInfo();
            string[] splitNumber = s.Split('.');
            if (splitNumber.Length &amp;gt; 1)
            {
                return 1 / Math.Pow(10, splitNumber[1].Length);
            }
            else
            {
                return 1;
            }
        }

        public int getscale(decimal dec)
        {
            uint[] bits = (uint[])(object)decimal.GetBits(dec);
            uint scale = (bits[3] &amp;gt;&amp;gt; 16) &amp;amp; 31;
            return (int)scale;
        }

        const int SIGN_MASK = ~Int32.MinValue;
        public static int GetDigits(Decimal value)
        {
            return (Decimal.GetBits(value)[3] &amp;amp; SIGN_MASK) &amp;gt;&amp;gt; 16;
        }

Currency (Money) in Javascript



            var Money = function (amount) {
                this.amount = amount;
            }
            Money.prototype.valueOf = function () {
                return Math.round(this.amount * 100) / 100;
            }

            var m = new Money(50.42355446);
            var n = new Money(30.342141);

            alert(m.amount + n.amount);
            alert(m + n);

Wednesday, February 6, 2013

Get list of DataField in gridview


String boundFields = String.Empty;

for (int i = 0; i < grd.Columns.Count; i++)
{
    DataControlField field = grd.Columns[i];
    BoundField bfield = field as BoundField;

    if (bfield != null)
        boundFields += bfield.DataField + ",";
}
boundFields = boundFields.TrimEnd(','); 

The code is from the following link:
http://stackoverflow.com/questions/6449832/get-list-of-datafield-in-gridview

Monday, September 26, 2011

A simple example GetPostBackEventReference

You can copy the code and test. Web page:
Code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Pass Javascript Variables to Server</title>
     <script type="text/javascript">
         function SetHiddenVariable()
         {
            var jsVar = "dotnetcurry.com";
            __doPostBack('callPostBack', jsVar);
        }
        function Submit_Click() {
            var text = document.getElementById("TextArea1");
            __doPostBack("btnSubmit", text.value);
            return false;
        }
    </script>
</head>
 
<body>
    <form id="form1" runat="server">
    <div>   
        <asp:TextBox ID="txtJSValue" runat="server"></asp:TextBox><br />
        <textarea id="TextArea1" cols="20" rows="2"></textarea><br />
        <asp:Button ID="btnJSValue" Text="Click to retreive Javascript Variable"
            runat="server"/><br />
        <asp:Button ID="Button1" runat="server" Text="Button"  OnClientClick="return Submit_Click();" UseSubmitBehavior="false" />
    </div>
    </form>
</body>
</html>
Code Behind:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.ClientScript.GetPostBackEventReference(this, "arg");
        if (IsPostBack)
        {
            string eventTarget = this.Request["__EVENTTARGET"];
            string eventArgument = this.Request["__EVENTARGUMENT"];

            if (eventTarget != String.Empty && eventTarget == "callPostBack")
            {
                if (eventArgument != String.Empty)
                {
                    txtJSValue.Text = eventArgument;
                    //btnJSValue.Enabled = false;
                }
            }
            else if (eventTarget != String.Empty && eventTarget == "btnSubmit")
            { txtJSValue.Text = eventArgument; }
        }
        else
        {
            btnJSValue.Attributes.Add("onClick", "SetHiddenVariable();");
        }
    }
}

Thursday, August 4, 2011

3 Client Side Javascript sample



<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>

<script type="text/javascript">

function displaymessage() {
alert("Hello World!");
}

function updatelabelcontrol(Lavell) {
var usename = $get("TextBox1").value;
var lb2 = $get("Label2").innerText;
var labella = $get("Label1");// $get(Lavell);
labella.innerHTML = usename+lb2; //"test";
}

function btn_Click() {
var usename = $get("TextBox1").value;
var lb2 = $get("Label2").value
var labell = $get("Label1");
labell.innerHTML = usename+lb2;
}

function getCookie(c_name) {
var i, x, y, ARRcookies = document.cookie.split(";");
for (i = 0; i < ARRcookies.length; i++) {
x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
x = x.replace(/^\s+|\s+$/g, "");
if (x == c_name) {
return unescape(y);
}
}
}

function setCookie(c_name, value, exdays) {
var exdate = new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString());
document.cookie = c_name + "=" + c_value;
}

function checkCookie() {
var username = getCookie("username");
if (username != null && username != "") {
alert("Welcome again " + username);
}
else {
username = prompt("Please enter your name:", "");
if (username != null && username != "") {
setCookie("username", username, 365);
}
}
startTime();
}

function startTime() {
var today = new Date();
var h = today.getHours();
var m = today.getMinutes();
var s = today.getSeconds();
// add a zero in front of numbers<10
m = checkTime(m);
s = checkTime(s);
document.getElementById('txt').innerHTML = h + ":" + m + ":" + s;
t = setTimeout('startTime()', 500);
}

function checkTime(i) {
if (i < 10) {
i = "0" + i;
}
return i;
}

</script>

</head>
<body onload="checkCookie()">
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div id="txt"></div>
<div>
<asp:Label ID="Label1" runat="server" Text="Label1" onclick='updatelabelcontrol(this)'></asp:Label>
<br />
<asp:Label ID="Label2" runat="server" Text="Label2"></asp:Label><br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="btn_Click()" />
<br />
<input type="button" value="Click me!" onclick="displaymessage()" />
</div>
</form>
</body>
</html>