Uploaded by Sumit Badugu

3020 awp p10 (1)

advertisement
T.Y.I.T – Semester –V – P-III- Advanced Web Programming
IT-2011
Practical No 10: Working with AJAX and XML
a. Create Web application demonstrate reading and writing operation with XML
Read from XML file
GUI:
Source Code
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="webform.aspx.cs"
Inherits="prac10a.webform" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Read
from XML" />
<br />
<br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
webform.aspx.cs
using
using
using
using
using
using
using
using
System;
System.Collections.Generic;
System.Linq;
System.Web;
System.Web.UI;
System.Web.UI.WebControls;
System.Xml;
System.IO;
namespace prac10a
{
public partial class webform : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
T.Y.I.T – Semester –V – P-III- Advanced Web Programming
IT-2011
}
protected void Button1_Click(object sender, EventArgs e)
{
string file = Path.Combine(Request.PhysicalApplicationPath, @"Product.xml");
FileStream fs = new FileStream(file, FileMode.Open);
XmlTextReader r = new XmlTextReader(fs);
StringWriter writer = new StringWriter();
while(r.Read())
{
if (r.NodeType == XmlNodeType.Whitespace) continue;
writer.Write("<b>Type:</b> ");
writer.Write(r.NodeType.ToString());
writer.Write("<br>");
if(r.Name != " ")
{
writer.Write("<b>Name:</b> ");
writer.Write(r.Name);
writer.Write("<br>");
}
if (r.Value != " ")
{
writer.Write("<b>Value:</b> ");
writer.Write(r.Value);
writer.Write("<br>");
}
if (r.AttributeCount > 0)
{
writer.Write("<b>Attributes:</b> ");
for (int i = 0; i < r.AttributeCount; i++)
{
writer.Write(" ");
writer.Write(r.GetAttribute(i));
writer.Write(" ");
}
writer.Write("<br>");
}
writer.Write("<br>");
}
fs.Close();
Label1.Text = writer.ToString();
}
}
}
Output
T.Y.I.T – Semester –V – P-III- Advanced Web Programming
(Create an XML file)
GUI:
IT-2011
T.Y.I.T – Semester –V – P-III- Advanced Web Programming
IT-2011
Source Code
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="webform 2.aspx.cs"
Inherits="prac10a.webform_2" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Create
XML" />
<br />
<br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
webform2.aspx.cs
using
using
using
using
using
using
using
using
System;
System.Collections.Generic;
System.Linq;
System.Web;
System.Web.UI;
System.Web.UI.WebControls;
System.Xml;
System.IO;
namespace prac10a
{
public partial class webform_2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string file = Path.Combine(Request.PhysicalApplicationPath,
@"App_Data\Product2.xml");
FileStream fs = new FileStream(file, FileMode.Create);
XmlTextWriter w = new XmlTextWriter(fs, null);
T.Y.I.T – Semester –V – P-III- Advanced Web Programming
w.WriteStartDocument();
w.WriteStartElement("ProductList");
w.WriteComment("This file is generated by the XmlTextWriter class");
w.WriteStartElement("Product");
w.WriteAttributeString("ID", "1");
w.WriteAttributeString("Name", "Chair");
w.WriteStartElement("Price");
w.WriteString("49.33");
w.WriteEndElement();
w.WriteEndElement();
w.WriteStartElement("Product");
w.WriteAttributeString("ID", "2");
w.WriteAttributeString("Name", "Car");
w.WriteStartElement("Price");
w.WriteString("495564.33");
w.WriteEndElement();
w.WriteEndElement();
w.WriteStartElement("Product");
w.WriteAttributeString("ID", "3");
w.WriteAttributeString("Name", "Fresh fruit basket");
w.WriteStartElement("Price");
w.WriteString("490.33");
w.WriteEndElement();
w.WriteEndElement();
w.WriteEndElement();
w.WriteEndDocument();
w.Close();
Response.Write("XL File created successfuly...Check the content in
Product2.xml file");
}
}
}
Output:
IT-2011
T.Y.I.T – Semester –V – P-III- Advanced Web Programming
IT-2011
b. Create a web application to demonstrate Form Security and Window security with proper
Authentication and Authorization properties.
GUI:
Source Code
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="webform.aspx.cs"
Inherits="prac10b.webform" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Lbl_Name" runat="server" Text="Name"></asp:Label>
              &nbsp
;    
<asp:TextBox ID="Txt_Name" runat="server"></asp:TextBox>
<br />
T.Y.I.T – Semester –V – P-III- Advanced Web Programming
IT-2011
<asp:Label ID="Lbl_Password" runat="server" Text="Password"></asp:Label>
             
<asp:TextBox ID="Txt_Password" runat="server"
TextMode="Password"></asp:TextBox>
<br />
<br />
<asp:CheckBox ID="CheckBox1" runat="server" Text="Remember Me" />
<br />
<br />
<asp:Button ID="Btn_Login" runat="server" OnClick="Btn_Login_Click"
Text="Login" />
              &nbsp
; 
<asp:Button ID="Btn_Reset" runat="server" Text="Reset" />
<br />
<br />
<asp:Label ID="Lbl_Msg" runat="server" Text="Label"></asp:Label>
<br />
</div>
</form>
</body>
</html>
welcome.aspx
Webconfig
<configuration>
<system.web>
<authentication mode="Forms">
<forms loginUrl="webform.aspx" defaultUrl="welcome.aspx">
<credentials passwordFormat="Clear">
<user name="janhvi" password="janhvi123"/>
<user name="sourabh" password="sourabh123"/>
</credentials>
</forms>
</authentication>
</system.web>
</configuration>
Webform.aspx.cs
using
using
using
using
using
using
using
System;
System.Collections.Generic;
System.Linq;
System.Web;
System.Web.UI;
System.Web.UI.WebControls;
System.Web.Security;
namespace prac10b
{
T.Y.I.T – Semester –V – P-III- Advanced Web Programming
IT-2011
public partial class webform : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Btn_Login_Click(object sender, EventArgs e)
{
if(FormsAuthentication.Authenticate(Txt_Name.Text, Txt_Password.Text))
{
FormsAuthentication.RedirectFromLoginPage(Txt_Name.Text,
CheckBox1.Checked);
}
else
{
Lbl_Msg.Text = "Invalid Name or Password";
}
}
}
}
Output:
c. Create a web application to demonstrate use of various Ajax controls.
GUI:
T.Y.I.T – Semester –V – P-III- Advanced Web Programming
IT-2011
Source Code
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm.aspx.cs"
Inherits="prac7c.WebForm" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Lbl_FNo" runat="server" Text="Flight_No"></asp:Label>
               &nbsp
;
<asp:TextBox ID="Txt_FNo" runat="server"></asp:TextBox>
<br />
<asp:Label ID="Lbl_FName" runat="server" Text="Flight_Name"></asp:Label>
            
<asp:TextBox ID="Txt_FName" runat="server"></asp:TextBox>
<br />
<br />
<asp:Button ID="Btn_Insert" runat="server" Text="Insert into Database" />
            
<asp:Button ID="Btn_Delete" runat="server" Text="Delete from Database" />
<br />
<asp:Label ID="Lbl_Show" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
Webconfig
<configuration>
<connectionStrings>
<add name="constr" connectionString="Data Source=VM;Initial Catalog=Airlinesss;Integrated
Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>
<compilation debug="true"/></system.web></configuration>
Webform.aspx.cs
using System;
T.Y.I.T – Semester –V – P-III- Advanced Web Programming
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace prac7cdata
{
public partial class webform : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Btn_Insert_Click(object sender, EventArgs e)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
SqlConnection con = new SqlConnection(constr);
string InsertQuery = "insert into TblAirlines values(@Flight_No, @Flight_Name)";
SqlCommand cmd = new SqlCommand(InsertQuery, con);
cmd.Parameters.AddWithValue("@Flight_No",Txt_FNo.Text);
cmd.Parameters.AddWithValue("@Flight_Name",Txt_FName.Text);
con.Open();
cmd.ExecuteNonQuery();
Lbl_Show.Text = "Record Inserted Successfully.";
con.Close();
}
protected void Btn_Delete_Click(object sender, EventArgs e)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
SqlConnection con = new SqlConnection(constr);
string InsertQuery = "delete from TblAirlines where Flight_No=@Flight_No";
SqlCommand cmd = new SqlCommand(InsertQuery, con);
cmd.Parameters.AddWithValue("@Flight_No", Txt_FNo.Text);
con.Open();
cmd.ExecuteNonQuery();
Lbl_Show.Text = "Record Deleted Successfuly.";
con.Close();
}
}
}
Output
IT-2011
T.Y.I.T – Semester –V – P-III- Advanced Web Programming
IT-2011
Download