Friday, 27 May 2016

How to call Javascript function from ASP.Net, C# code behind

There are multiple ways of calling Javascript functions from code behind C#. This can be done by using ClientScriptManager Class which is introduced in .NetFreamework 2.0.

ClientScriptManager Class is used to create the scripts in HTML dynamically. This class has set of methods to accomplish the requirements based on the situation.

Often, we use the below two methods for many of our ASP.Net applications.

  • RegisterClientScriptBlock() Method
  • RegisterStartupScript()

RegisterClientScriptBlock() Method

ClientScriptManager script = Page.ClientScript;
if (!script.IsClientScriptBlockRegistered(this.GetType(), "Alert"))
{
    script.RegisterClientScriptBlock(this.GetType(), "Alert", "alert('hi')",true);
}

RegisterStartupScript() Method

ClientScriptManager script = Page.ClientScript;
if (!script.IsClientScriptBlockRegistered(this.GetType(), "Alert"))
{
    script.RegisterStartupScript(this.GetType(), "Alert", "alert('hi')",true);
}

Both the methods, RegisterStartupScript() and RegisterClientScriptBlock() inject Javascript code that will be execute during page load or start up of subsequent postback. The main difference is that RegisterClientScriptBlock() methods injects the script after the form open tag ("< form >") but before page controls and the RegisterStartupScript() methods injects the script after page controls but before form close tag ("< /form >"). That means using RegisterClientScriptBlock() method, we cannot access page controls in the script block while using RegisterStartupScript() method we can.

We can also directly call the Javascript functions available in the page as show below.

Page.ClientScript.RegisterStartupScript(this.GetType(),"CallMyFunction","MyFunction()",true);

MyFunction() is the function of Javascript in HTML. CallMyFunction is the string of ClientScriptmanager Class which is unique to avoid duplicated functions

Here is the example for Javascript confirm alert in C# -

Page.ClientScript.RegisterStartupScript(this.GetType(),"CallMyFunction","return confirm('Are you sure to continue?')",true);


Monday, 23 May 2016

Google Charts | AreaCharts | PieCharts by populating data from JSON


Using AJAX to load JSON Data for Google Visualization Charts

This post describes how to load data from server-side code using AJAX for Google Visualization Charts. This example fetching the data from external JSON file which contains the Chart data in JSON format.
Here are the files we are using in this example -
  • GoogleChart.html
  • data.txt

GoogleChart.html

This is the file that the user browses to. The drawChart() function calls the jQuery ajax() function to send a query to a URL and get back a JSON string. The URL here is of the local data.txt file. The returned data is actually a DataTable defined in the local data.txt file. This DataTable is used to populate a pie chart, area chart which is then rendered on the page.

data.txt

{
"cols": [
{"id":"","label":"Topping","pattern":"","type":"string"},
{"id":"","label":"Slices","pattern":"","type":"number"}
],
"rows": [
{"c":[{"v":"Mushrooms","f":null},{"v":3,"f":null}]},
{"c":[{"v":"Onions","f":null},{"v":1,"f":null}]},
{"c":[{"v":"Olives","f":null},{"v":1,"f":null}]},
{"c":[{"v":"Zucchini","f":null},{"v":1,"f":null}]},
{"c":[{"v":"Pepperoni","f":null},{"v":2,"f":null}]}
]
}

Friday, 6 May 2016

Indian Billionaire Nandan Nilekani invests in train travel based start-up RailYatri.in

Nandan Nilekani, co-founder of Infosys and former Chairman of UIDAI has invested an undisclosed amount in RailYatri.

In addition to the former UIDAI chairman, RailYatri's existing investors Helion Ventures, Omidyar Networks and Blume Ventures have participated in this round, RailYatri said in a statement.

"What excites me about RailYatri is the fact that this is a new-age product made by Indians for Indians. The RailYatri app, which intelligently harnesses the power of smartphones, data and mobile payments, has a huge opportunity of impacting the lives of millions of travellers," Nilekani reasoned.

RailYatri.in uses deep-analytics technology to make intelligent predictions that help travelers make smart decisions for their upcoming travel.

About RailYatri.in:

The brainchild of seasoned experts from IITs and IIM – KapilRaizada, SachinSaxena; and Manish Rathi with over 20 years of engineering experience – RailYatri is changing the ecosystem of train travel in India. The intelligent travel appprovides the most comprehensive information on trains, passenger amenities at stations, platform information, speed of the train, personalized alerts, and much more via the mobile apps, mobile web and SMS.

“It’s not just the money which is key to the business, but the right kind of advisors are as important. We are honored to board Nandan Nilekani on our platform,” says Kapil Raizada, Co-Founder RailYatri.