Hi all, here a very fast example on how to read json data from file in Javascript,
here the content of *.htm file:
<!DOCTYPE html>
<html>
<head>
<title>LWebCode Javascript Read JSON from file</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<script type="text/javascript" src="mydata.json"></script>
<script>
function ReadData()
{
var dataLstAz = JSON.parse(jsLstData);
alert(jsLstData.length);
for (var key=0, size=jsLstData.length; key<size; key++)
{
var obj = jsLstData[key];
liout = obj.Name + ", " + obj.Age + "<br/>";
$("#Output").append(liout);
}
}
$( document ).ready(function() {
ReadData();
});
</script>
</head>
<body>
<div id="Output">
</div>
</body>
</html>
Here the content of "mydata.json" file,
put this file in same folder of *.htm file, or change path in *.htm file:
jsLstData = '[{"Name":"MyName1","Age":"20"}, {"Name":"MyName2","Age":"22"}]';
//End of file "mydata.json"
Now simply open *.htm file in browser and you'll get this output:
MyName1 20
MyName2 22
That's it, hope it helps
Showing posts with label ajax. Show all posts
Showing posts with label ajax. Show all posts
Tuesday, 18 April 2017
Wednesday, 11 January 2017
C# AJAX TextBox Cursor,Caret Focus with AutoPostBack="True" OnTextChanged="TextBox1_TextChanged"
Hi All, here explain how to mantain get and set cursor/caret position after a c# with AJAX Textbox TextChanged with AutoPostBack="True", this solution is cross browser compatible.
Put this Javascript code in your *.aspx Page:
Always in your *.aspx page:
<
On Page Code behind *.apsx.cs
Put this Javascript code in your *.aspx Page:
<script language="javascript" type="text/javascript" >//lwebcode.bloggger.com
//http://blog.vishalon.net/javascript-getting-and-setting-caret-position-in-textarea
var selstart=0;
var selend=0;
var pos = 0;
function GetPos(el)
{
if("selectionStart" in el) {
pos = el.selectionStart;
} else if("selection" in document) {
el.focus();
var Sel = document.selection.createRange();
var SelLength = document.selection.createRange().text.length;
Sel.moveStart("character", -el.value.length);
pos = Sel.text.length - SelLength;
}
return pos;
}
function SetPos(ctrl, mypos)
{
if(ctrl.setSelectionRange)
{
ctrl.focus();
ctrl.setSelectionRange(mypos, mypos);
}
else if (ctrl.createTextRange)
{
var range = ctrl.createTextRange();
range.collapse(true);
range.moveEnd('character', mypos);
range.moveStart('character', mypos);
range.select();
}
}
</script> Always in your *.aspx page:
<
asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True" OnTextChanged="TextBox1_TextChanged" > <asp:TextBox ID="TextBox2" runat="server" AutoPostBack="True" OnTextChanged="TextBox2_TextChanged" >On Page Code behind *.apsx.cs
private const string SCRIPT_DOFOCUS =
@"window.setTimeout('DoFocus()', 1);
function DoFocus()
{
try {
document.getElementById('REQUEST_LASTFOCUS').focus();
SetPos(document.getElementById('REQUEST_LASTFOCUS'), pos);
} catch (ex) {}
}";
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
HookOnFocus(this.Page as Control);
}
ScriptManager.RegisterStartupScript(this, typeof(Page), "ScriptDoFocus", SCRIPT_DOFOCUS.Replace("REQUEST_LASTFOCUS", Request["__LASTFOCUS"]), true);
}
private void HookOnFocus(Control CurrentControl)
{
//checks if control is one of TextBox, DropDownList, ListBox or Button
if ((CurrentControl is TextBox)
/*||
(CurrentControl is DropDownList) ||
(CurrentControl is ListBox) ||
(CurrentControl is Button)*/
)
//adds a script which saves active control on receiving focus
//in the hidden field __LASTFOCUS.
(CurrentControl as WebControl).Attributes.Add(
"onfocus",
"try{document.getElementById('__LASTFOCUS').value=this.id; GetPos(this);} catch(e) {}");
//checks if the control has children
if (CurrentControl.HasControls())
//if yes do them all recursively
foreach (Control CurrentChildControl in CurrentControl.Controls)
HookOnFocus(CurrentChildControl);
}protected void TextBox1_TextChanged(object sender, EventArgs e) {
//your code...
}protected void TextBox2_TextChanged(object sender, EventArgs e) {
//your code...
}
Labels:
ajax,
aspx,
autopostback,
c#,
caret,
cursor,
javascript,
lwebcode,
position,
scriptmanager,
textbox,
textchanged
Subscribe to:
Posts (Atom)