This is a user control which contains a Drop Down list and an event
UserControl.ascx contents will be like this :
<%@ Control Language="C#" AutoEventWireup="true" Inherits="NameSpaceName.ClassName, NameSpaceName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=74a8568657e011e0" %>
.drp-color
{
color:#7d99c1;
font-family:Calibri;
font-size: 8pt;
width:150px;
}
</style>
<asp:Panel runat="server" ID="mainPanel">
<asp:DropDownList ID="drpList" runat="server" AutoPostBack="true" CssClass="drp-color" OnSelectedIndexChanged="drpList_SelectedIndexChanged">
</asp:DropDownList>
</asp:Panel>
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.UI.WebControls;
using System.Web.UI;
using Microsoft.SharePoint;
namespace NameSpaceName
{
public partial class ClassName: System.Web.UI.UserControl
{
public string TextColumnName { get; set; }
public string IdColumnName { get; set; }
public string ListName { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
PopulateDropDown();
}
}
public void PopulateDropDown()
{
try
{
DataTable dtResult;
using (SPSite spSite = new SPSite(SPContext.Current.Site.Url))
{
using (SPWeb spWeb = spSite.OpenWeb())
{
dtResult = GetListValues(spWeb);
drpList.DataSource = dtResult;
drpList.DataValueField = this.IdColumnName;
drpList.DataTextField = this.TextColumnName;
drpList.DataBind();
ListItem objItem = new ListItem("View All", "0");
drpList.Items.Insert(0, objItem);
//if (this.drpList.Items.Count > 0)
//{
// this.drpList.Items[0].Selected = true;
// this.OnDropDownListSelectionChanged();
//}
}
}
}
catch (Exception ex)
{
throw ex;
}
}
public DataTable GetListValues(SPWeb spWeb)
{
SPList lstValues = GetList(this.ListName, spWeb);
if (null != lstValues)
{
if (lstValues.ItemCount > 0)
return lstValues.Items.GetDataTable();
else
return null;
}
return null;
}
public SPList GetList(string listName, SPWeb spWeb)
{
SPList spList = null;
if (null != spWeb)
{
try
{
spList = spWeb.Lists[listName];
}
catch (Exception)
{
spList = null;
}
}
return spList;
}
public void drpList_SelectedIndexChanged(object sender, EventArgs e)
{
this.OnDropDownListSelectionChanged(sender,e);
}
# region Event Section
public delegate void DropDownSelectionDelegate(object sender, EventArgs e);
protected event DropDownSelectionDelegate drpListselectionChanged;
public event DropDownSelectionDelegate DropDownListSelectionChanged
{
add
{
this.drpListselectionChanged += value;
}
remove
{
this.drpListselectionChanged -= value;
}
}
public virtual void OnDropDownListSelectionChanged(object sender, EventArgs e)
{
if (this.drpListselectionChanged != null)
this.drpListselectionChanged(sender,e);
}
# endregion
}
}
Now in the Web part/Page in which you are using this control or want to trap this user control events. In this case its a visual web part (It will be inside another user control) will be like this :
UserControlABC.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="UserControlA.ascx.cs"
Inherits="NameSpace.ClassName.UserControlA" %>
<%@ Register Src="/_controltemplates/Project1/SPDropDownControl.ascx" TagPrefix="SAUserControl"
TagName="SPDropDownList" %>
<style type="text/css">
.pnlHorizontal
{
width:100%
}
</style>
<asp:Panel runat="server" ID="pnlHorizontal" Visible="false" CssClass="pnlHorizontal">
<table width="100%">
<tr>
<td>
<font style='font-family: Calibri !important; color: #C0C0C0 !important; font-size: 14pt!important; padding-left:50px; width:30%;'>
B :</font>
</td>
<td >
<font style='font-family: Calibri !important; font-size: 8 !important; color: #58443B !important; width:30%;'>
D : </font></br>
<SAUserControl:SPDropDownList runat="server" ID="uscH1" OnDropDownListSelectionChanged="ucListControl1_DropDownListSelectionChanged"
TextColumnName="Title" IdColumnName="ID" ListName="Channel" />
</td>
<td >
<font style='font-family: Calibri !important; font-size: 8 !important; color: #58443B !important; width:30%;'>
H : </font></br><SAUserControl:SPDropDownList runat="server" ID="uscH2"
OnDropDownListSelectionChanged="ucListControl1_DropDownListSelectionChanged"
TextColumnName="Title" IdColumnName="ID" ListName="Topic" />
</td>
<td>
<font style='font-family: Calibri !important; font-size: 8 !important; color: #58443B !important; width:30%; padding-right:100px;'>
I : </font></br><SAUserControl:SPDropDownList runat="server" ID="uscH3"
OnDropDownListSelectionChanged="ucListControl1_DropDownListSelectionChanged"
TextColumnName="Name" IdColumnName="ID" ListName="SMEProfile" />
</td>
</tr>
</table>
</asp:Panel>
<asp:Panel runat="server" ID="pnlVertical" Visible="false">
<table>
<%--<tr>
<td class='tc-VerticalSMEHeading'>
Find eXpert Knowledge by:
</td>
</tr>--%>
<tr>
<td>
<font style='font-family: Calibri !important; font-size: 8pt !important; color: black !important;'>
A:</font></br><SAUserControl:SPDropDownList runat="server" ID="uscV2" OnDropDownListSelectionChanged="ucListControl1_DropDownListSelectionChanged"
TextColumnName="Title" IdColumnName="ID" ListName="Channel" />
</td>
<td>
</tr>
<tr>
<td>
<font style='font-family: Calibri !important; font-size: 8pt !important; color: black !important;'>
F :</font></br><SAUserControl:SPDropDownList runat="server" ID="uscV1"
OnDropDownListSelectionChanged="ucListControl1_DropDownListSelectionChanged"
TextColumnName="Title" IdColumnName="ID" ListName="Topic" />
</td>
</tr>
<tr>
<td>
<font style='font-family: Calibri !important; font-size: 8pt !important; color: black !important;'>
E : </font></br><SAUserControl:SPDropDownList runat="server" ID="uscV3"
OnDropDownListSelectionChanged="ucListControl1_DropDownListSelectionChanged"
TextColumnName="Name" IdColumnName="ID" ListName="SMEProfile" />
</td>
</tr>
</table>
</asp:Panel>
UserControlABC.ascx.cs:
namespace NameSpace
{
public partial class UserControlABC : UserControl
{
public bool Horizontal { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
}
protected void ucListControl1_DropDownListSelectionChanged(object sender, EventArgs e)
{
DropDownList ddlCurrent = (DropDownList)sender;
string selectedID = ddlCurrent.SelectedValue;
switch (((System.Web.UI.WebControls.ListControl)(sender)).Parent.Parent.ID)
{
case "A":
Response.Redirect(SPContext.Current.Web.Url + string.Format("/Pages/rets.aspx?ID={0}&Mode=Channel",selectedID));
break;
case "B":
Response.Redirect(SPContext.Current.Web.Url + string.Format("/Pages/hyut.aspx?ID={0}&Mode=Topic", selectedID));
break;
case "F":
Response.Redirect(SPContext.Current.Web.Url + string.Format("/Pages/kjil.aspx?ID={0}", selectedID));
break;
case "C":
Response.Redirect(SPContext.Current.Web.Url + string.Format("/Pages/ghi.aspx?ID={0}&Mode=Channel",selectedID));
break;
case "D":
Response.Redirect(SPContext.Current.Web.Url + string.Format("/Pages/mno.aspx?ID={0}&Mode=Topic", selectedID));
break;
case "E":
Response.Redirect(SPContext.Current.Web.Url + string.Format("/Pages/abs.aspx?ID={0}", selectedID));
break;
}
}
}
}
Now you can trap this event as per cases in which you have used it.
UserControl.ascx contents will be like this :
<%@ Control Language="C#" AutoEventWireup="true" Inherits="NameSpaceName.ClassName, NameSpaceName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=74a8568657e011e0" %>
.drp-color
{
color:#7d99c1;
font-family:Calibri;
font-size: 8pt;
width:150px;
}
</style>
<asp:Panel runat="server" ID="mainPanel">
<asp:DropDownList ID="drpList" runat="server" AutoPostBack="true" CssClass="drp-color" OnSelectedIndexChanged="drpList_SelectedIndexChanged">
</asp:DropDownList>
</asp:Panel>
UserControl.ascx.cs contents will be like this :
using System;using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.UI.WebControls;
using System.Web.UI;
using Microsoft.SharePoint;
namespace NameSpaceName
{
public partial class ClassName: System.Web.UI.UserControl
{
public string TextColumnName { get; set; }
public string IdColumnName { get; set; }
public string ListName { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
PopulateDropDown();
}
}
public void PopulateDropDown()
{
try
{
DataTable dtResult;
using (SPSite spSite = new SPSite(SPContext.Current.Site.Url))
{
using (SPWeb spWeb = spSite.OpenWeb())
{
dtResult = GetListValues(spWeb);
drpList.DataSource = dtResult;
drpList.DataValueField = this.IdColumnName;
drpList.DataTextField = this.TextColumnName;
drpList.DataBind();
ListItem objItem = new ListItem("View All", "0");
drpList.Items.Insert(0, objItem);
//if (this.drpList.Items.Count > 0)
//{
// this.drpList.Items[0].Selected = true;
// this.OnDropDownListSelectionChanged();
//}
}
}
}
catch (Exception ex)
{
throw ex;
}
}
public DataTable GetListValues(SPWeb spWeb)
{
SPList lstValues = GetList(this.ListName, spWeb);
if (null != lstValues)
{
if (lstValues.ItemCount > 0)
return lstValues.Items.GetDataTable();
else
return null;
}
return null;
}
public SPList GetList(string listName, SPWeb spWeb)
{
SPList spList = null;
if (null != spWeb)
{
try
{
spList = spWeb.Lists[listName];
}
catch (Exception)
{
spList = null;
}
}
return spList;
}
public void drpList_SelectedIndexChanged(object sender, EventArgs e)
{
this.OnDropDownListSelectionChanged(sender,e);
}
# region Event Section
public delegate void DropDownSelectionDelegate(object sender, EventArgs e);
protected event DropDownSelectionDelegate drpListselectionChanged;
public event DropDownSelectionDelegate DropDownListSelectionChanged
{
add
{
this.drpListselectionChanged += value;
}
remove
{
this.drpListselectionChanged -= value;
}
}
public virtual void OnDropDownListSelectionChanged(object sender, EventArgs e)
{
if (this.drpListselectionChanged != null)
this.drpListselectionChanged(sender,e);
}
# endregion
}
}
Now in the Web part/Page in which you are using this control or want to trap this user control events. In this case its a visual web part (It will be inside another user control) will be like this :
UserControlABC.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="UserControlA.ascx.cs"
Inherits="NameSpace.ClassName.UserControlA" %>
<%@ Register Src="/_controltemplates/Project1/SPDropDownControl.ascx" TagPrefix="SAUserControl"
TagName="SPDropDownList" %>
<style type="text/css">
.pnlHorizontal
{
width:100%
}
</style>
<asp:Panel runat="server" ID="pnlHorizontal" Visible="false" CssClass="pnlHorizontal">
<table width="100%">
<tr>
<td>
<font style='font-family: Calibri !important; color: #C0C0C0 !important; font-size: 14pt!important; padding-left:50px; width:30%;'>
B :</font>
</td>
<td >
<font style='font-family: Calibri !important; font-size: 8 !important; color: #58443B !important; width:30%;'>
D : </font></br>
<SAUserControl:SPDropDownList runat="server" ID="uscH1" OnDropDownListSelectionChanged="ucListControl1_DropDownListSelectionChanged"
TextColumnName="Title" IdColumnName="ID" ListName="Channel" />
</td>
<td >
<font style='font-family: Calibri !important; font-size: 8 !important; color: #58443B !important; width:30%;'>
H : </font></br><SAUserControl:SPDropDownList runat="server" ID="uscH2"
OnDropDownListSelectionChanged="ucListControl1_DropDownListSelectionChanged"
TextColumnName="Title" IdColumnName="ID" ListName="Topic" />
</td>
<td>
<font style='font-family: Calibri !important; font-size: 8 !important; color: #58443B !important; width:30%; padding-right:100px;'>
I : </font></br><SAUserControl:SPDropDownList runat="server" ID="uscH3"
OnDropDownListSelectionChanged="ucListControl1_DropDownListSelectionChanged"
TextColumnName="Name" IdColumnName="ID" ListName="SMEProfile" />
</td>
</tr>
</table>
</asp:Panel>
<asp:Panel runat="server" ID="pnlVertical" Visible="false">
<table>
<%--<tr>
<td class='tc-VerticalSMEHeading'>
Find eXpert Knowledge by:
</td>
</tr>--%>
<tr>
<td>
<font style='font-family: Calibri !important; font-size: 8pt !important; color: black !important;'>
A:</font></br><SAUserControl:SPDropDownList runat="server" ID="uscV2" OnDropDownListSelectionChanged="ucListControl1_DropDownListSelectionChanged"
TextColumnName="Title" IdColumnName="ID" ListName="Channel" />
</td>
<td>
</tr>
<tr>
<td>
<font style='font-family: Calibri !important; font-size: 8pt !important; color: black !important;'>
F :</font></br><SAUserControl:SPDropDownList runat="server" ID="uscV1"
OnDropDownListSelectionChanged="ucListControl1_DropDownListSelectionChanged"
TextColumnName="Title" IdColumnName="ID" ListName="Topic" />
</td>
</tr>
<tr>
<td>
<font style='font-family: Calibri !important; font-size: 8pt !important; color: black !important;'>
E : </font></br><SAUserControl:SPDropDownList runat="server" ID="uscV3"
OnDropDownListSelectionChanged="ucListControl1_DropDownListSelectionChanged"
TextColumnName="Name" IdColumnName="ID" ListName="SMEProfile" />
</td>
</tr>
</table>
</asp:Panel>
UserControlABC.ascx.cs:
namespace NameSpace
{
public partial class UserControlABC : UserControl
{
public bool Horizontal { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
}
protected void ucListControl1_DropDownListSelectionChanged(object sender, EventArgs e)
{
DropDownList ddlCurrent = (DropDownList)sender;
string selectedID = ddlCurrent.SelectedValue;
switch (((System.Web.UI.WebControls.ListControl)(sender)).Parent.Parent.ID)
{
case "A":
Response.Redirect(SPContext.Current.Web.Url + string.Format("/Pages/rets.aspx?ID={0}&Mode=Channel",selectedID));
break;
case "B":
Response.Redirect(SPContext.Current.Web.Url + string.Format("/Pages/hyut.aspx?ID={0}&Mode=Topic", selectedID));
break;
case "F":
Response.Redirect(SPContext.Current.Web.Url + string.Format("/Pages/kjil.aspx?ID={0}", selectedID));
break;
case "C":
Response.Redirect(SPContext.Current.Web.Url + string.Format("/Pages/ghi.aspx?ID={0}&Mode=Channel",selectedID));
break;
case "D":
Response.Redirect(SPContext.Current.Web.Url + string.Format("/Pages/mno.aspx?ID={0}&Mode=Topic", selectedID));
break;
case "E":
Response.Redirect(SPContext.Current.Web.Url + string.Format("/Pages/abs.aspx?ID={0}", selectedID));
break;
}
}
}
}
Now you can trap this event as per cases in which you have used it.
No comments:
Post a Comment