Tuesday, December 18, 2007

Javascript Calling from Page behind in asp.net

Its a sample javascript which is called from page behind

In .Aspx File

<head runat="server">
<title>Untitled Page</title>
<script language="javascript" type="text/javascript">

function IsChecked(chkObj1,strText,strFormName)
{
if(strText == "")
return;

strText = strText.replace( '<b>', '');
strText = strText.replace( '</b>', '');

var chkObj = document.getElementById(chkObj1);
var txtObj = document.getElementById('<%= TextBox1.ClientID %>');
if(chkObj.checked == true) //For Adding Item
{
var iFormFound = 0;
var iTextFound = 0;
iFormFound = txtObj.value.search(strFormName);
if(iFormFound == -1)
{
//If Form Name is Not There Append it
if(txtObj.value.length == 0)
txtObj.value = txtObj.value + strFormName;
else
txtObj.value = txtObj.value + "\n\n" + strFormName;
}

//If Text Not There Append it After Form Name
iTextFound = txtObj.value.search(strText);
if(iTextFound == -1)
{
if(txtObj.value.length == 0)
{
//txtObj.value = strText;
var iIndexOf = txtObj.value.indexOf(strFormName);
iIndexOf = iIndexOf + strFormName.length;
//alert(iIndexOf);
var strBefore = txtObj.value.substr(0,iIndexOf);
var strAfter = txtObj.value.substr(iIndexOf,txtObj.value.length);
txtObj.value = strBefore + strText + strAfter;
//alert(strBefore + " => " + strText + " => " + strAfter + " => " + txtObj.value);
}
else
{
//txtObj.value = txtObj.value + "\n" + strText;
var iIndexOf = txtObj.value.indexOf(strFormName);
iIndexOf = iIndexOf + strFormName.length;
//alert(iIndexOf);
var strBefore = txtObj.value.substr(0,iIndexOf);
var strAfter = txtObj.value.substr(iIndexOf,txtObj.value.length);
txtObj.value = strBefore + "\n" + strText + strAfter;
//alert(strBefore + " => " + strText + " => " + strAfter + " => " + txtObj.value);
}
}

}
else //For Removing Item
{
var iTextFound = 0;
iTextFound = txtObj.value.search(strText);
if(iTextFound != -1)
{
txtObj.value = txtObj.value.replace( strText + "\r\n", '');
//For Last Element (Special Case)
if(txtObj.value.search(strText) != -1)
{
txtObj.value = txtObj.value.replace( strText, '');
txtObj.value = txtObj.value.substr(0,txtObj.value.length - 2);
}
}
}
}

</script>
</head>



In .Aspx.Cs File (asp.net Code behind file)

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
ApplyJavascript();
}
}

private void ApplyJavascript()
{
CheckBox1.Attributes.Add("onClick", "IsChecked('" + CheckBox1.ClientID.ToString() + "','Group1','Checkbox1')");
CheckBox2.Attributes.Add("onClick", "IsChecked('" + CheckBox2.ClientID.ToString() + "','Group1','Checkbox2')");
CheckBox3.Attributes.Add("onClick", "IsChecked('" + CheckBox3.ClientID.ToString() + "','Group1','Checkbox3')");

CheckBox4.Attributes.Add("onClick", "IsChecked('" + CheckBox4.ClientID.ToString() + "','Group2','Checkbox4')");
CheckBox5.Attributes.Add("onClick", "IsChecked('" + CheckBox5.ClientID.ToString() + "','Group2','Checkbox5')");
CheckBox6.Attributes.Add("onClick", "IsChecked('" + CheckBox6.ClientID.ToString() + "','Group2','Checkbox6')");
CheckBox7.Attributes.Add("onClick", "IsChecked('" + CheckBox7.ClientID.ToString() + "','Group2','Checkbox7')");
}

No comments:

Most Recent Post

Subscribe Blog via Email

Enter your email address:



Disclaimers:We have tried hard to provide accurate information, as a user, you agree that you bear sole responsibility for your own decisions to use any programs, documents, source code, tips, articles or any other information provided on this Blog.
Page copy protected against web site content infringement by Copyscape