Friday, 20 July 2012

how to call a user control function in general page

user control code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Swash.Objects;
using Swash.BusinessLayer;

namespace Website.App_WebControls.UAMS_UserControls
{
    public partial class AMS_UCFilterwiseMultipleSelection : System.Web.UI.UserControl
    {
        public delegate void SingleSelect(List<AMS_UCUSelection> MultiCheckList);
        public event SingleSelect SelectEvent;
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        public void FillCheckBoxList(AMS_UCUSelection objSelect)
        {
            lblHeader.Text = objSelect.HeaderText;
            ChkMultiFilterWiseSelect.DataSource = UERPManagement.GetInstance.FilterwiseSelection(objSelect);
            ChkMultiFilterWiseSelect.DataTextField = "Text";
            ChkMultiFilterWiseSelect.DataValueField = "Value";
            ChkMultiFilterWiseSelect.DataBind();
        }
        protected void ImgBtnAdd_Click(object sender, ImageClickEventArgs e)
        {
            List<AMS_UCUSelection> MultiChkList = new List<AMS_UCUSelection>();
            foreach (ListItem li in ChkMultiFilterWiseSelect.Items)
            {
                if (li.Selected)
                {
                    AMS_UCUSelection obj = new AMS_UCUSelection();
                    obj.Text = li.Text;
                    obj.Value = li.Value;
                    MultiChkList.Add(obj);
                }

            }
            if (this.SelectEvent != null)
            {
                this.SelectEvent(MultiChkList);
            }
        }
    }
}
general page code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Swash.Objects;
using Swash.BusinessLayer;
using System.IO;

namespace Website.UERP.UAMS
{
    public partial class AMS_UAssetEntry : System.Web.UI.Page
    {
        int UserId = 0;
        #region Event
        protected void Page_Load(object sender, EventArgs e)
        {
            UserId = 1;
            if (!IsPostBack)
            {
                FillAssetCode(); 
                if (Request.QueryString["AssetId"] != null)
                {
                    ButtonEdit();
                    HidAssetId.Value = Request.QueryString["AssetId"].ToString();
                    EditAsset();
                }
                else
                {
                    ButtonInsert();
                }
            }
            UCSingleSelect.SelectEvent += new App_WebControls.UAMS_UserControls.AMS_UCUSingleSelection.SingleSelect(UCSingleSelect_SelectEvent);
        }
        protected void LnkAssetType_Click(object sender, EventArgs e)
        {
            ViewState["Select"] = 1;
            LoadAssetTypeUC();
            UCSingleSelect.Visible = true;
            ScriptManager.RegisterStartupScript(this, this.GetType(), "popup", "ShowPopupNew()", true);
        }
        protected void LnkBrand_Click(object sender, EventArgs e)
        {
            ViewState["Select"] = 2;
            LoadBrandUC();
            UCSingleSelect.Visible = true;
            ScriptManager.RegisterStartupScript(this, this.GetType(), "popup", "ShowPopupNew()", true);
        }
        protected void LnkOrderUnit_Click(object sender, EventArgs e)
        {
            ViewState["Select"] = 3;
            LoadUnitUC();
            UCSingleSelect.Visible = true;
            ScriptManager.RegisterStartupScript(this, this.GetType(), "popup", "ShowPopupNew()", true);
        }
        protected void LnkIssueUnit_Click(object sender, EventArgs e)
        {
            ViewState["Select"] = 4;
            LoadUnitUC();
            UCSingleSelect.Visible = true;
            ScriptManager.RegisterStartupScript(this, this.GetType(), "popup", "ShowPopupNew()", true);
        }
        protected void ImgBtnSave_Click(object sender, ImageClickEventArgs e)
        {
            if (Request.QueryString["AssetId"] != null)
            {
                UpdateAsset();
            }
            else
            {
                InsertAsset();
            }
        }
        protected void ImgBtnReset_Click(object sender, ImageClickEventArgs e)
        {
            Reset();
        }
        protected void ImgBtnDelete_Click(object sender, ImageClickEventArgs e)
        {
            DeleteAsset();
        }
        protected void UCSingleSelect_SelectEvent(List<AMS_UCUSelection> SingleChkSelect) // Method for selecting Items to Textbox
        {
            string msg = "";
            if (SingleChkSelect.Count > 0)
            {
                if (Convert.ToInt32(ViewState["Select"]) == 1)
                {
                    TxtAssetType.Text = SingleChkSelect.ToList()[0].Text;
                    HidAssetTypeId.Value = SingleChkSelect.ToList()[0].Value;
                }
                else if (Convert.ToInt32(ViewState["Select"]) == 2)
                {
                    TxtBrand.Text = SingleChkSelect.ToList()[0].Text;
                    HidBrandId.Value = SingleChkSelect.ToList()[0].Value;
                }
                else if (Convert.ToInt32(ViewState["Select"]) == 3)
                {
                    TxtOrderUnit.Text = SingleChkSelect.ToList()[0].Text;
                    HidOrderUnitId.Value = SingleChkSelect.ToList()[0].Value;
                }
                else if (Convert.ToInt32(ViewState["Select"]) == 4)
                {
                    TxtIssueUnit.Text = SingleChkSelect.ToList()[0].Text;
                    HidIssueUnitId.Value = SingleChkSelect.ToList()[0].Value;
                }
            }
            else
            {
                msg = "alert('Select an Item')";
                ScriptManager.RegisterStartupScript(this, this.GetType(), "alertScript", msg, true);
            }
        }
        #endregion Event
        #region Method
        public void FillAssetCode()
        {
            string AssetCode = string.Empty;
            AssetCode = UERPManagement.GetInstance.GetAssetCode();
            TxtCode.Text = "SWA/AST" + AssetCode;
        }
        public void LoadAssetTypeUC()
        {
            AMS_UCUSelection objSelect = new AMS_UCUSelection();
            objSelect.HeaderText = "Asset Type [Selection]";
            objSelect.TableName = "Uams.AMS_AssetTypeMaster";
            objSelect.Text = "Asset_Type_Name";
            objSelect.Value = "Asset_Type_ID";
            UCSingleSelect.FillRadioList(objSelect);
        }
        public void LoadBrandUC()
        {
            AMS_UCUSelection objSelect = new AMS_UCUSelection();
            objSelect.HeaderText = "Brand [Selection]";
            objSelect.TableName = "Uams.AMS_BrandMaster";
            objSelect.Text = "Brand_Name";
            objSelect.Value = "Brand_ID";
            UCSingleSelect.FillRadioList(objSelect);
        }
        public void LoadUnitUC()
        {
            AMS_UCUSelection objSelect = new AMS_UCUSelection();
            objSelect.HeaderText = "Unit [Selection]";
            objSelect.TableName = "Uams.AMS_UnitMaster";
            objSelect.Text = "Unit_Name";
            objSelect.Value = "Unit_ID";
            UCSingleSelect.FillRadioList(objSelect);
        }
        public void ButtonInsert()
        {
            ImgBtnReset.Visible = true;
            ImgBtnDelete.Visible = false;
        }
        public void ButtonEdit()
        {
            ImgBtnReset.Visible = false;
            ImgBtnDelete.Visible = true;
        }
        public void Reset()
        {
            TxtAssetType.Text = "";
            HidAssetTypeId.Value = "";
            TxtAssetName.Text = "";
            TxtBrand.Text = "";
            HidBrandId.Value = "";
            TxtSpecification.Text = "";
            TxtOrderUnit.Text = "";
            HidOrderUnitId.Value = "";
            TxtIssueUnit.Text = "";
            HidIssueUnitId.Value = "";
            TxtQtyIssue.Text = "";
            HidAssetImage.Value = "";
            ImgAsset.ImageUrl = "";
            ChkCondition.Checked = false;
            ChkRotating.Checked = false;
            ChkKit.Checked = false;
            ChkSerial.Checked = false;
            ChkMultiSerial.Checked = false;
            ChkTag.Checked = false;
            TxtAssetType.Focus();
        }
        public void EditAsset()
        {
            int AssetId = int.Parse(HidAssetId.Value);
            List<AMS_UMasterType> astList = new List<AMS_UMasterType>();
            astList = UERPManagement.GetInstance.GetAllAssetdetails();
            var lnqAst = from ast in astList
                         where ast.AssetId == AssetId
                         select ast;
            TxtCode.Text = lnqAst.ToList()[0].AssetCode;
            TxtAssetType.Text = lnqAst.ToList()[0].AssetType;
            HidAssetTypeId.Value = lnqAst.ToList()[0].AssetTypeId.ToString();
            TxtAssetName.Text = lnqAst.ToList()[0].AssetName;
            TxtBrand.Text = lnqAst.ToList()[0].BrandName;
            HidBrandId.Value = lnqAst.ToList()[0].BrandId.ToString();
            TxtSpecification.Text = lnqAst.ToList()[0].AssetSpecification;
            TxtOrderUnit.Text = lnqAst.ToList()[0].OrderUnitName;
            HidOrderUnitId.Value = lnqAst.ToList()[0].OrderUnitId.ToString();
            TxtIssueUnit.Text = lnqAst.ToList()[0].IssueUnitName;
            HidIssueUnitId.Value = lnqAst.ToList()[0].IssueUnitId.ToString();
            if (lnqAst.ToList()[0].MaxQtyIssued == 0)
            {
                TxtQtyIssue.Text = "";
            }
            else
            {
                TxtQtyIssue.Text = lnqAst.ToList()[0].MaxQtyIssued.ToString();
            }
            if (lnqAst.ToList()[0].AssetImage != "")
            {
                HidAssetImage.Value = lnqAst.ToList()[0].AssetImage;
                ImgAsset.ImageUrl = "~/images/ERP/AMS/Asset/" + HidAssetImage.Value;
                ImgAsset.DataBind();
            }
            else
            {
                HidAssetImage.Value = "";
                ImgAsset.ImageUrl = "";
            }

            if (lnqAst.ToList()[0].ConditionEnabled == true)
            {
                ChkCondition.Checked = true;
            }
            if (lnqAst.ToList()[0].Kit == true)
            {
                ChkKit.Checked = true;
            }
            if (lnqAst.ToList()[0].Rotating == true)
            {
                ChkRotating.Checked = true;
            }
            if (lnqAst.ToList()[0].Serial == true)
            {
                ChkSerial.Checked = true;
            }
            if (lnqAst.ToList()[0].MultiSerial == true)
            {
                ChkMultiSerial.Checked = true;
            }
            if (lnqAst.ToList()[0].TagNo == true)
            {
                ChkTag.Checked = true;
            }
        }
        public void InsertAsset()
        {
            string uploadPath = "";
            string img = "";
            int count = 0;
            AMS_UMasterType obAsset = new AMS_UMasterType();
            obAsset.AssetCode = TxtCode.Text;
            obAsset.AssetTypeId = int.Parse(HidAssetTypeId.Value);
            obAsset.AssetName = TxtAssetName.Text;
            obAsset.BrandId = int.Parse(HidBrandId.Value);
            obAsset.AssetSpecification = TxtSpecification.Text;
            obAsset.OrderUnitId = int.Parse(HidOrderUnitId.Value);
            obAsset.IssueUnitId = int.Parse(HidIssueUnitId.Value);
            if (TxtQtyIssue.Text == "")
            {
                obAsset.MaxQtyIssued = 0;
            }
            else
            {
                obAsset.MaxQtyIssued = int.Parse(TxtQtyIssue.Text.Trim());
            }
            if (FileUploadImage.HasFile)
            {
                img = FileUploadImage.FileName;
                uploadPath = "~/images/ERP/AMS/Asset/" + FileUploadImage.FileName;
                FileUploadImage.SaveAs(Server.MapPath(uploadPath));
            }
            else
            {
                img = "";
            }
            obAsset.AssetImage = img;

            if (ChkCondition.Checked)
            {
                obAsset.ConditionEnabled = true;
            }
            else
            {
                obAsset.ConditionEnabled = false;
            }
            if (ChkRotating.Checked)
            {
                obAsset.Rotating = true;
            }
            else
            {
                obAsset.Rotating = false;
            }
            if (ChkKit.Checked)
            {
                obAsset.Kit = true;
            }
            else
            {
                obAsset.Kit = false;
            }
            if (ChkSerial.Checked)
            {
                obAsset.Serial = true;
            }
            else
            {
                obAsset.Serial = false;
            }
            if (ChkMultiSerial.Checked)
            {
                obAsset.MultiSerial = true;
            }
            else
            {
                obAsset.MultiSerial = false;
            }
            if (ChkTag.Checked)
            {
                obAsset.TagNo = true;
            }
            else
            {
                obAsset.TagNo = false;
            }
            obAsset.CreatedBy = UserId;
            count = UERPManagement.GetInstance.InsertAsset(obAsset);
            if (count != 0)
            {
                LbMsg.Text = "Insert Successfully";
            }
            else
            {
                LbMsg.Text = "Already Exists";
            }
            ViewState["AstImage"] = "~/images/ERP/AMS/Asset/" + obAsset.AssetImage;
            ImgAsset.ImageUrl = ViewState["AstImage"].ToString();
        }
        public void UpdateAsset()
        {
            string img="",uploadPath="";
            AMS_UMasterType obAsset = new AMS_UMasterType();
            obAsset.AssetId = int.Parse(HidAssetId.Value);
            obAsset.AssetCode = TxtCode.Text;
            obAsset.AssetTypeId = int.Parse(HidAssetTypeId.Value);
            obAsset.AssetName = TxtAssetName.Text;
            obAsset.BrandId = int.Parse(HidBrandId.Value);
            obAsset.AssetSpecification = TxtSpecification.Text;
            obAsset.OrderUnitId = int.Parse(HidOrderUnitId.Value);
            obAsset.IssueUnitId = int.Parse(HidIssueUnitId.Value);
            if (TxtQtyIssue.Text == "")
            {
                obAsset.MaxQtyIssued = 0;
            }
            else
            {
                obAsset.MaxQtyIssued = int.Parse(TxtQtyIssue.Text.Trim());
            }
            if (FileUploadImage.HasFile)
            {
                img = FileUploadImage.FileName;
                uploadPath = "~/images/ERP/AMS/Asset/" + FileUploadImage.FileName;
                FileUploadImage.SaveAs(Server.MapPath(uploadPath));
            }
            else if (HidAssetImage.Value != "")
            {
                img = HidAssetImage.Value;
            }
            else
            {
                img = "";
            }
            obAsset.AssetImage = img;
            if (ChkCondition.Checked)
            {
                obAsset.ConditionEnabled = true;
            }
            else
            {
                obAsset.ConditionEnabled = false;
            }
            if (ChkRotating.Checked)
            {
                obAsset.Rotating = true;
            }
            else
            {
                obAsset.Rotating = false;
            }
            if (ChkKit.Checked)
            {
                obAsset.Kit = true;
            }
            else
            {
                obAsset.Kit = false;
            }
            if (ChkSerial.Checked)
            {
                obAsset.Serial = true;
            }
            else
            {
                obAsset.Serial = false;
            }
            if (ChkMultiSerial.Checked)
            {
                obAsset.MultiSerial = true;
            }
            else
            {
                obAsset.MultiSerial = false;
            }
            if (ChkTag.Checked)
            {
                obAsset.TagNo = true;
            }
            else
            {
                obAsset.TagNo = false;
            }
            obAsset.ModifiedBy = UserId;

            UERPManagement.GetInstance.UpdateAsset(obAsset);
            LbMsg.Text = "Update Successfully";
            ViewState["AstImage"] = "~/images/ERP/AMS/Asset/" + obAsset.AssetImage;
            ImgAsset.ImageUrl = ViewState["AstImage"].ToString();
        }
        public void DeleteAsset()
        {
            int count = 0;
            AMS_UMasterType obAst = new AMS_UMasterType();
            obAst.AssetId = int.Parse(HidAssetId.Value);
            obAst.ModifiedBy = UserId;
            count = UERPManagement.GetInstance.DeleteAsset(obAst);
            if (count == 1)
            {
                LbMsg.Text = "Delete Successfully";
                Reset();
                FillAssetCode();
            }
        }
        #endregion Method
    }
}

No comments:

Post a Comment