Friday, March 22, 2013

Tic Tac Toe Game


CSS
<style type="text/css">
        input
        {
            width: 50px;
            height: 50px;
        }
        .winner
        {
            background-color: #FF69B4;
        }
</style>
HTML
<table width="150px">
            <tr>
                <td>
                    <input type="button" id="1" onclick="return Tic(this);" />
                </td>
                <td>
                    <input type="button" id="2" onclick="return Tic(this);" />
                </td>
                <td>
                    <input type="button" id="3" onclick="return Tic(this);" />
                </td>
            </tr>
            <tr>
                <td>
                    <input type="button" id="4" onclick="return Tic(this);" />
                </td>
                <td>
                    <input type="button" id="5" onclick="return Tic(this);" />
                </td>
                <td>
                    <input type="button" id="6" onclick="return Tic(this);" />
                </td>
            </tr>
            <tr>
                <td>
                    <input type="button" id="7" onclick="return Tic(this);" />
                </td>
                <td>
                    <input type="button" id="8" onclick="return Tic(this);" />
                </td>
                <td>
                    <input type="button" id="9" onclick="return Tic(this);" />
                </td>
            </tr>
        </table>
        <br />
        <input type="button" id="btnPlay" onclick="return Play();" value="Play" 
                  style="width:100px; height: 30px; font: verdana 12pt;" />
        <asp:HiddenField ID="hfGameOver" runat="server" Value="No" />
        <asp:HiddenField ID="hfValue" runat="server" Value="X" />
Script
<script type="text/javascript" language="javascript">
   var ID = [1, 2, 3, 4, 5, 6, 7, 8, 9];
       
    // Possible Solution Array 2D
   var SolArr = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [1, 4, 7],
                     [2, 5, 8], [3, 6, 9], [1, 5, 9], [3, 5, 7]];
       
        // For Play Again
        function Play() {
            for (var i = 0; i < 9; i++) {
              var btn = document.getElementById(ID[i]);
              btn.value = "";
              btn.className = "";
              document.getElementById('<%= hfGameOver.ClientID  %>').value = "No";
            }
        }
        // For Tic
        function Tic(obj) {           
            var val;
            var TicValue = document.getElementById('<%=  hfValue.ClientID  %>').value;
            if (document.getElementById('<%= hfGameOver.ClientID  %>').value == "Yes") {
                alert("Game Over");
                return false;
            }
            if (obj.value != "") {
                alert("You can't change value");
                return false;
            }
            if (TicValue == "X") {
                obj.value = TicValue;
                val = "O";
            }
            else {
                obj.value = TicValue;
                val = "X";
            }
            CheckForResult();
            document.getElementById('<%=  hfValue.ClientID  %>').value = val;
        }
        // For Checking Result.
        function CheckForResult() {           
            var TicValue = document.getElementById('<%=  hfValue.ClientID  %>').value;
            for (var i = 0; i < 8; i++) {
                var Id1 = SolArr[i][0], Id2 = SolArr[i][1], Id3 = SolArr[i][2];
                var txt1 = document.getElementById(Id1);
                var txt2 = document.getElementById(Id2);
                var txt3 = document.getElementById(Id3);
                if ((txt1.value != "") && (txt1.value == txt2.value) && 
                    (txt2.value == txt3.value)) {
                    txt1.className = "winner";
                    txt2.className = "winner";
                    txt3.className = "winner";
                   
                    document.getElementById('<%= hfGameOver.ClientID  %>').value = "Yes";
                    alert(TicValue + " is Winner.");
                    return false;
                }
               
            }
        }
            
    </script>

Joins in Linq


Join Between two Tables
DataTable DT1 = FillDatset("select * from country_Master");
DataTable DT2 = FillDatset("select * from State_Master");
          var List = (from DtCountry in DT1.AsEnumerable()
                           join DtState in DT2.AsEnumerable()
                           on DtCountry.Field<int>("CountryID")
                           equals DtState.Field<int>("CountryID") into DTALL
                           from _dtAll in DTALL.DefaultIfEmpty()
                           select new
                            {
                               CountryID = DtCountry.Field<int>("CountryID"),
                               StateName = (_dtAll != null) ?
                                                   _dtAll.Field<string>("StateName") :
                                                     string.Empty
                             }
                           ).ToList();
          var List1 = (from DtCountry in DT1.AsEnumerable()
                            join DtState in DT2.AsEnumerable()
                            on DtCountry.Field<int>("CountryID")
                            equals DtState.Field<int>("CountryID") into DTALL
                            from _dtAll in DTALL.DefaultIfEmpty()
                            select _dtAll).ToList();

Read Excel file in C#


protected void BtnReadExcel_Click(object sender, EventArgs e)
        {
            try
            {
              OleDbConnection con = new OleDbConnection("
                                                Provider=Microsoft.Jet.OLEDB.4.0;
                                                Data Source="
+ Server.MapPath("TestExcel.xlsx") + ";
                                                Extended Properties='Excel 8.0;HDR=Yes;'"
 

                                               );
                OleDbDataAdapter da = new OleDbDataAdapter("select * from [Sheet1$]",
                                                                                 con);
                DataSet ds = new DataSet();
                da.Fill(ds);
            }
            catch (Exception ex)
            {
                throw;
            }
        }

Call server side function using JQuery/JSON


<script src="App_Script/jquery-1.4.3.min.js" type="text/javascript">
</script>
<script type="text/javascript">
        $(document).ready(function () {
            $("#<%= btnLogin.clientID %>").click(function () {
                    $.ajax({
                        type: "POST",
                        url: "server-side.aspx/GetCategory",
                        data: "{}",
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        async: true,
                        cache: false,
                        success: function (msg) {
                            $('#msg').text(msg.d);
                        },
                        error: function (data) { alert("Error" + data.d); }
                    })
                    return false;
               
             });
        });
   
</script>
HTML
<div id="msg">
 </div>
 <asp:Button runat="server" ID="btnLogin" />

C#
[WebMethod]
public static string FillRelatedCategory1 ()
{
        return " success";
}

Modal Popup With Jquery


<script type="text/javascript">
        $(document).ready(function () {
            //select all the a tag with name equal to modal
            $('a[name=modal]').click(function (e) {
                //Cancel the link behavior
                e.preventDefault();
                //Get the A tag
                var id = $(this).attr('href');
                //Get the screen height and width
                var maskHeight = $(document).height();
                var maskWidth = $(window).width();
                //Set heigth and width to mask to fill up the whole screen
                $('#mask').css({ 'width': maskWidth, 'height': maskHeight });
                //transition effect                
                $('#mask').fadeIn(500);
                $('#mask').fadeTo("slow", 0.8);
                //Get the window height and width
                var winH = $(window).height();
                var winW = $(window).width();
                //Set the popup window to center
                $(id).css('top', winH / 2 - $(id).height() / 2 );
                $(id).css('left', winW / 2 - $(id).width() / 2);
                //transition effect
                $(id).fadeIn(500);
            });
            //if mask is clicked
            $('#mask').click(function () {
                $get("<%=txtUN.clientID %>").value = "";
                $get("<%=txtPass.clientID %>").value = "";
                $(this).fadeOut(500, '', 'slow');
                $('.window').fadeOut(500, '', 'slow');
            });
            // When close button clicked
            $('#Close').click(function (e) {              
                e.preventDefault();
                $get("<%=txtUN.clientID %>").value = "";
                $get("<%=txtPass.clientID %>").value = "";
                $get('MSG').innerHTML = "";
                $('#mask').fadeOut(500, '', 'slow');
                $('.window').fadeOut(500, '', 'slow');
            });
            // when browser resize
            $(window).resize(function () {
                var box = $('#boxes .window');
                //Get the screen height and width
                var maskHeight = $(document).height();
                var maskWidth = $(window).width();
                //Set height and width to mask to fill up the whole screen
                $('#mask').css({ 'width': maskWidth, 'height': maskHeight });
                //Get the window height and width
                var winH = $(window).height();
                var winW = $(window).width();
                //Set the popup window to center
                box.css('top', winH / 2 - box.height() / 2 );
                box.css('left', winW / 2 - box.width() / 2);
            });
        });
    </script>
CSS
<style type="text/css">
        body{font-family: Verdana;font-size: 10pt;}
        a{color: #333;text-decoration: none;}
        a:hover{color: #ccc;text-decoration: none;}
        #mask{position: absolute;left: 0;top: 0;z-index: 9000;
                     background-color: #000;display: none;}
        #boxes .window{position: fixed;left: 0;top: 0;width: 440px;
                                  height: 200px;display: none;z-index: 9999;
                                   padding: 20px;}
        #boxes #dialog{width: 375px;height: 203px;padding: 10px;
                                  background-color: #ffffff;}
        #boxes #dialog1{width: 375px;height: 203px;}
        #dialog1 .d-login{float: left;width: 180px;height: 53px;margin-left: 109px;}
        .txtBox{height: 25px;margin: 10px;width: 200px;}
        .loginbtnC{background-image: url("Images/ImgCancel.jpeg");
                         border: medium none;height: 32px;width: 80px;}
        .loginbtnL{background-image: url("Images/loginImage.jpeg");}
    </style>
HTML
<div id="boxes">
                
                <div id="dialog1" class="window" style="display: none;">
                    <fieldset style="background-color: #FFF; border-radius: 25px;
                                           -webkit-border-radius: 25px;">
                        <legend style="margin-left: 15px;"> LOGIN</legend>
                        <div style="padding-top: 10px; text-align: center;">
                            <span>UserName :</span>
                            <
asp:TextBox ID="txtUN" CssClass="txtBox" runat="server"
placeholder="Enter UserName"></asp:TextBox>
     <
br />
                            <span>Password :</span>
    <asp:TextBox ID="txtPass" CssClass="txtBox" runat="server"
                                placeholder="Enter Password"></asp:TextBox>
                        </div>
                        <div class="d-login">
                        <asp:Button runat="server" ID="btnLogin" CssClass="loginbtnC loginbtnL" />
                        <input id="Close" type="button" class="loginbtnC" />
                      </div>
                    </fieldset>
                </div>
               
               
                <div id="mask">
                </div>
            </div>