<%
@ Control Language="C#" %><
table id="tblManutencaoTecnologia" style="width: 100%; height: 328px; class="ms-formbody"
class="ms-formbody" ><
tr> <td valign="top" class="ms-formbody"> <asp:ListBox runat="server" id="lbLista" Height="320px" Width="308px" AutoPostBack="True" Rows="8" CssClass="ms-formline" CausesValidation="True"> </asp:ListBox> </td> <td valign="top" class="ms-formbody" > <table style="width: 100%; height: 316px; " class="ms-formlabel" > <td class="ms-formlabel" colspan="0" >C¢digo
</td> <tr> <td colspan="0" > <asp:TextBox runat="server" id="tbCodigo" Width="70px" MaxLength="9" CssClass="ms-inputuserfield" ></asp:TextBox> </td> </tr> <tr> <td class="ms-formlabel" colspan="0" >Nome
</td> </tr> <tr> <td class="ms-formlabel" colspan="0" > <asp:TextBox runat="server" id="tbNomeTecnologia" Width="400px" MaxLength="60" CssClass="ms-inputuserfield" ></asp:TextBox> </td> </tr> <tr> <td class="ms-formlabel" colspan="0" >Quantidade de Horas para Ponto de Fun‡Æo Baixa
</td> </tr> <tr> <td colspan="0" > <asp:TextBox runat="server" id="tbQtdeHorasPFBaixa" Width="70px" MaxLength="5" CssClass="ms-inputuserfield" ></asp:TextBox> </td> </tr> <tr> <td class="ms-formlabel" colspan="0" >Quantidade de Horas para Ponto de Fun‡Æo M‚dia
</td> </tr> <tr> <td colspan="0"> <asp:TextBox runat="server" id="tbQtdeHorasPFMedia" Width="70px" MaxLength="5" CssClass="ms-inputuserfield" ></asp:TextBox> </td> </tr> <tr> <td class="ms-formlabel" colspan="0" >Quantidade de Horas para Ponto de Fun‡Æo Alta
</td> </tr> <tr> <td colspan="0" > <asp:TextBox runat="server" id="tbQtdeHorasPFAlta" Width="70px" MaxLength="5" CssClass="ms-inputuserfield" ></asp:TextBox> </td> </tr> <tr> <td class="ms-formlabel" colspan="0" >Indicador
</td> </tr> <tr> <td bgcolor="White" colspan="0" class="ms-formbody" > <asp:CheckBox runat="server" id="cbIndicador" Checked="True" Text="Ativo" CssClass="ms-inputuserfield" /> </td> </tr> </table> </td></
tr> <td style="width: 195px" valign="top" class="ms-formbody"> <table> <td style="height: 18px" > <table style="width: 100%"> <td> <asp:Button runat="server" Text="?" id="btnPesquisar" Width="17px" Font-Size="XX-Small" Height="20px" CssClass="ms-splinkbuttonactive" /> </td> <td> <asp:TextBox runat="server" id="tbPesquisar" Width="253px" MaxLength="60" Font-Size="XX-Small" CssClass="ms-inputuserfield"></asp:TextBox> </td> <td> <asp:Button runat="server" Text="+" id="btnIncluir" Font-Size="XX-Small" Height="20px" Width="17px" CssClass="ms-splinkbuttonactive" /> </td> </table> </td> </table> <table style="width: 156%; height: 10px;" class="ms-formlabel"> <td align="center" > <table style="width: 100%"> <td align="left"> <asp:Button runat="server" Text="Salvar" id="btnSalvar" Width="84px" Font-Size="XX-Small" CssClass="ms-splinkbuttonactive" /> </td> <td align="center"> <asp:Button runat="server" Text="Cancelar" id="btnCancelar" Width="84px" Font-Size="XX-Small" CssClass="ms-splinkbuttonactive" /> </td> <td align="right"> <asp:Button runat="server" Text="Excluir" id="btnExcluir" Width="84px" Font-Size="XX-Small" CssClass="ms-splinkbuttonactive" /> </td> </table> </td> </table> </td> <td valign="top" class="ms-formbody"> <asp:Label runat="server" id="lblMensagem" CssClass="ms-error" ></asp:Label> </td></
table>Código Fonte em C#
using
System;using
System.Collections.Generic;using
System.Linq;using
System.Text;using
System.Web.UI;using
System.Web.UI.WebControls;using
System.Web.UI.WebControls.WebParts;using
Microsoft.SharePoint;using
Microsoft.SharePoint.WebControls;using
Microsoft.SharePoint.Utilities;using
System.Data;using
System.Drawing;using
System.Xml.Serialization;namespace
CAFE.NET{
#region
Atributos /// <summary> /// Controles Padräes /// </summary> private Control userControl; private ListBox lbLista; private TextBox tbPesquisar; private Button btnPesquisar; private Button btnIncluir; private Button btnSalvar; private Button btnCancelar; private Button btnExcluir; private Label lblMensagem; /// <summary> /// Controles Manuten‡Æo Tecnologia /// </summary> private TextBox tbCodigo; private TextBox tbNomeTecnologia; private TextBox tbQtdeHorasPFBaixa; private TextBox tbQtdeHorasPFMedia; private TextBox tbQtdeHorasPFAlta; private CheckBox cbIndicador; private UpdatePanel oUpdatePanel; private ScriptManager oScriptManager;#endregion
#region
Propriedades#endregion
#region
Construtor public WebPartManutencaoTecnologia(){
this.Title = "Manuten‡Æo de Tecnologias"; this.ExportMode = WebPartExportMode.All;}
#endregion
#region
M‚todos /// <summary> /// CreateChildControls /// </summary> protected override void CreateChildControls(){
try{
base.CreateChildControls();EnsurePanelFix();
this.userControl = this.Page.LoadControl(@"/_controltemplates/ucManutencaoTecnologia.ascx");FindUserControl();
oScriptManager =
new ScriptManager();oUpdatePanel =
new UpdatePanel();oUpdatePanel.ID =
"oUpdatePanel";oUpdatePanel.UpdateMode =
UpdatePanelUpdateMode.Conditional;oUpdatePanel.ChildrenAsTriggers =
true;oUpdatePanel.ContentTemplateContainer.Controls.Add(
this.userControl); this.Controls.Add(oScriptManager); this.Controls.Add(oUpdatePanel); if (!Page.IsPostBack){
lbLista.Enabled =
true;btnIncluir.Enabled =
true;btnExcluir.Enabled =
true;PopularLista();
}
}
catch (Exception ex){
lblMensagem.Text = ex.ToString();
}
}
/// <summary> /// EnsurePanelFix - AJAX /// </summary> private void EnsurePanelFix(){
if (this.Page.Form != null){
string formOnSubmitAtt = this.Page.Form.Attributes["onsubmit"]; if (formOnSubmitAtt == "return _spFormOnSubmitWrapper();"){
this.Page.Form.Attributes["onsubmit"] = "_spFormOnSubmitWrapper();";}
}
ScriptManager.RegisterStartupScript(this, typeof(WebPartManutencaoTecnologia), "UpdatePanelFixup", "_spOriginalFormAction = document.forms[0].action; _spSuppressFormOnSubmitWrapper=true;", true);}
/// <summary> /// FindUserControl /// </summary> private void FindUserControl(){
try{
// Controles PadräeslbLista = (
ListBox)this.userControl.FindControl("lbLista");lbLista.AutoPostBack =
true;lbLista.SelectedIndexChanged +=
new EventHandler(lbLista_SelectedIndexChanged);tbPesquisar = (
TextBox)this.userControl.FindControl("tbPesquisar");tbPesquisar.ToolTip =
"Conte£do … Pesquisar";btnPesquisar = (
Button)this.userControl.FindControl("btnPesquisar");btnPesquisar.ToolTip =
"Pesquisar";btnPesquisar.Click +=
new EventHandler(btnPesquisar_Click);btnIncluir = (
Button)this.userControl.FindControl("btnIncluir");btnIncluir.ToolTip =
"Incluir";btnIncluir.Click +=
new EventHandler(btnIncluir_Click);btnSalvar = (
Button)this.userControl.FindControl("btnSalvar");btnSalvar.Click +=
new EventHandler(btnSalvar_Click);btnCancelar = (
Button)this.userControl.FindControl("btnCancelar");btnCancelar.Click +=
new EventHandler(btnCancelar_Click);btnExcluir = (
Button)this.userControl.FindControl("btnExcluir");btnExcluir.Click +=
new EventHandler(btnExcluir_Click);lblMensagem = (
Label)this.userControl.FindControl("lblMensagem");// Controles Manuten‡Æo Tecnologia
tbCodigo = (
TextBox)this.userControl.FindControl("tbCodigo");tbCodigo.ReadOnly =
true;tbNomeTecnologia = (
TextBox)this.userControl.FindControl("tbNomeTecnologia");tbQtdeHorasPFBaixa = (
TextBox)this.userControl.FindControl("tbQtdeHorasPFBaixa");tbQtdeHorasPFMedia = (
TextBox)this.userControl.FindControl("tbQtdeHorasPFMedia");tbQtdeHorasPFAlta = (
TextBox)this.userControl.FindControl("tbQtdeHorasPFAlta");cbIndicador = (
CheckBox)this.userControl.FindControl("cbIndicador");}
catch (Exception ex){
lblMensagem.Text = ex.ToString();
}
}
/// <summary> /// PopularLista /// </summary> private void PopularLista(){
try{
lblMensagem.Text =
string.Empty; using (SPWeb oWeb = SPContext.Current.Site.AllWebs[SPContext.Current.Web.Name]){
string strOrderBy = "<OrderBy><FieldRef Name=\"Title\" /></OrderBy>"; SPList oList = oWeb.Lists["Tecnologia"]; SPQuery oSPQuery = new SPQuery();oSPQuery.Query = strOrderBy;
SPListItemCollection oSPListItemCollection = oList.GetItems(oSPQuery);lbLista.DataSource = oSPListItemCollection;
lbLista.DataTextField =
"Title";lbLista.DataValueField =
"ID";lbLista.DataBind();
if (lbLista.Items.Count == 0){
lbLista.SelectedIndex = -1;
LimparCampos();
}
else{
lbLista.SelectedIndex = 0;
SPListItem oSPListItem = oSPListItemCollection[0]; string id = oSPListItem["ID"].ToString();PopularDados(id);
}
}
}
catch (Exception ex){
lblMensagem.Text = ex.ToString();
}
}
/// <summary> /// PopularDados /// </summary> /// <param name="id"></param> private void PopularDados(string id){
try{
using (SPWeb oWeb = SPContext.Current.Site.AllWebs[SPContext.Current.Web.Name]){
string strQuery = "<Where><Eq><FieldRef Name=\"ID\" />" + "<Value Type=\"Number\">" +id +
"</Value></Eq></Where>"; SPList oList = oWeb.Lists["Tecnologia"]; SPQuery oSPQuery = new SPQuery();oSPQuery.Query = strQuery;
SPListItemCollection oSPListItemCollection = oList.GetItems(oSPQuery); if (oSPListItemCollection.Count > 0){
foreach (SPListItem oSPListItem in oSPListItemCollection){
tbCodigo.Text = oSPListItem[
"ID"].ToString();tbNomeTecnologia.Text = oSPListItem[
"Title"].ToString();tbQtdeHorasPFBaixa.Text = oSPListItem[
"Quantidade Horas Ponto Fun‡Æo Baixa"].ToString();tbQtdeHorasPFMedia.Text = oSPListItem[
"Quantidade Horas Ponto Fun‡Æo M‚dia"].ToString();tbQtdeHorasPFAlta.Text = oSPListItem[
"Quantidade Horas Ponto Fun‡Æo Alta"].ToString();cbIndicador.Checked = (
Boolean)oSPListItem["Ativo"];}
for (int i=0; i < lbLista.Items.Count; i++){
if (lbLista.Items[i].Value == tbCodigo.Text){
lbLista.SelectedIndex = i;
break;}
}
}
else{
PopularLista();
}
}
}
catch (Exception ex){
lblMensagem.Text = ex.ToString();
}
}
/// <summary> /// LimparCampos /// </summary> private void LimparCampos(){
tbCodigo.Text =
string.Empty;tbNomeTecnologia.Text =
string.Empty;tbQtdeHorasPFBaixa.Text =
string.Empty;tbQtdeHorasPFMedia.Text =
string.Empty;tbQtdeHorasPFAlta.Text =
string.Empty;cbIndicador.Checked =
true;}
/// <summary> /// ValidarCampos /// </summary> /// <returns></returns> private bool ValidarCampos(int id){
bool bRet = true; StringBuilder strb = new StringBuilder(); try{
if (tbNomeTecnologia.Text.Trim().Length == 0){
strb.Append(
"--> Nome nÆo informado. <BR>");}
else{
int idValidarNome = ValidarNome(id, tbNomeTecnologia.Text.Trim()); if (idValidarNome != id){
strb.Append(
"--> Nome j existente. <BR>");}
}
if ( tbQtdeHorasPFBaixa.Text.Trim().Length == 0){
strb.Append(
"--> Quantidade de Horas PF Baixa nÆo informada. <BR>");}
else{
try{
int qtde = Convert.ToInt32(tbQtdeHorasPFBaixa.Text); if (qtde <= 0){
strb.Append(
"--> Quantidade de Horas PF Baixa nÆo informada. <BR>");}
}
catch{
strb.Append(
"--> Quantidade de Horas PF Baixa inv lida. <BR>");}
}
if (tbQtdeHorasPFMedia.Text.Trim().Length == 0){
strb.Append(
"--> Quantidade de Horas PF M‚dia nÆo informada. <BR>");}
else{
try{
int qtde = Convert.ToInt32(tbQtdeHorasPFMedia.Text); if (qtde <= 0){
strb.Append(
"--> Quantidade de Horas PF M‚dia nÆo informada. <BR>");}
}
catch{
strb.Append(
"--> Quantidade de Horas PF M‚dia inv lida. <BR>");}
}
if (tbQtdeHorasPFAlta.Text.Trim().Length == 0){
strb.Append(
"--> Quantidade de Horas PF Alta nÆo informada. <BR>");}
else{
try{
int qtde = Convert.ToInt32(tbQtdeHorasPFAlta.Text); if (qtde <= 0){
strb.Append(
"--> Quantidade de Horas PF Alta nÆo informada. <BR>");}
}
catch{
strb.Append(
"--> Quantidade de Horas PF Alta inv lida. <BR>");}
}
if (strb.Length > 0){
bRet =
false;lblMensagem.Text = strb.ToString();
}
}
catch (Exception ex){
lblMensagem.Text = ex.ToString();
}
return bRet;}
/// <summary> /// PesquisarNome /// </summary> /// <param name="nome"></param> /// <returns></returns> private int PesquisarNome(string nome){
int iRet = 0; try{
using (SPWeb oWeb = SPContext.Current.Site.AllWebs[SPContext.Current.Web.Name]){
string strQuery = "<Where><Contains><FieldRef Name=\"Title\" />" + "<Value Type=\"Text\">" +nome.Trim() +
"</Value></Contains></Where>"; SPList oList = oWeb.Lists["Tecnologia"]; SPQuery oSPQuery = new SPQuery();oSPQuery.Query = strQuery;
SPListItemCollection oSPListItemCollection = oList.GetItems(oSPQuery); foreach (SPListItem oSPListItem in oSPListItemCollection){
iRet =
Convert.ToInt32(oSPListItem["ID"]); break;}
}
PopularDados(iRet.ToString());
}
catch (Exception ex){
lblMensagem.Text = ex.ToString();
}
return iRet;}
/// <summary> /// ValidarNome /// </summary> /// <param name="nome"></param> /// <returns></returns> private int ValidarNome(int id, string nome)
{
int iRet = id; try{
using (SPWeb oWeb = SPContext.Current.Site.AllWebs[SPContext.Current.Web.Name]){
string strQuery = "<Where><Eq><FieldRef Name=\"Title\" />" + "<Value Type=\"Text\">" +nome.Trim() +
"</Value></Eq></Where>"; SPList oList = oWeb.Lists["Tecnologia"]; SPQuery oSPQuery = new SPQuery();oSPQuery.Query = strQuery;
SPListItemCollection oSPListItemCollection = oList.GetItems(oSPQuery); foreach (SPListItem oSPListItem in oSPListItemCollection){
if (Convert.ToInt32(oSPListItem["ID"]) != id){
iRet =
Convert.ToInt32(oSPListItem["ID"]); break;}
}
}
}
catch (Exception ex){
lblMensagem.Text = ex.ToString();
}
return iRet;}
/// <summary> /// SalvarDados /// </summary> /// <param name="nome"></param> /// <returns></returns> private int SalvarDados(int id){
int iRet = id; try{
using (SPWeb oWeb = SPContext.Current.Site.AllWebs[SPContext.Current.Web.Name]){
string strQuery = "<Where><Eq><FieldRef Name=\"ID\" />" + "<Value Type=\"Number\">" +id +
"</Value></Eq></Where>"; SPList oList = oWeb.Lists["Tecnologia"]; SPQuery oSPQuery = new SPQuery();oSPQuery.Query = strQuery;
SPListItemCollection oSPListItemCollection = oList.GetItems(oSPQuery); if (oSPListItemCollection.Count > 0){
foreach (SPListItem oSPListItem in oSPListItemCollection){
oSPListItem[
"Title"] = tbNomeTecnologia.Text.Trim();oSPListItem[
"Quantidade Horas Ponto Fun‡Æo Baixa"] = Convert.ToInt32(tbQtdeHorasPFBaixa.Text);oSPListItem[
"Quantidade Horas Ponto Fun‡Æo M‚dia"] = Convert.ToInt32(tbQtdeHorasPFMedia.Text);oSPListItem[
"Quantidade Horas Ponto Fun‡Æo Alta"] = Convert.ToInt32(tbQtdeHorasPFAlta.Text);oSPListItem[
"Ativo"] = cbIndicador.Checked;oSPListItem.Update();
}
}
else{
SPListItem oSPListItem = oSPListItemCollection.Add();oSPListItem[
"Title"] = tbNomeTecnologia.Text.Trim();oSPListItem[
"Quantidade Horas Ponto Fun‡Æo Baixa"] = Convert.ToInt32(tbQtdeHorasPFBaixa.Text);oSPListItem[
"Quantidade Horas Ponto Fun‡Æo M‚dia"] = Convert.ToInt32(tbQtdeHorasPFMedia.Text);oSPListItem[
"Quantidade Horas Ponto Fun‡Æo Alta"] = Convert.ToInt32(tbQtdeHorasPFAlta.Text);oSPListItem[
"Ativo"] = cbIndicador.Checked;oSPListItem.Update();
id = -1;
}
}
}
catch (Exception ex){
lblMensagem.Text = ex.ToString();
}
return iRet;}
/// <summary> /// ExcluirDados /// </summary> /// <param name="nome"></param> /// <returns></returns> private int ExcluirDados(int id){
int iRet = id; try{
using (SPWeb oWeb = SPContext.Current.Site.AllWebs[SPContext.Current.Web.Name]){
string strQuery = "<Where><Eq><FieldRef Name=\"ID\" />" + "<Value Type=\"Number\">" +id +
"</Value></Eq></Where>"; SPList oList = oWeb.Lists["Tecnologia"]; SPQuery oSPQuery = new SPQuery();oSPQuery.Query = strQuery;
SPListItemCollection oSPListItemCollection = oList.GetItems(oSPQuery); if (oSPListItemCollection.Count > 0){
foreach (SPListItem oSPListItem in oSPListItemCollection){
oSPListItem[
"Title"] = tbNomeTecnologia.Text.Trim();oSPListItem[
"Quantidade Horas Ponto Fun‡Æo Baixa"] = Convert.ToInt32(tbQtdeHorasPFBaixa.Text);oSPListItem[
"Quantidade Horas Ponto Fun‡Æo M‚dia"] = Convert.ToInt32(tbQtdeHorasPFMedia.Text);oSPListItem[
"Quantidade Horas Ponto Fun‡Æo Alta"] = Convert.ToInt32(tbQtdeHorasPFAlta.Text);oSPListItem[
"Ativo"] = cbIndicador.Checked;oSPListItem.Delete();
}
}
else{
lblMensagem.Text =
"--> ExclusÆo inexistente.";id = -1;
}
}
}
catch (Exception ex){
lblMensagem.Text = ex.ToString();
}
return iRet;}
#endregion
#region
Eventos /// <summary> /// btnPesquisar_Click /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnPesquisar_Click(object sender, EventArgs e){
PesquisarNome(tbPesquisar.Text);
}
/// <summary> /// btnIncluir_Click /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnIncluir_Click(object sender, EventArgs e){
try{
lbLista.Enabled =
false;btnIncluir.Enabled =
false;btnExcluir.Enabled =
false;LimparCampos();
lbLista.SelectedIndex = -1;
}
catch (Exception ex){
lblMensagem.Text = ex.ToString();
}
}
/// <summary> /// btnSalvar_Click /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnSalvar_Click(object sender, EventArgs e){
int idValue = 0; if (lbLista.SelectedIndex >= 0){
idValue =
Convert.ToInt32(lbLista.SelectedValue);}
else{
idValue = -1;
}
if (ValidarCampos(idValue) == true){
SalvarDados(idValue);
lbLista.Enabled =
true;btnIncluir.Enabled =
true;btnExcluir.Enabled =
true;PopularLista();
}
}
/// <summary> /// btnCancelar_Click /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnCancelar_Click(object sender, EventArgs e){
lbLista.Enabled =
true;btnIncluir.Enabled =
true;btnExcluir.Enabled =
true;PopularLista();
}
/// <summary> /// btnExcluir_Click /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnExcluir_Click(object sender, EventArgs e){
int idValue = 0; if (lbLista.SelectedIndex >= 0){
idValue =
Convert.ToInt32(lbLista.SelectedValue);}
else{
idValue = -1;
}
if (ExcluirDados(idValue) >= 0){
lbLista.Enabled =
true;btnIncluir.Enabled =
true;btnExcluir.Enabled =
true;PopularLista();
}
}
/// <summary> /// lbLista_SelectedIndexChanged /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void lbLista_SelectedIndexChanged(object sender, EventArgs e){
try{
LimparCampos();
if (lbLista.SelectedIndex >= 0){
tbCodigo.Text = lbLista.SelectedValue;
tbNomeTecnologia.Text = lbLista.Items[lbLista.SelectedIndex].Text;
PopularDados(lbLista.SelectedValue);
}
}
catch (Exception ex){
lblMensagem.Text = ex.ToString();
}
}
#endregion
}
}