This is a requirement in an Interview for one of my friend. First Take two user controls. In First User Control Add the Controls u want and now add the second user control as a control and write the following code in WebPart1.
Procedure:-
Add one Sample Project with Name Sample001. Now Add two Visual WebParts with Names WebPart001 & WebPart002.
In WebPart Ascx Page write the below HTML.
Procedure:-
Add one Sample Project with Name Sample001. Now Add two Visual WebParts with Names WebPart001 & WebPart002.
In WebPart Ascx Page write the below HTML.
WebPart001 Ascx |
<%@ 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="WebPart001UserControl.ascx.cs" Inherits="Sample001.WebPart001.WebPart001UserControl" %> <%@ Register src="../WebPart002/WebPart002UserControl.ascx" tagname="WebPart002UserControl" tagprefix="uc1" %> <table width="100%" cellpadding="0" cellspacing="0"> <tr> <td valign="top"> <table cellpadding="0" cellspacing="0"> <tr><td>Enter Employee ID</td><td><asp:TextBox ID="txtEmpID" runat="server"></asp:TextBox></td></tr> <tr><td>Enter Employee Name</td><td><asp:TextBox ID="txtEmpName" runat="server"></asp:TextBox></td></tr> <tr><td colspan="2" align="center"><asp:Button ID="btnSave" Text="Save" runat="server" onclick="btnSave_Click" /></td></tr> <tr><td colspan="2" align="center"><asp:Label ID="lblErrorMessage" runat="server"></asp:Label></td></tr> </table> </td> <td valign="top"> <div id="divGrid" height="300px" style="scroll:auto;"><uc1:WebPart002UserControl ID="WebPart002UserControl1" runat="server" /></div> </td> </tr> </table> |
WebPart001 Ascx Load |
using System; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using Microsoft.SharePoint; using Microsoft.SharePoint.WebControls; using System.Security; using WP2=Sample001.WebPart002; namespace Sample001.WebPart001 { public partial class WebPart001UserControl : UserControl { protected void Page_Load(object sender, EventArgs e) { } protected void btnSave_Click(object sender, EventArgs e) { try { SPWeb currentWeb = SPContext.Current.Web; SPList lst = currentWeb.Lists["Employee"]; SPListItemCollection myColl = lst.Items; SPSecurity.RunWithElevatedPrivileges(delegate() { SPListItem item = myColl.Add(); item["EmpID"] = txtEmpID.Text; item["EmpName"] = txtEmpName.Text; item.Update(); txtEmpID.Text = ""; txtEmpName.Text = ""; }); SPGridView sgv = (SPGridView)WebPart002UserControl1.FindControl("sgvEmployee"); sgv.DataSource = myColl.GetDataTable(); sgv.DataBind(); } catch (Exception ex) { lblErrorMessage.Text = ex.ToString(); } } } } |
WebPart002 Ascx |
<%@ 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="WebPart002UserControl.ascx.cs" Inherits="Sample001.WebPart002.WebPart002UserControl" %> <table width="100%" cellpadding="0" cellspacing="0"> <tr> <td> <SharePoint:SpGridView ID="sgvEmployee" runat="server" AutoGenerateColumns="false"> <Columns> <asp:BoundField HeaderText="Employee ID" DataField="EmpID" /> <asp:BoundField HeaderText="Employee Name" DataField="EmpName" /> </Columns> </SharePoint:SpGridView> </td> </tr> </table> |
WebPart002 Ascx Load |
using System; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using Microsoft.SharePoint; using System.Data; namespace Sample001.WebPart002 { public partial class WebPart002UserControl : UserControl { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { getData(); } } public void getData() { SPWeb currentWeb1 = SPContext.Current.Web; SPList lst1 = currentWeb1.Lists["Employee"]; SPListItemCollection myColl1 = lst1.Items; if (myColl1.Count > 0) { sgvEmployee.DataSource = myColl1.GetDataTable(); sgvEmployee.DataBind(); } } } } |
No comments:
Post a Comment