In Validations.JS placed the following code.
function validate()
{
if(document.getElementById("ctl00_m_g_739959ea_9aad_4d96_b0d1_5b6615fa3ced_ txtUserName").value=="")
{
document.getElementById("ctl00_m_g_739959ea_9aad_4d96_b0d1_5b6615fa3ced_txt UserName").focus();
alert('Username Required');
return false;
}
if(document.getElementById("ctl00_m_g_739959ea_9aad_4d96_b0d1_5b6615fa3ced_ txtPassWord").value=="")
{
document.getElementById("ctl00_m_g_739959ea_9aad_4d96_b0d1_5b6615fa3ced_txt PassWord").focus();
alert('Password Required');
return false;
}
return true;
_______________________________________
using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using System.Security;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
[assembly:AllowPartiallyTrustedCallers]
namespace TestSampleWebPartRD1
{
[Guid("d0ac89e5-5c41-4f1f-9ae1-5ac6e9f1cf38")]
public class TestSampleWebPartRD1 :
System.Web.UI.WebControls.WebParts.WebPart
{
TextBox txtUserName;
TextBox txtPassWord;
Button btnSave;
string strError = string.Empty;
public TestSampleWebPartRD1()
{
}
protected override void Render(HtmlTextWriter writer)
{
writer.Write(strError);
try
{
writer.Write("<Script Language='JavaScript'
type='Text/JavaScript'
src='http://vlnrm:100/CustomWebParts/Validations.JS'></Script>");
//ScriptLink.Register(this.Page, "Validations.JS", true);
writer.Write("<Table>");
writer.Write("<Tr>");
writer.Write("<Td>");
writer.Write("Enter UserName");
writer.Write("</Td>");
writer.Write("<Td>");
txtUserName.RenderControl(writer);
writer.Write("</Td>");
writer.Write("</Tr>");
writer.Write("<Tr>");
writer.Write("<Td>");
writer.Write("Enter PassWord");
writer.Write("</Td>");
writer.Write("<Td>");
txtPassWord.RenderControl(writer);
writer.Write("</Td>");
writer.Write("</Tr>");
writer.Write("<Tr>");
writer.Write("<Td Colspan='2' Align='Center'>");
btnSave.RenderControl(writer);
writer.Write("</Td>");
writer.Write("</Tr>");
writer.Write("</Table>");
}
catch (Exception ex)
{
writer.Write(ex.ToString());
}
}
protected override void CreateChildControls()
{
try
{
txtUserName = new TextBox();
txtUserName.ID = "txtUserName";
this.Controls.Add(txtUserName);
txtPassWord = new TextBox();
txtPassWord.ID = "txtPassWord";
this.Controls.Add(txtPassWord);
btnSave = new Button();
btnSave.ID = "btnSave";
btnSave.Text = "Save Record";
btnSave.Attributes.Add("onclick", "return validate();");
btnSave.Click += new EventHandler(btnSave_Click);
this.Controls.Add(btnSave);
}
catch (Exception ex)
{
strError+=ex.ToString();
}
}
void btnSave_Click(object sender, EventArgs e)
{
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite("http://vlnrm:100/"))
{
using (SPWeb objWeb = site.OpenWeb())
{
objWeb.AllowUnsafeUpdates = true;
SPList objList = objWeb.Lists["Login"];
SPListItemCollection myColl = objList.Items;
SPListItem item = myColl.Add();
item["Title"] = txtUserName.Text;
item["Password"] = txtPassWord.Text;
item.Update();
txtUserName.Text = "";
txtPassWord.Text = "";
strError += "Record Saved Successfully...!";
}
}
});
}
catch (Exception ex)
{
strError += ex.ToString();
}
}
}
}
function validate()
{
if(document.getElementById("ctl00_m_g_739959ea_9aad_4d96_b0d1_5b6615fa3ced_
{
document.getElementById("ctl00_m_g_739959ea_9aad_4d96_b0d1_5b6615fa3ced_txt
alert('Username Required');
return false;
}
if(document.getElementById("ctl00_m_g_739959ea_9aad_4d96_b0d1_5b6615fa3ced_
{
document.getElementById("ctl00_m_g_739959ea_9aad_4d96_b0d1_5b6615fa3ced_txt
alert('Password Required');
return false;
}
return true;
}
___________________________________________________________________________using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using System.Security;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
[assembly:AllowPartiallyTrustedCallers]
namespace TestSampleWebPartRD1
{
[Guid("d0ac89e5-5c41-4f1f-9ae1-5ac6e9f1cf38")]
public class TestSampleWebPartRD1 :
System.Web.UI.WebControls.WebParts.WebPart
{
TextBox txtUserName;
TextBox txtPassWord;
Button btnSave;
string strError = string.Empty;
public TestSampleWebPartRD1()
{
}
protected override void Render(HtmlTextWriter writer)
{
writer.Write(strError);
try
{
writer.Write("<Script Language='JavaScript'
type='Text/JavaScript'
src='http://vlnrm:100/CustomWebParts/Validations.JS'></Script>");
//ScriptLink.Register(this.Page, "Validations.JS", true);
writer.Write("<Table>");
writer.Write("<Tr>");
writer.Write("<Td>");
writer.Write("Enter UserName");
writer.Write("</Td>");
writer.Write("<Td>");
txtUserName.RenderControl(writer);
writer.Write("</Td>");
writer.Write("</Tr>");
writer.Write("<Tr>");
writer.Write("<Td>");
writer.Write("Enter PassWord");
writer.Write("</Td>");
writer.Write("<Td>");
txtPassWord.RenderControl(writer);
writer.Write("</Td>");
writer.Write("</Tr>");
writer.Write("<Tr>");
writer.Write("<Td Colspan='2' Align='Center'>");
btnSave.RenderControl(writer);
writer.Write("</Td>");
writer.Write("</Tr>");
writer.Write("</Table>");
}
catch (Exception ex)
{
writer.Write(ex.ToString());
}
}
protected override void CreateChildControls()
{
try
{
txtUserName = new TextBox();
txtUserName.ID = "txtUserName";
this.Controls.Add(txtUserName);
txtPassWord = new TextBox();
txtPassWord.ID = "txtPassWord";
this.Controls.Add(txtPassWord);
btnSave = new Button();
btnSave.ID = "btnSave";
btnSave.Text = "Save Record";
btnSave.Attributes.Add("onclick", "return validate();");
btnSave.Click += new EventHandler(btnSave_Click);
this.Controls.Add(btnSave);
}
catch (Exception ex)
{
strError+=ex.ToString();
}
}
void btnSave_Click(object sender, EventArgs e)
{
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite("http://vlnrm:100/"))
{
using (SPWeb objWeb = site.OpenWeb())
{
objWeb.AllowUnsafeUpdates = true;
SPList objList = objWeb.Lists["Login"];
SPListItemCollection myColl = objList.Items;
SPListItem item = myColl.Add();
item["Title"] = txtUserName.Text;
item["Password"] = txtPassWord.Text;
item.Update();
txtUserName.Text = "";
txtPassWord.Text = "";
strError += "Record Saved Successfully...!";
}
}
});
}
catch (Exception ex)
{
strError += ex.ToString();
}
}
}
}
No comments:
Post a Comment