<script type="text/javascript">
function allclear()
{ //debugger
var elements=document.getElementsByTagName("INPUT");
for(i=0;i<elements.length;i++)
if(elements[i].type=="radio")
elements[i].checked=false;
for(i=0;i<elements.length;i++)
if(elements[i].type=="checkbox")
elements[i].checked=false;
for(i=0;i<elements.length;i++)
if (elements[i].type=="text")
elements[i].value="";
var elements1=document.getElementsByTagName("SELECT");
for(i=0;i<elements1.length;i++)
if(elements1[i].id!="" && elements1[i].id!=null)
elements1[i].selectedIndex=0;
}
</script>
---------------------------------------------------------------------------------------
private void ClearControls(Control c)
{
if (c.GetType() == typeof(System.Web.UI.WebControls.TextBox))
((TextBox)c).Text = "";
if (c.GetType() == typeof(System.Web.UI.WebControls.RadioButtonList))
{
RadioButtonList rbtl = (RadioButtonList)c;
for (int j = 0; j < rbtl.Items.Count; j++)
rbtl.Items[j].Selected = false;
}
if (c.GetType() == typeof(System.Web.UI.WebControls.DropDownList))
((DropDownList)c).SelectedIndex = 0;
if (c.GetType() == typeof(System.Web.UI.WebControls.RadioButton))
((RadioButton)c).Checked = false;
foreach (Control child in c.Controls)
ClearControls(child);
}
------------------------------------------------------------------------------------------------------------------------
private void DisableControls(Control c)
{
if ((c is TextBox) || (c is LinkButton) || (c is Button) || (c is CheckBox) || (c is CheckBoxList) || (c is RadioButtonList) || (c is DropDownList) || (c is Panel) || (c is ImageButton))
{
((WebControl)c).Enabled = false;
}
foreach (Control child in c.Controls)
DisableControls(child);
}
Blog that contains articles about new technologies related or not with programming. I will describe and solves some problems that I encounter in my career. ASP .NET, AJAX, Javascript, C++, C# and SQL are some of the subjects that will appear.
Showing posts with label JavaScript. Show all posts
Showing posts with label JavaScript. Show all posts
28 June 2012
javascript handlers
// Add handler using the getElementById method
$addHandler(Sys.UI.DomElement.getElementById("Button1"), "click", toggleCssClassMethod);
// Add handler using the shortcut to the getElementById method
$addHandler($get("Button2"), "click", removeCssClassMethod);
$addHandler(Sys.UI.DomElement.getElementById("Button1"), "click", toggleCssClassMethod);
// Add handler using the shortcut to the getElementById method
$addHandler($get("Button2"), "click", removeCssClassMethod);
25 May 2012
Email validation javascript
function emailCheck(str)
{
var at="@";
var dot=".";
var lat=str.indexOf(at);
var lstr=str.length;
var ldot=str.indexOf(dot);
if (str.indexOf(at)==-1)
return false;
if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
return false;
if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
return false;
if (str.indexOf(at,(lat+1))!=-1)
return false;
if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
return false;
if (str.indexOf(dot,(lat+2))==-1)
return false;
if (str.indexOf(" ")!=-1)
return false;
return true;
}
{
var at="@";
var dot=".";
var lat=str.indexOf(at);
var lstr=str.length;
var ldot=str.indexOf(dot);
if (str.indexOf(at)==-1)
return false;
if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
return false;
if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
return false;
if (str.indexOf(at,(lat+1))!=-1)
return false;
if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
return false;
if (str.indexOf(dot,(lat+2))==-1)
return false;
if (str.indexOf(" ")!=-1)
return false;
return true;
}
29 March 2012
get web.config appsettings in javascript
var conn = '<%=ConfigurationManager.ConnectionStrings["MyConnString"].ConnectionString %>'
alert(conn);
var v1 = '<%=ConfigurationManager.AppSettings["var1"].ToString() %>'
var v1 = '<%=ConfigurationManager.AppSettings["var1"] %>'
<%$AppSettings:Title%>
alert(v1);
alert(conn);
var v1 = '<%=ConfigurationManager.AppSettings["var1"].ToString() %>'
var v1 = '<%=ConfigurationManager.AppSettings["var1"] %>'
<%$AppSettings:Title%>
alert(v1);
28 March 2012
restrict textbox to allow only numerics
function numerics()
{
key = String.fromCharCode(window.event.keyCode);
if (!(key >= '0' && key <= '9')) window.event.keyCode=0;
}
<asp:TextBox ID="txt1" Text="" runat="server" onkeypress=" return numerics();"></asp:TextBox>
function isAlpha(keyCode)
{
return ((keyCode >= 65 && keyCode <= 90) || keyCode == 8||keyCode==37||keyCode==39||keyCode==9||keyCode==46)
}
function isNum(keyCode)
{
return((keyCode < 105 && keyCode > 96)||(keyCode < 57 && keyCode > 48) || keyCode == 8||keyCode==37||keyCode==39||keyCode==9||keyCode==46)
}
<asp:TextBox ID="txtUserName" runat="server" onkeydown = "return isAlpha(event.keyCode);" onpaste = "return false;" Width="130px"></asp:TextBox>
public static bool IsNumeric(String strVal)
{
Regex reg = new Regex("[^0-9-]");
Regex reg2 = new Regex("^-[0-9]+$|^[0-9]+$");
return (!reg.IsMatch(strVal) && reg2.IsMatch(strVal));
}
{
key = String.fromCharCode(window.event.keyCode);
if (!(key >= '0' && key <= '9')) window.event.keyCode=0;
}
<asp:TextBox ID="txt1" Text="" runat="server" onkeypress=" return numerics();"></asp:TextBox>
function isAlpha(keyCode)
{
return ((keyCode >= 65 && keyCode <= 90) || keyCode == 8||keyCode==37||keyCode==39||keyCode==9||keyCode==46)
}
function isNum(keyCode)
{
return((keyCode < 105 && keyCode > 96)||(keyCode < 57 && keyCode > 48) || keyCode == 8||keyCode==37||keyCode==39||keyCode==9||keyCode==46)
}
<asp:TextBox ID="txtUserName" runat="server" onkeydown = "return isAlpha(event.keyCode);" onpaste = "return false;" Width="130px"></asp:TextBox>
public static bool IsNumeric(String strVal)
{
Regex reg = new Regex("[^0-9-]");
Regex reg2 = new Regex("^-[0-9]+$|^[0-9]+$");
return (!reg.IsMatch(strVal) && reg2.IsMatch(strVal));
}
21 March 2012
check all checkboxes in a form
$(document).ready(function() {
$('#chkAll').click(
function() {
$("INPUT[type='checkbox']").attr('checked', $('#chkAll').is(':checked'));
});
});
$('#chkAll').click(
function() {
$("INPUT[type='checkbox']").attr('checked', $('#chkAll').is(':checked'));
});
});
checkbox list javascript validation
function valid()
{
var chkBoxList = document.getElementById('<%=CheckBoxList11.ClientID %>');
var chkBoxCount= chkBoxList.getElementsByTagName("input");
var k=0;
for(var i=0;i<chkBoxCount.length;i++)
{
if(chkBoxCount[i].checked)
k++;
}
if(k==0)
alert("select any item");
return false;
}
{
var chkBoxList = document.getElementById('<%=CheckBoxList11.ClientID %>');
var chkBoxCount= chkBoxList.getElementsByTagName("input");
var k=0;
for(var i=0;i<chkBoxCount.length;i++)
{
if(chkBoxCount[i].checked)
k++;
}
if(k==0)
alert("select any item");
return false;
}
17 November 2011
querystring n gv rows in javascript
query string in js: '<%=Request.QueryString["val"]%>';
--------------------------
gridrows in js: Totalrows = parseInt('<%= this.gv1.Rows.Count %>');
--------------------------
var rows = document.getElementById('gv').rows;
for(i=0;i <rows.length;i++)
{
fd = rows[i].cells[4].innerText;
rows[i].cells[4].style.display="none";
rows[i].cells[8].style.display="none";
}
------------------------------------
<asp:Label ID="lblSno" runat="server" Text="<%# Container.DataItemIndex + 1%>" /> ------------> to get the serial no for the gridview rows after bind the data (take the control to item template field of the grid)
--------------------------
gridrows in js: Totalrows = parseInt('<%= this.gv1.Rows.Count %>');
--------------------------
var rows = document.getElementById('gv').rows;
for(i=0;i <rows.length;i++)
{
fd = rows[i].cells[4].innerText;
rows[i].cells[4].style.display="none";
rows[i].cells[8].style.display="none";
}
------------------------------------
<asp:Label ID="lblSno" runat="server" Text="<%# Container.DataItemIndex + 1%>" /> ------------> to get the serial no for the gridview rows after bind the data (take the control to item template field of the grid)
28 October 2011
validate checkbox list using javascript
function valid()
{
var chkBoxList = document.getElementById('CheckBoxList11');
var chkBoxCount= chkBoxList.getElementsByTagName("input");
var k=0;
for(var i=0;i<chkBoxCount.length;i++)
{
if(chkBoxCount[i].checked)
k++;
}
if(k==0)
alert("select any item");
return false;
}
{
var chkBoxList = document.getElementById('CheckBoxList11');
var chkBoxCount= chkBoxList.getElementsByTagName("input");
var k=0;
for(var i=0;i<chkBoxCount.length;i++)
{
if(chkBoxCount[i].checked)
k++;
}
if(k==0)
alert("select any item");
return false;
}
24 October 2011
add js functions in .cs file in simple
Literal li = new Literal();
li.Text = "alert('hello');";
Page.Controls.Add(li);
------------------------------------
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "", jsfunction(), true);
Page.RegisterStartupScript("new","<script>alert('this is alert');</script>");
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('hai');", true);
Page.ClientScript.RegisterStartupScript(this.GetType(), "", jsfunction();, true);
Page.RegisterClientScriptBlock("a", "<script>jsfunction();</script>");
ScriptManager.RegisterClientScriptBlock(UpdatedatePanel1, this.GetType(), "Message", "Status(true);", true);
ScriptManager
.RegisterStartupScript(Page, Page.GetType(), Guid.NewGuid().ToString(), "$('#bd').attr('className', 'capse');", true);
Button.Attributes.Add("onclick", "return fnGet()");
for !ispostback:..
ClientScriptManager scm = this.ClientScript;
scm.RegisterStartupScript(this.GetType(), "onload", " __doPostBack('form1',null);setInterval(fncheck,10000);", true);
-------------------------------------
Response.Write("<script language="javascript">window.close();</script>");
Response.End();
li.Text = "alert('hello');";
Page.Controls.Add(li);
------------------------------------
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "", jsfunction(), true);
Page.RegisterStartupScript("new","<script>alert('this is alert');</script>");
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('hai');", true);
Page.ClientScript.RegisterStartupScript(this.GetType(), "", jsfunction();, true);
Page.RegisterClientScriptBlock("a", "<script>jsfunction();</script>");
ScriptManager.RegisterClientScriptBlock(UpdatedatePanel1, this.GetType(), "Message", "Status(true);", true);
ScriptManager
.RegisterStartupScript(Page, Page.GetType(), Guid.NewGuid().ToString(), "$('#bd').attr('className', 'capse');", true);
Button.Attributes.Add("onclick", "return fnGet()");
for !ispostback:..
ClientScriptManager scm = this.ClientScript;
scm.RegisterStartupScript(this.GetType(), "onload", " __doPostBack('form1',null);setInterval(fncheck,10000);", true);
-------------------------------------
Response.Write("<script language="javascript">window.close();</script>");
Response.End();
check whether the given value is string or not
public static bool IsNumeric(String strVal)
{
Regex reg = new Regex("[^0-9-]");
Regex reg2 = new Regex("^-[0-9]+$|^[0-9]+$");
return (!reg.IsMatch(strVal) && reg2.IsMatch(strVal));
}
-----------------------------
isNaN(objnumber)
{
Regex reg = new Regex("[^0-9-]");
Regex reg2 = new Regex("^-[0-9]+$|^[0-9]+$");
return (!reg.IsMatch(strVal) && reg2.IsMatch(strVal));
}
-----------------------------
isNaN(objnumber)
get datetime in user friendly (datetime convertion) in sqlserver
select convert (varchar(10), getdate(),101)
------------------------------------------
ddat = new Date();
ddat = new Date(ddat.setDate(new Date(stdate.value).getDate()+parseInt(days)));
var d=newdate.getDate();
var m=(newdate.getMonth()+1);
var y=newdate.getFullYear();
-----------------------------------------
day = new Date()
day = new Date("August15, 2006 08:25:00")
day = new Date(06,8,15)
day = new Date(06,8,15,8,25,0)
Ref: http://msdn.microsoft.com/en-us/library/ms187928.aspx with convertion chart
select convert(varchar, getdate(), 100) convertResult,100 style union
select convert(varchar, getdate(), 101),101 union
select convert(varchar, getdate(), 102),102 union
select convert(varchar, getdate(), 103),103 union
select convert(varchar, getdate(), 104),104 union
select convert(varchar, getdate(), 105),105 union
select convert(varchar, getdate(), 106),106 union
select convert(varchar, getdate(), 107),107 union
select convert(varchar, getdate(), 108),108 union
select convert(varchar, getdate(), 109),109 union
select convert(varchar, getdate(), 110),110 union
select convert(varchar, getdate(), 111),111 union
select convert(varchar, getdate(), 112),112 union
select convert(varchar, getdate(), 113),113 union
select convert(varchar, getdate(), 114),114 union
select convert(varchar, getdate(), 120),120 union
select convert(varchar, getdate(), 121),121 union
select convert(varchar, getdate(), 126),126 union
select convert(varchar, getdate(), 127),127 union
select convert(varchar, getdate(), 130),130 union
select convert(varchar, getdate(), 131),131
order by 2
------------------------------------------
ddat = new Date();
ddat = new Date(ddat.setDate(new Date(stdate.value).getDate()+parseInt(days)));
var d=newdate.getDate();
var m=(newdate.getMonth()+1);
var y=newdate.getFullYear();
-----------------------------------------
day = new Date()
day = new Date("August15, 2006 08:25:00")
day = new Date(06,8,15)
day = new Date(06,8,15,8,25,0)
Ref: http://msdn.microsoft.com/en-us/library/ms187928.aspx with convertion chart
select convert(varchar, getdate(), 100) convertResult,100 style union
select convert(varchar, getdate(), 101),101 union
select convert(varchar, getdate(), 102),102 union
select convert(varchar, getdate(), 103),103 union
select convert(varchar, getdate(), 104),104 union
select convert(varchar, getdate(), 105),105 union
select convert(varchar, getdate(), 106),106 union
select convert(varchar, getdate(), 107),107 union
select convert(varchar, getdate(), 108),108 union
select convert(varchar, getdate(), 109),109 union
select convert(varchar, getdate(), 110),110 union
select convert(varchar, getdate(), 111),111 union
select convert(varchar, getdate(), 112),112 union
select convert(varchar, getdate(), 113),113 union
select convert(varchar, getdate(), 114),114 union
select convert(varchar, getdate(), 120),120 union
select convert(varchar, getdate(), 121),121 union
select convert(varchar, getdate(), 126),126 union
select convert(varchar, getdate(), 127),127 union
select convert(varchar, getdate(), 130),130 union
select convert(varchar, getdate(), 131),131
order by 2
convertResult | style |
Mar 18 2016 5:27AM | 100 |
3/18/2016 | 101 |
2016.03.18 | 102 |
18/03/2016 | 103 |
18.03.2016 | 104 |
18-03-2016 | 105 |
18-Mar-16 | 106 |
18-Mar-16 | 107 |
5:27:19 | 108 |
Mar 18 2016 5:27:19:257AM | 109 |
3/18/2016 | 110 |
3/18/2016 | 111 |
20160318 | 112 |
18 Mar 2016 05:27:19:257 | 113 |
05:27:19:257 | 114 |
3/18/2016 5:27 | 120 |
27:19.3 | 121 |
2016-03-18T05:27:19.257 | 126 |
2016-03-18T05:27:19.257 | 127 |
9 ????? ??????? 1437 5:27:19 | 130 |
9/06/1437 5:27:19:257AM | 131 |
03 October 2011
get the ascii value as well as the typed value in alert
function getval()
{
var keyASCII = window.event.keyCode;
alert(keyASCII);
var keyValue = String.fromCharCode(keyASCII);
alert(keyValue);
}
{
var keyASCII = window.event.keyCode;
alert(keyASCII);
var keyValue = String.fromCharCode(keyASCII);
alert(keyValue);
}
Subscribe to:
Posts (Atom)