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>

Friday, June 3, 2011

Combining JQuery Form Validation and Ajax




<script src="scripts/jquery-1.4.4.js" type="text/javascript"></script>

<script src="scripts/jquery.popup.js" type="text/javascript"></script>

<script src="scripts/jquery.updnValidatorCallout.js" type="text/javascript"></script>


<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div class="textField">
<asp:TextBox Width="255px" ID="txbName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="txbName"
ErrorMessage="Error: Contest Name is required."></asp:RequiredFieldValidator></div>
<p>
</p>
<asp:Button ID="btnSave" Text="Save" Class="submitBTN" runat="server" OnClick="btnSaveNew_Click" />

<script type="text/javascript">
$(document).ready(function() {

$.updnValidatorCallout.attachAll();
});
</script>

Wednesday, May 11, 2011

An MYSQL Statement that gets the last Thursday in the month

Here is a simplified version using just date math:

SELECT LAST_DAY(NOW()) - ((7 + WEEKDAY(LAST_DAY(NOW())) - 5) % 7);

Tuesday, March 29, 2011

Using Table-Valued Function in MS SQL

Code:
-- SET NOCOUNT ON 

CREATE FUNCTION DatesBetween2()
RETURNS @dates TABLE (
DateValue date NOT NULL PRIMARY KEY CLUSTERED
)
as
begin
DECLARE @startDate DATETIME = GETDATE();
DECLARE @endDate DATETIME = DATEADD(day, 9999, GETDATE());
BEGIN
WHILE (@startDate <= @endDate) BEGIN
INSERT INTO @dates VALUES (@startDate);
SET @startDate = DATEADD(day, 1, @startDate);
END;
RETURN;
END;
END;

select * from dbo.DatesBetween2()

Thursday, March 17, 2011

What is message queuing?

Message queuing has been used in data processing for many years. It is most commonly used today in electronic mail. Without queuing, sending an electronic message over long distances requires every node on the route to be available for forwarding messages, and the addressees to be logged on and conscious of the fact that you are trying to send them a message. In a queuing system, messages are stored at intermediate nodes until the system is ready to forward them. At their final destination they are stored in an electronic mailbox until the addressee is ready to read them.

Even so, many complex business transactions are processed today without queuing. In a large network, the system might be maintaining many thousands of connections in a ready-to-use state. If one part of the system suffers a problem, many parts of the system become unusable.

You can think of message queuing as being electronic mail for programs. In a message queuing environment, each program from the set that makes up an application suite is designed to perform a well-defined, self-contained function in response to a specific request. To communicate with another program, a program must put a message on a predefined queue. The other program retrieves the message from the queue, and processes the requests and information contained in the message. So message queuing is a style of program-to-program communication.
Queuing is the mechanism by which messages are held until an application is ready to process them. Queuing allows you to:

* Communicate between programs (which might each be running in different environments) without having to write the communication code.
* Select the order in which a program processes messages.
* Balance loads on a system by arranging for more than one program to service a queue when the number of messages exceeds a threshold.
* Increase the availability of your applications by arranging for an alternative system to service the queues if your primary system is unavailable.

How to: Access Binary Data as a Stream (WCF Data Services/Silverlight)

Silverlight

Open Data Protocol (OData) enables a data service to make binary data available outside of the feed itself. This binary data, called a media resource, is separate from but related to an entity, which is called a media link entry. For more information, see Working with Binary Data (WCF Data Services).

The procedure and examples in this topic show you how to add a reference to the Northwind streaming sample data service and call the GetReadStream(Object) method to retrieve binary data as a stream from an OData service.

The streaming data service accessed by the application is a special version of the Northwind sample data service that has been modified to support streaming. For more information see Streaming Provider (WCF Data Services). The Northwind streaming data service sample can be downloaded from the MSDN Code Gallery Web site.

Check the following link:
http://msdn.microsoft.com/en-us/library/ff602281%28v=VS.95%29.aspx