WebPart Basic:
http://www.c-sharpcorner.com/UploadFile/munnamax/WbParts07062007124326PM/WbParts.aspx
In this article I have created two user controls as follows:
- CustomerUcl.ascx
- OrderUcl.ascx
Ideas is depending on customer selected from the drop down list on selection change it should communicate with the Orderucl.aspx and provide the list of orders associated with that particular customer.
Web part connections have the following elements:
An interface that defines the communications contract between two parts. The interface describes properties and methods available through the connection.
A web part that behaves as a connection provider. To specify a provider connection point, a web part needs to have a method that creates and returns an instance of the communications interface. This method should be marked with the ConnectionProvider attribute. By default, a single provider connection point can be used with multiple connection consumers.
A web part that behaves as a connection consumer. To specify a consumer connection point, a web part needs to have a method that takes an instance of the communications interface as a parameter. This method should be marked with the ConnectionConsumer attribute. By default, a single consumer connection point can only be used with one connection provider.
Here is my code in detail:
CustomerUcl.ascx:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="CustomerUcl.ascx.cs" Inherits="CustomerUcl" %>
<asp:DropDownList ID="ddCust" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource1" DataTextField="Cust_Name" DataValueField="Cust_Id">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT [Cust_Id], [Cust_Name] FROM [Tbl_Customer]">
</asp:SqlDataSource>
CustomerUcl.ascx.cs:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class CustomerUcl : System.Web.UI.UserControl, ISelectedCustomer
{
protected void Page_Load(object sender, EventArgs e)
{
}
public string SelectedCustomerId
{
get { return ddCust.SelectedValue; }
}
[ConnectionProvider("SelectedCustomer", "SelectedCustomer")]
public ISelectedCustomer GetSelectedCustomer()
{
return this;
}
}
OrderUcl.ascx:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="OrderUcl.ascx.cs" Inherits="OrderUcl" %>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting= "True" DataSourceID="SqlDataSource1">
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT [Ord_Date], [Fright_Cost] FROM [Tbl_Orders] WHERE ([Cust_Id] = @Cust_Id)">
<SelectParameters>
<asp:ControlParameter ControlID="hselCustomerId" DefaultValue="0" Type="Int32" Name="Cust_Id" PropertyName="value"/>
</SelectParameters>
</asp:SqlDataSource>
<asp:HiddenField ID="hselCustomerId" runat=server />
OrderUcl.ascx.cs:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class OrderUcl : System.Web.UI.UserControl
{
private ISelectedCustomer customer = null;
[ConnectionConsumer("SelectedCustomer", "SelectedCustomer")]
public void SetSelectedCustomer(ISelectedCustomer selectedCustomer)
{
customer = selectedCustomer;
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
if (customer != null)
{
hselCustomerId.Value = customer.SelectedCustomerId;
}
}
}
To communicate between both these controls I have interface called as selectedCustomer.
ISelectedCustomer.cs:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// Summary description for ISelectedCustomer
/// </summary>
public interface ISelectedCustomer
{
string SelectedCustomerId { get;}
}
Which has SelectedCustomerId getter method to which value can be given by the hidden variable at runtime.
<asp:HiddenField ID="hselCustomerId" runat=server />
Here is my page, which uses these controls
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Connection.aspx.cs" Inherits="Connection" %>
<%@ Register Src="CustomerUcl.ascx" TagName="CustomerUcl" TagPrefix="uc1"%>
<%@ Register Src="OrderUcl.ascx" TagName="OrderUcl" TagPrefix="uc2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td style="width: 100px">
<asp:WebPartManager ID="WebPartManager1" runat="server">
<StaticConnections>
<asp:WebPartConnection ID="Connection1" ConsumerConnectionPointID="SelectedCustomer"
ProviderConnectionPointID="SelectedCustomer" ConsumerID="OrderUcl1" ProviderID="CustomerUcl1"></asp:WebPartConnection>
</StaticConnections>
</asp:WebPartManager>
<asp:WebPartZone ID="WebPartZone1" runat="server">
<ZoneTemplate>
<uc1:CustomerUcl ID="CustomerUcl1" runat="server" />
</ZoneTemplate>
</asp:WebPartZone>
<asp:WebPartZone ID="WebPartZone2" runat="server">
<ZoneTemplate>
<uc2:OrderUcl ID="OrderUcl1" runat="server" />
</ZoneTemplate>
</asp:WebPartZone>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>