Search This Blog

Thursday, March 12, 2015

Save Record In List & Dropdown Populating and Selection changed Event






<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="EmployeeRegitrationUserControl.ascx.cs" Inherits="EUDGridView.EmployeeRegitration.EmployeeRegitrationUserControl" %>
<style type="text/css">
    .style1
    {
        width: 334px;
    }
</style>
<asp:Panel ID="Panel1" runat="server" Width="600px" BackColor="#FFCCFF">
    <table id="Submit" style="width: 597px">
        <tr>
            <td>
                Employee Name
            </td>
            <td class="style1">
                <asp:TextBox ID="txtEmployeeName" Width="200px" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                <p>
                    Designation</p>
            </td>
            <td class="style1">
                <asp:TextBox ID="txtDesi" Width="200px" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                EmpID</td>
            <td class="style1">
                <asp:TextBox ID="txtEmployeeID" runat="server" Width="200px"
       ></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                Country</td>
            <td class="style1">
                <asp:DropDownList ID="ddlCountry" runat="server" Width="200px"
                    AutoPostBack="True" onselectedindexchanged="ddlCountry_SelectedIndexChanged">
                </asp:DropDownList>
            </td>
        </tr>
        <tr>
            <td>
                State</td>
            <td class="style1">
                <asp:DropDownList ID="ddlState" Width="200px" runat="server">
                </asp:DropDownList>
            </td>
        </tr>
        <tr>
            <td align="right" colspan="2">
                <asp:Button ID="btnSubmit" runat="server" onclick="btnSubmit_Click"
                    Text="Submit" Width="100px" />
                &nbsp;&nbsp;&nbsp;<asp:Button ID="btnCancel" runat="server" onclick="btnCancel_Click"
                    Text="Cancel" Width="100px" />
                &nbsp;&nbsp;&nbsp;&nbsp;
            </td>
            <asp:Panel ID="Panel2" runat="server">
            </asp:Panel>
        </tr>
    </table>
</asp:Panel>




using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
namespace EUDGridView.EmployeeRegitration
{
    public partial class EmployeeRegitrationUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!IsPostBack)
                {
                    FillCountryDropDown();
                    ddlState.Items.Insert(0, new ListItem("--Select State--"));
                }
            }
            catch (Exception ex)
            {
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            try
            {
                txtEmployeeName.Text = String.Empty;
                txtDesi.Text = String.Empty;
                txtEmployeeID.Text = String.Empty;
                FillCountryDropDown();
                ddlState.Items.Clear();
                ddlState.Items.Insert(0, new ListItem("--Select State--"));
                SPUtility.Redirect("http://win-4ibnt6tq8js:26871/Ramtech/Lists/Employee_Details/AllItems.aspx", SPRedirectFlags.Trusted, this.Context);


            }
            catch (Exception ex)
            {
            }

        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {

            try
            {
                SPSite mysite = SPContext.Current.Site;
                SPWeb myweb = mysite.OpenWeb();
                SPListItemCollection listItemsCol = myweb.Lists["Employee_Details"].Items;
                SPListItem item = listItemsCol.Add();
                item["EmployeeName"] = txtEmployeeName.Text;
                item["Title"] = txtDesi.Text;
                item["EmployeeID"] = Convert.ToInt32(txtEmployeeID.Text);
                item["Country"] = ddlCountry.SelectedItem.ToString();
                item["CountryID"] = ddlCountry.SelectedValue;
                item["State"] = ddlState.SelectedItem.ToString();
                item["StateID"] = ddlState.SelectedValue;
                item.Update();
                txtEmployeeName.Text = String.Empty;
                txtDesi.Text = String.Empty;
                txtEmployeeID.Text = String.Empty;
                FillCountryDropDown();
                ddlState.Items.Clear();
                ddlState.Items.Insert(0, new ListItem("--Select State--"));

                SPUtility.TransferToSuccessPage("Save Records Successfully");
               
            


            }
            catch (Exception ex)
            {
            }
          
        }

        protected void FillCountryDropDown()
        {
            SPWeb currentWeb = SPContext.Current.Web;
            SPList lst = currentWeb.Lists["Country LookUp"];
            SPQuery sQuery = new SPQuery();
            sQuery.Query = "<OrderBy><FieldRef Name='ID' /></OrderBy>";
            SPListItemCollection myColl = lst.GetItems(sQuery);
            if (myColl.Count > 0)
            {
                ddlCountry.DataValueField = "ID";
                ddlCountry.DataTextField = "Title";
                ddlCountry.DataSource = myColl.GetDataTable();
                ddlCountry.DataBind();
            }
            ddlCountry.Items.Insert(0, new ListItem("--Select Country--"));
           
        }

        protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
        {
            SPWeb currentWeb = SPContext.Current.Web;
            int strItemID = int.Parse(ddlCountry.SelectedValue.ToString());
            SPList lstStateList = currentWeb.Lists["StateList"];
            SPQuery strFindLookUpValue = new SPQuery();
            strFindLookUpValue.Query = "<Where><Eq><FieldRef Name='CountID' /><Value Type='Number'>" + strItemID + "</Value></Eq></Where>";
            SPListItemCollection StateListColl = lstStateList.GetItems(strFindLookUpValue);
            if (StateListColl.Count > 0)
            {
                ddlState.DataValueField = "ID";
                ddlState.DataTextField = "Title";
                ddlState.DataSource = StateListColl.GetDataTable();
                ddlState.DataBind();
                //SPListItem item = StateListColl[0];
                //SPFieldLookupValue value = new SPFieldLookupValue(item["CountryID"].ToString());
                //for (int i = 0; i < ddlCountryNew.Items.Count; i++)
                //{

                //}
            }
            else
            {
                ddlState.Items.Clear();
                ddlState.Items.Insert(0, new ListItem("--Select State--"));
              
            }
        }

    }
}




No comments:

Post a Comment