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.