Simple function to encode and decode UTF8 character for unicode (this is for IE)
Encode
utf8bytes= unescape(encodeURIComponent(unicodecharacters));
Decode
unicodecharacters= decodeURIComponent(escape(utf8bytes));
This article is demonstrating how to wire up an enter key into a textbox. For example you have a search text box where you press enter then it will click go button and at the same page you have another textbox where you want to do another button click when you press the enter which means it’ is not necessary to post the page. This java script is used to capture the key event of enter and execute the LinkButton and ASP.NET button Click Event on the server side. Please add this javascript to your javascript common library or add this to your master page. This piece of code works in Firefox as well
function ButtonKeyPress(evt, thisElementName)
{
if(evt.which || evt.keyCode)
{
if ((evt.which == 13) || (evt.keyCode == 13))
{
// alert('post back href: ' +document.getElementById(thisElementName).href);
if (typeof document.getElementById(thisElementName).href != 'undefined')
{
location = document.getElementById(thisElementName).href;
}
else
{
document.getElementById(thisElementName).click();
}
return false;
}
}
else
{
return true;
}
}
And add this to your .NET code behind on the page load
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
If (User.Identity.IsAuthenticated) Then
Response.Redirect("frmTimeMain.aspx")
End If
txtGUID.Attributes.Add("onkeydown", "ButtonKeyPress(event, '" + lnkSubmit.ClientID + "')")
txtPassword.Attributes.Add("onkeydown", "ButtonKeyPress(event, '" + lnkSubmit.ClientID + "')")
End If
End Sub
I need to make a text box that does not allow the user to type any numeric key at all
// function to only allow numeric key presses in a textbox
// this doesn't stop pasting of non numeric values
function fnNumeric_only(e) {
// deal with unicode character sets
var unicode = e.charCode ? e.charCode : e.keyCode;
// if the key is backspace, tab, or numeric
if (unicode == 8 || unicode == 9 || (unicode >= 48 && unicode <= 57) || (unicode == 46 || unicode == 44)) {
// we allow the key press
return true;
}
else {
// otherwise we don't
return false;
}
}
At the sametime I also need to make the counter/tally changing by the time you start typing or deleting a character from the text box. Let’s make a scenario this way e.g 3.25 is the text in your textbox and basically what we want is when the user pressing backspace and stop in “.” character, you want your function to assume it as “3″ instead of “0″, you can use this function
function parseLocalNum(num) {
return +(num.replace(",", "."));
}
I’ve also added these two function for getting and setting value of an input (e.g textbox and label) which is cross browser compatible
function getControlValue(controlName) {
var controlValue = "";
var hasInnerText = (document.getElementsByTagName("body")[0].innerText != undefined) ? true : false;
if (hasInnerText) {
controlValue = document.getElementById(controlName).innerText;
} else {
controlValue = document.getElementById(controlName).textContent;
}
return controlValue;
}
function setControlValue(controlName, value) {
var controlValue = "";
var hasInnerText = (document.getElementsByTagName("body")[0].innerText != undefined) ? true : false;
if (hasInnerText) {
document.getElementById(controlName).innerText = value;
} else {
document.getElementById(controlName).textContent = value;
}
return controlValue;
}
How to use getControlValue or setControlValue
setControlValue("ctl00_ContentPlaceHolder1_lblClaimedHours", totalClaimed.toFixed(1).toLocaleString())
A few days back I’ve got the error message “webform_dopostbackwithoptions is undefined” on the project that i’m working on.
The strange thing it happened only when I activated the securepagemodule, when i deactivated the module it works perfectly. I tried to debug it with HTTP debugger (fiddler tool) and i found that on that particular page, there is a request from webresources.axd but the request is not into https but into http and what i believe since the page is on secure mode therefore it discards the “webresources.axd” since it’s not secure. The workaround for this issue is by adding entry of “webresources.axd” under securepage and the problem is solved.
This is the sample of web.config for it
</secureWebPages
NOTE:This is resolved in the new version of securepage module
This might be useful for web developer which wants to have one key event handler that compatible with all browsers.
document.onkeyup = KeyCheck;
function KeyCheck(e)
{
//this is used for cross browser
var KeyID = (window.event) ? event.keyCode : e.keyCode;
switch(KeyID)
{
case 13:
checkPostCodeSelection();
break;
}
}
Normally we have two text boxes which is user name and password. On the first load of the page we want to set the default value of user name text box becoming ‘username’ and set the default value of password text box becoming ‘password’ not ‘*******’.
Well what most of us think that we can use javascript to change the type of the input box?well I’ve got this trick to have two textboxes in the same location and swap it with “OnFocus” event
Set the default value for username text box via code behind
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
txtUserName.Attributes.Add("onfocus", "JavaScript:if(this.value=='username'){this.value=''};")
txtUserName.Attributes.Add("onblur", "JavaScript:if(this.value==''){this.value='username'};")
txtUserName.Attributes.Add("value", "username")
txtPassword.Attributes.Add("onkeydown", _
"javascript:if((event.which && event.which == 13) || " _
+ "(event.keyCode && event.keyCode == 13))" _
+ " {document.getElementById('" + btnLogin.ClientID + "').click(); " _
+ " return false; } else return true;")
End Sub
I tried to implement AJAX on my website by using AJAX extension on visual studio .NET. I tried to drag update panel to my page and I’ve put my button event handler to the trigger collection in update panel. I was expecting when i click that particular button then it will not refresh or load up the page, but in fact it reloads the page which showing me that the AJAX panel does not work, and my firebug detects that there are errors related with sys is undefined.
This happens because I haven’t AJAX config on my web.config. The config is related with HTTP handler, you can try to put this in your web.config. Please make sure it should be inside “”
This is a function to capture querystring in javascript. I used this javascript to control my menu dynamically.
function getQueryString(strParamName){
var strReturn = "";
var strHref = window.location.href;
if ( strHref.indexOf("?") > -1 ){
var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
var aQueryString = strQueryString.split("&");
for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
if (
aQueryString[iParam].indexOf(strParamName.toLowerCase() + "=") > -1 ){
var aParam = aQueryString[iParam].split("=");
strReturn = aParam[1];
break;
}
}
}
return unescape(strReturn);
}
Sometimes people add javascript into the aspx page instead of using code behind but there is a case when we need to add javascript in the runtime using code behind. This is the example where javascript is used to provide switching of visibility based on the table visibility status. I used this technique to hide another user control when user click expand button. Client ID is required to know the real ID of the user control after rendering. In here we also add onclick event to the image tag.
if (!this.Page.IsStartupScriptRegistered("tableScript"))
{
string script = @"function checkVisibility() "
+ @"{"
+ @" var element = document.getElementById('" + this.OuterTableDetailSearch.ClientID + @"');"
+ @" var elementBtn = document.getElementById('" + this.btnExp.ClientID + @"');"
+ @" if (element.style['visibility'] == 'hidden')"
+ @"{"
+ @"element.style['visibility'] = 'visible';"
+ @"elementBtn.src = '/Images/button_expand.gif';"
+ @"}"
+ @"else"
+ @"{"
+ @"element.style['visibility'] = 'hidden';"
+ @"elementBtn.src = '/Images/image_casestudy_search.gif';"
+ @"}"
+ @"}";
this.Page.RegisterStartupScript("tableScript", script);
}
btnExp.Attributes.Add("OnClick",
"javascript:checkVisibility()");