Audio Video Modules for ASP.NET Community Starter Kit : Part IV

Implement the page for insert.

Again CSK offers the "ContentAddPage" class to disburden our adding task. In order to claim this facility, we have to name child controls of the content- skin (AudioVideos_AddAudioVideo.ascx) in a particular manner so that the override protected void ContentAddPage InitializeSkin(Control skin) method can grasp the child controls and wire them with suitable event-handlers. We have created the "AddAudioVideo" class (see Figure 8) to control the content-skin.

namespace
ASPNET.StarterKit.Communities.AudioVideos
{
using
System;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
ASPNET.StarterKit.Communities; 
//*********************************************************************
//
// AddAudioVideo Class
//
// Represents the Add Link page. Enables users to list new Broadcastlinks.
//
//*********************************************************************
public class
AddAudioVideo : ContentAddPage
{
string
_skinFileName = "AudioVideos_AddAudioVideo.ascx";
string
_sectionContent = "ASPNET.StarterKit.Communities.AudioVideos.AudioVideoSection";
TextBox txtUrl;
TextBox txtTitle;
TextBox txtDescription;
TextBox txtWidth;
TextBox txtHeight;
TopicPicker dropTopics;
AudioVideoTitle previewLinkTitle;
BriefDescription previewDescription;
DisplayTopic previewTopic;
RadioButton rbVideo;
RadioButton rbAudio; 
//*********************************************************************
//
// SkinLoadAV
//
// The skin load event happens after a page skin has been loaded.
// Here, we grab the necessary controls from the page skin.
//
//*********************************************************************
void
SkinLoadAV(Object s, SkinLoadEventArgs e)
{
txtTitle = (TextBox)GetControl(e.Skin,"txtTitle");
txtUrl = (TextBox)GetControl(e.Skin,"txtUrl");
txtDescription = (TextBox)GetControl(e.Skin,"txtDescription");
txtWidth=(TextBox)GetControl(e.Skin,"txtWidth");
txtHeight=(TextBox)GetControl(e.Skin,"txtHeight");
dropTopics = (TopicPicker)GetControl(e.Skin, "dropTopics");
previewLinkTitle = (AudioVideoTitle)GetControl(e.Skin, "previewLinkTitle");
previewDescription = (BriefDescription)GetControl(e.Skin, "previewDescription");
previewTopic = (DisplayTopic)GetControl(e.Skin, "previewTopic" );rbVideo =
(RadioButton)GetControl(e.Skin,"rbVideo");
rbAudio =(RadioButton)GetControl(e.Skin,"rbAudio");
rbVideo.GroupName="AudioVideo";
rbAudio.GroupName="AudioVideo";
}
//*********************************************************************
//
// PreviewLink Method
//
// When previewing a link, we want to transfer everything from
// the text boxes to the labels.
//
//*********************************************************************
void
PreviewLink(Object s, EventArgs e)
{
previewLinkTitle.Text = txtTitle.Text;
previewLinkTitle.Url = txtUrl.Text;
previewDescription.Text = txtDescription.Text;
if
(objSectionInfo.EnableTopics)
previewTopic.Name = dropTopics.SelectedItem.Text;

//*********************************************************************
//
// SubmitAudioVideo Method
//
// This method is raised by clicking the Add button in the
udioVideos_AddAudioVideo.ascx
//. It inserts an Audio-Video content to the database
//
/*********************************************************************
void
SubmitAudioVideo(Object s, EventArgs e)
{
if
(Page.IsValid)
{
// Get Topic
int
topicID = -1;
if
(objSectionInfo.EnableTopics)
topicID = Int32.Parse(dropTopics.SelectedItem.Value);
// Check moderation status
int
moderationStatus = 1;
if
(objSectionInfo.EnableModeration && !objUserInfo.MayModerate)
moderationStatus = 0; 
// Content type
bool bVideo=(this.rbVideo.Checked?true:false
);
short
sHeight=Convert.ToInt16(txtHeight.Text);
short
sWidth=Convert.ToInt16(txtWidth.Text);
// Insert
int
contentPageID = AudioVideoUtility.AddAudioVideoLink
(
objSectionInfo.ID,
objUserInfo.Username,
txtTitle.Text,
txtUrl.Text,
bVideo,
sWidth,
sHeight,
txtDescription.Text,
moderationStatus,
topicID
);
// Show warning message if moderation enabled
if
(objSectionInfo.EnableModeration && !objUserInfo.MayModerate)
Context.Response.Redirect(CommunityGlobals.CalculatePath("Messages_Message.aspx?
essage=moderation"));
// Otherwise, redirect to default page and send notifications
Context.Server.ScriptTimeout = 10 * 60;
Context.Response.Redirect(CommunityGlobals.CalculatePath("Default.aspx"),
false
);
Context.Response.Flush();
Context.Response.Close();
NotifyUtility.SendNotifications(objSectionInfo.ID, contentPageID, txtTitle.Text, objUserInfo.Username);
Context.Response.End();
}

//*********************************************************************
//
// AddAudioVideo Constructor
//
// Calls the base SkinnedCommunityControl constructor
// and assigns the default page skin.
//
//*********************************************************************
public AddAudioVideo() : base
()
{
// Assign a default skin file name
SkinFileName = _skinFileName;
// Not specified
SectionContent = _sectionContent;
// Wire-up event handlers
this.SkinLoad += new
SkinLoadEventHandler(SkinLoadAV);
this.Preview += new
PreviewEventHandler(PreviewLink);
this.Submit += new
SubmitEventHandler(SubmitAudioVideo);
}
}
}

Figure 8 (Class AddAudioVideo)

We have used the following statement to register
this
page.

/* register AudioVideo_AddAudioVideo.aspx*/
Insert INTO Community_NamedPages
(
namedpage_path ,
namedpage_pageContent ,
namedpage_title ,
namedpage_description ,
namedpage_sortOrder ,
namedpage_isVisible ,
namedpage_name ,
namedPage_menuID
)
VALUES
(
'/AudioVideos_AddAudioVideo.aspx',
'ASPNET.StarterKit.Communities.AudioVideos.AddAudioVideo',
'Adds an AudioVideo record',
'Enables users to add a BroadCast Link',
0,
1,
'Add a AudioVideo content',
0
)
Go

Create a page to represent a particular audio-video content.

We have created the " AudioVideos_AudioVideo" class and the "AudioVideos_AudioVideo.ascx" to display a particular Audio-Video content. The "AudioVideos_AudioVideo" class is derived from the "ContentItemPage" class and you can see from figure 9 that we have initialized the inherited properties "_skinFilename" and "_getContentItem". The ContentItemPage .OnInit(EventArgs e) uses the supplied callback method(AudioVideoUtility.GetAudioVideo) to retrieve the requested content and saves it into the memory.

override protected void OnInit(EventArgs e)
{
ContentInfo _contentInfo = GetContentItem(objUserInfo.Username, objPageInfo.ID);
Context.Items["ContentInfo"] = _contentInfo;
}
namespace namespace
ASPNET.StarterKit.Communities.AudioVideos
{
using
System;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
ASPNET.StarterKit.Communities;
//*********************************************************************
//
// AudioVideo Class
//
// Represents an individual AudioVideo content.
//
// _skinFileName = the name of the skin to use for this section
//
// _getContentItem = the name of the method that retrieves the content item
//
//*********************************************************************
public class
AudioVideo : ContentItemPage
{
string
_skinFileName = "AudioVideos_AudioVideo.ascx";
GetContentItemDelegate _getContentItem = new
GetContentItemDelegate
AudioVideoUtility.GetAudioVideo);
//*********************************************************************
//
// Article Constructor
//
// Assigns skin and contentItems method to base ContentItemPage class
//
//*********************************************************************
public AudioVideo() : base
()
{
SkinFileName = _skinFileName;
GetContentItem = _getContentItem;
}
}
}

( Source code for the ItemAVTitle class )

Additionally, we have created an another custom control " AudioVideoEditControl" 

namespace ASPNET.StarterKit.Communities.AudioVideos
{
using
System;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
ASPNET.StarterKit.Communities;
//*********************************************************************
//
// AudioVideo Class
//
// Represents an individual AudioVideo content.
//
// _skinFileName = the name of the skin to use for this section
//
// _getContentItem = the name of the method that retrieves the content item
//
//*********************************************************************
public class
AudioVideo : ContentItemPage
{
string
_skinFileName = "AudioVideos_AudioVideo.ascx";
GetContentItemDelegate _getContentItem =
new
GetContentItemDelegate
AudioVideoUtility.GetAudioVideo);
//*********************************************************************
//
// Article Constructor
//
// Assigns skin and contentItems method to base ContentItemPage class
//
//*********************************************************************
public AudioVideo() : base
()
{
SkinFileName = _skinFileName;
GetContentItem = _getContentItem;
}
}
}
}

Figure 9 (AudioVideo class)

Now, embedded custom controls can take the AudioVideoInfo object from the memory and display it.

The "WMPlayer" custom control(Figure 10 embeds Windows Media ActiveX control and customizes the ActiveX control
using its PARAM Tags ( check for more details http://www.webreference.com/js/column51/install.html ).

using System;
using
System.Web;
using
System.Web.UI;
using
System.Text;
using
System.Web.UI.WebControls;
using
System.ComponentModel;
using
System.Text.RegularExpressions; 
namespace
ASPNET.StarterKit.Communities
{
///
<summary>
///
embeds Windows Media Player in the webpage
///
</summary>
public class
WMPlayer: WebControl
{
string
strBroadcastLink=String.Empty;
string
strWidth="250";
string
strHeight="250";
public WMPlayer():base
()
{
CssClass = "bookPublisher";
// Get ContentInfo object
if (Context != null
)
{
AudioVideoInfo objAVInfo = (AudioVideoInfo)Context.Items["ContentInfo"];
this
.strBroadcastLink=objAVInfo.BroadcastLink;
this
.strHeight=Convert.ToString(objAVInfo.Height);
this
.strWidth=Convert.ToString(objAVInfo.Width.ToString());
}
}
//*********************************************************************
//
// RenderContents Method
//
//*********************************************************************
override protected void
RenderContents(HtmlTextWriter htWriter)
{
StringBuilder oBuilder =
new
StringBuilder();
oBuilder.Append("<OBJECT CLASSID=\"clsid:6BF52A52-394A-11D3-B153-
0C04F79FAA6\" ID=\"WMP\" Height=\"#Height#\" width=\"#Width#\" VIEWASTEXT> ");
oBuilder.Append("<PARAM NAME=\"Name\" VALUE=\"AVWMPlayer\">");
oBuilder.Append("<PARAM NAME=\"URL\" VALUE=\"#BroadcastLink#\">");
oBuilder.Append("<PARAM NAME=\"autoStart\" VALUE=false>");
oBuilder.Append("<PARAM NAME=\"showControls\" VALUE=true>");
oBuilder.Append("<PARAM NAME=\"transparentAtStart\" VALUE=true>");
oBuilder.Append("</OBJECT>");
string
strBuilder=oBuilder.ToString();
strBuilder=strBuilder.Replace("#BroadcastLink#",strBroadcastLink);
strBuilder=strBuilder.Replace("#Height#",strHeight);
strBuilder=strBuilder.Replace("#Width#",strWidth);
htWriter.Write(strBuilder);
}
}
}  
using
System;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
ASPNET.StarterKit.Communities;
//*********************************************************************
//
// AudioVideo Class
//
// Represents an individual AudioVideo content.
//
// _skinFileName = the name of the skin to use for this section
//
// _getContentItem = the name of the method that retrieves the content item
//
//*********************************************************************
public class
AudioVideo : ContentItemPage
{
string
_skinFileName = "AudioVideos_AudioVideo.ascx";
GetContentItemDelegate _getContentItem =
new
GetContentItemDelegate
AudioVideoUtility.GetAudioVideo);
//*********************************************************************
//
// Article Constructor
//
// Assigns skin and contentItems method to base ContentItemPage class
//
//*********************************************************************
public AudioVideo() : base
()
{
SkinFileName = _skinFileName;
GetContentItem = _getContentItem;
}
}
}

Figure 7 ( Source code
for the ItemAVTitle class
)

Additionally, we have created an another custom control "AudioVideoEditControl" (figure 7) which
is
used to navigate to other related pages from section page.

namespace ASPNET.StarterKit.Communities.AudioVideos
{
using
System;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
ASPNET.StarterKit.Communities;
//*********************************************************************
// AudioVideo Class
//
// Represents an individual AudioVideo content.
//
// _skinFileName = the name of the skin to use for this section
//
// _getContentItem = the name of the method that retrieves the content item
//
//*********************************************************************
public class
AudioVideo : ContentItemPage
{
string
_skinFileName = "AudioVideos_AudioVideo.ascx";
GetContentItemDelegate _getContentItem =
new
GetContentItemDelegate
AudioVideoUtility.GetAudioVideo);
//*********************************************************************
//
// Article Constructor
//
// Assigns skin and contentItems method to base ContentItemPage class
//
//*********************************************************************
public AudioVideo() : base
()
{
SkinFileName = _skinFileName;
GetContentItem = _getContentItem;
}
}
}
}

Figure 10 (Class WMPlayer)

We have used the following sql statement to register this page.

/*registers AudioVideo as page type*/
IF NOT EXISTS (SELECT * FROM Community_PageTypes WHERE pageType_Name='AudioVideo')
BEGIN
INSERT INTO Community_PageTypes
(
pageType_Name,
pageType_Description,
pageType_PageContent,
pageType_isSectionType
)
VALUES
(
'AudioVideo',
'Contains a single AudioVideo content',
'ASPNET.StarterKit.Communities.AudioVideos.AudioVideo',
0
)
END
ELSE

PRINT
'WARNING: The AudioVideo has already been registered.'
Go

Implement the page for editing a particular AudioVideo -content

This last step is very similar to step 5 (page for insert) ;therefore, I give only the names of skin and class which are used to implement this funtionlity.

Skin name: "AudioVideos_AddAudioVideo.ascx"
Control class: EditAudioVideo which is derived from the ContentEditPage class .

Use following sql statement to register the edit page.

/* register AudioVideo_EditAudioVideo.aspx*/
Insert INTO Community_NamedPages
(
namedpage_path ,
namedpage_pageContent ,
namedpage_title ,
namedpage_description ,
namedpage_sortOrder ,
namedpage_isVisible ,
namedpage_name ,
namedPage_menuID
)
VALUES
(
'/AudioVideos_EditAudioVideo.aspx',
'ASPNET.StarterKit.Communities.AudioVideos.EditAudioVideo',
'edits an AudioVideo record',
'Enables users to edit a particular audivideo',
0,
1,
'Edits a particular AudioVideo record',
0
)
Go

Folks, That's all you need to do add a new module . Good speed!!

Reference

1. [CSK Architecture ] Community Starter kit Architecture.
(
http://www.asp.net/StarterKits/DownloadCommunity.aspx?tabindex=0&tabid=1 )

2. [Martin Fowler 2002] Martin Fowler :Patterns of Enterprise Application Architecture
(
http://www.martinfowler.com/books.html )

3. [Intercepting Filter] A MSDN Article
(
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnpatterns/html/DesInterceptingFilter.asp )

4. [Front Controller] A MSDN Article
(
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnpatterns/html/DesFrontController.asp )