Readifarian

Joined Readify Australia as a Senior Consultant. Super excited to be a part of awesomeness!

Advertisements

Get iframe content

To get element say hidden field value residing in an iFrame from the parent form, you can do something on parent form’s client side like:

var value = frame1.contentWindow.document.getElementById(‘hdnField’).value;

Force observable to refresh

<div data-bind=”text: curDate”></div>
<input type=”button” data-bind=”click: recalcCurDate” value=”Mutated”></input>

In order to refresh current date, we need a dummy observable and then call notifysubscribers() on dummy wherver required in order to force refresh on the curDate:-

(function() {

var vm = function() {
var $this = this;

$this.dummy = ko.observable();

$this.curDate = ko.computed(function() {
$this.dummy();
return new Date();
});

$this.recalcCurDate = function() {
$this.dummy.notifySubscribers();
};
};

ko.applyBindings(new vm());

}());

fiddle

When $parent.foo is a prototype method, the this is not the parent

While working with prototype methods in Knockout, “this” will not be accessible if you bind like:
data-bind=”click: $parent.foo”

Do not pass the function directly to the click binding, wrap it in an anonymous function to have it called in context of your view model:

data-bind=”click: function() { $parent.foo() }”
or
data-bind=”click: $parent.foo.bind($parent)”

this way when the binding is activated it will use $parent as context of the call – i.e. this

Convert Datatable to JSON String in C#, VB.NET with Column Names (Dictionary Object)

//C#

public string ConverDataTableToJSON()
{
DataTable dt = new DataTable();
using (SqlConnection con = new SqlConnection("Data Source=SureshDasari;Initial Catalog=master;Integrated Security=true")){
using (SqlCommand cmd = new SqlCommand("select title=City,lat=latitude,lng=longitude,description from LocationDetails", con)){

con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);

//convert the filled DT into JSON
System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();

List<Dictionary> rows = new List<Dictionary>();
Dictionary row;
foreach (DataRow dr in dt.Rows){
row = new Dictionary();
foreach (DataColumn col in dt.Columns){
row.Add(col.ColumnName, dr[col]);
}
rows.Add(row);
}
return serializer.Serialize(rows);
}
}
}

‘VB.NET
Public Function ConvertDataTabletoString() As String
Dim dt As New DataTable()
Using con As New SqlConnection("Data Source=SureshDasari;Initial Catalog=master;Integrated Security=true")
Using cmd As New SqlCommand("select title=City,lat=latitude,lng=longitude,description from LocationDetails", con)
con.Open()
Dim da As New SqlDataAdapter(cmd)
da.Fill(dt)

'convert the filled DT into JSON
Dim serializer As New System.Web.Script.Serialization.JavaScriptSerializer()
Dim rows As New List(Of Dictionary(Of String, Object))()
Dim row As Dictionary(Of String, Object)
For Each dr As DataRow In dt.Rows
row = New Dictionary(Of String, Object)()
For Each col As DataColumn In dt.Columns
row.Add(col.ColumnName, dr(col))
Next
rows.Add(row)
Next
Return serializer.Serialize(rows)
End Using
End Using
End Function

Change execution timeout for a specific request handler

In case you want to increase the timeout for a specific handler e.g., Telerik ReportViewer in my case you can add the following in the bottom of your web.config

....
<location
path="Telerik.ReportViewer.axd"> <system.web> <!-- Note: executionTimeout is in seconds and is set to 1 hour here. --> <httpRuntime executionTimeout="3600" /> </system.web> </location>
....
</configuration>

Enforce latest IE version even if the site is added to Compatability Mode

In your web.config under the system.webserver tag add the following header to enforce for all the pages

<system.webServer>
<httpProtocol>
<customHeaders>
<add name=”X-UA-Compatible” value=”IE=Edge,chrome=1″ />
</customHeaders>
</httpProtocol>

…….

or you can also add a meta tag on specific page under the <head> but that would be a per page change

<meta http-equiv=”X-UA-Compatible” content=”IE=edge”> 

For details check:

http://stackoverflow.com/questions/6771258/whats-the-difference-if-meta-http-equiv-x-ua-compatible-content-ie-edge-e

Enforce latest IE version even if the site is added to Compatability Mode

In your web.config under the system.webserver tag add the following header to enforce for all the pages

<system.webServer>
<httpProtocol>
<customHeaders>
<add name=”X-UA-Compatible” value=”IE=Edge,chrome=1″ />
</customHeaders>
</httpProtocol>

…….

or you can also add a meta tag on specific page under the <head> but that would be a per page change

<meta http-equiv=”X-UA-Compatible” content=”IE=edge”> 

For details check:

http://stackoverflow.com/questions/6771258/whats-the-difference-if-meta-http-equiv-x-ua-compatible-content-ie-edge-e