Monday, December 31, 2007

Trim Function in Javascript

Copy and Paste the following in code between <script> tags.

String.prototype.trim = function()
{
return( this.replace(new RegExp("^([\\s]+)([\\s]+)$", "gm"), ""));
};


We can use this function like,

var s = " Success Within You ";
alert(s.trim());


You will get "Success Within You"

Thursday, October 11, 2007

Infragistics WebDateChooser

Set Maximum & Minimum Date on Infragistics WebDateChooser using Javascript.



This is the sample for how to set first webdatechooser's selected date as maximum date of second webdatechooser.



Add the following script function in your Script tag or file.



var today;
function SetMaxDate()
{
var t = document.getElementById('WebDateChooser1').value;
today = new Date();
var dt;
if(t.indexOf('-') != -1)
dt = t.split('-');
else if(t.indexOf('/') != -1)
dt = t.split('/');
var yr = dt[0];
var mn = dt[1];
var dy = dt[2];
today.setFullYear(yr,mn-1,dy);
oWebDateChooser2.setMaxDate(today);
}


function SetDate()
{
oWebDateChooser2.setValue(oWebDateChooser2.getMaxDate());
}





Add the following tag in specific webdatechooser inline.



WebdateChooser1

<ClientSideEvents ValueChanged="SetMaxDate()"/>



WebdateChooser2
<ClientSideEvents BeforeDropDown="SetDate()" />

Thursday, September 27, 2007

Xml File Creation in Asp.Net


This blog is for creating xml file and insert / update values from asp.net page.

clsXMLActions.cs

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.Collections;
///
/// Summary description for XML_Actions
///

public class clsXMLActions
{
XmlDocument xmlDoc = new XmlDocument();
string Path = HttpContext.Current.Server.MapPath("Emp.xml");
public clsXMLActions()
{
}
private void OpenXmlFile()
{
if (System.IO.File.Exists(Path))
{
xmlDoc.Load(Path);
}
else
{
XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);
xmlDoc.AppendChild(xmlDeclaration);
XmlElement root = xmlDoc.CreateElement("Emp_Details");
xmlDoc.AppendChild(root);
}
}
public void InsertValues(Hashtable ht)
{
OpenXmlFile();
XmlNodeList Nodelst = xmlDoc.GetElementsByTagName("Emp_Details");
XmlNode Node;
bool Edited = false;
if (ht.Count > 0)
{
for (int Cnt = 0; Cnt < node =" Nodelst.Item(0).ChildNodes[Cnt];" value =" ht[" value =" ht[" edited =" true;" edited ="="" root =" xmlDoc.DocumentElement;" child1 =" xmlDoc.CreateElement(">Page1.aspx
on Button Click


clsXMLActions obj = new clsXMLActions();
Hashtable ht = new Hashtable();
ht.Add("Id", TextBox1.Text);
ht.Add("Name", TextBox2.Text);
ht.Add("Mobile", TextBox3.Text);
obj.InsertValues(ht);
Page.RegisterStartupScript("s", "<script>alert('Updated Successfully...'); </script>");


Send Values to Popup window with unlimited length - Javascript

Usually passing values to popup window using querystring in client side script. But using querystring we can pass value with 2083 chars limit.

Using Parameter values we can pass more than 4000 chars to popup screen.

Page1.aspx

var winArgs = escape(document.getElementById('txtVal').value);
var winSettings = 'center:yes;resizable:no;help:no;status:no;dialogWidth:250px;dialogHeight:200px';
window.showModalDialog("popup.aspx", winArgs, winSettings);

txtVal is textbox ID.

The escape() function encodes a string, so it can be read on all computers.

popup.aspx

var par = window.dialogArguments;
document.getElementById('txt').value =unescape(par);

Here get the parameter value and assign it into textbox. txt is textbox ID.

The unescape() function decodes a string encoded with escape().


Thursday, September 13, 2007

Context Menu in Asp.Net 2.0 GridView

Right - Click Context Menu in Asp.Net 2.0 GridView

You can download the dll as well as sample for context menu from http://msdn.microsoft.com/msdnmag/issues/05/02/CuttingEdge/
Just include the contextmenu.dll in your toolbox and add context menu control.

To Add menu items as follows,
<cc1:contextmenu id="ContextMenu1" runat="server" autohide="False">
<contextmenuitems>
<cc1:contextmenuitem commandname="Copy" text="Copy" tooltip="Click to Copy">
<cc1:contextmenuitem commandname="Paste" text="Paste" tooltip="Click to Paste">
</contextmenuitems>
</cc1:ContextMenu>

In Your Page Html script include the following code in body,
onkeypress="<% = ContextMenu1.GetEscReference() %>"
onclick="<% = ContextMenu1.GetOnClickReference() %>"


The above code is used to hide the context menu while Escape button was clicking as well as left clicking anywhere in the page.

For Button, Add this attribute in button script
oncontextmenu ="<% = ContextMenu1.GetMenuReference() %>"

For Grid-View Row, You should add script funtion ,
<script language="javascript" type="text/javascript">
function CallContextMenu()
{
<% = ContextMenu1.GetMenuReference()%>
}
</script>

In Code-Behind , Add the following code on RowDataBound Event,
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("oncontextmenu", "javascript:ff();return false;");
}

Now we can get our own context menu in Gridview control.


Tuesday, August 7, 2007

Dynamically change background color of Header in Menu.

Set Background Color of Header in Menu

var Prev;
function HeaderFocus(curr)
// curr - Current menu to be painted.
{
if(Prev != null)
document.getElementById(Prev.id).style.background = 'transparent';
document.getElementById(curr.id).style.background = 'red';
Prev = curr; // Temporarily store current menu for next time modify.
}