An ASP.NET System To Allow Site Members To Contribute Content, Part 1: Overview
Crowdsourcing is all the rage these days, and even if you’re not managing a social media Web site, sometimes it’s helpful to accept content from end users.
For example, one of my clients has a community calendar on its Web site. Since the inception of the calendar, staff time had been devoted to retyping e-mailed and snail-mailed items into that calendar’s back end.
That was almost entirely wasted time, which my client rightfully wanted applied to something more profitable. My client wanted to allow staff to approve, edit or delete calendar submissions before they appeared on the site, but asked me to shift the burden of actually adding items directly onto the shoulders of site visitors.
Thanks to ASP.NET’s built-in membership system, we can easily provide a simple system for allowing end users to provide content. Not only that, but thanks to the role-based permissions incorporated into membership, we can even presort content to specific sections of the site, based on who is submitting it; grant specific users or user groups the ability to bypass an approval process; throttle contribution allowances; basically, any permission or restriction you might want to use.
(Aside: We can similarly implement a model like this in PHP, but it does not have a built-in membership provider. I may, at some later date, describe building a PHP membership provider that is similar to the ASP.NET model, at least in terms of practical use, if not mechanically similar.)
I am going to make a simple cancellations notification system as my demo.
After all, everyone wants to know if school is closed, or whether the play is still on in spite of the weather. Because canceling school, play, etc. generally comes down to a single person’s decision — or, at most, a few people — we can easily provide a system to log in, select a few options or enter a bit of text, and save everyone the time and grief such notifications otherwise take.
The specific features I will demo, in this and upcoming blog entries, will be:
- an administrative interface to add, edit and delete memberships;
- another administrative interface to add, edit or delete membership roles (i.e., membership groups), and to assign members to those groups, as well as to assign users to specific schools, organizations, etc.;
- an administrative interface to approve, edit or delete cancellation notices;
- a private form to allow membership to post cancellations for the schools, organizations, etc. with which they have been associated;
- a public view of cancellations that have been approved for viewing.
Continue reading: An ASP.NET System To Allow Site Members To Contribute Content, Part 1: Overview »
Dynamically Adding JavaScript To Your ASP.NET Master Page From A Child Page
A common challenge when working with ASP.NET master pages is how to dynamically add JavaScript that is relevant to a specific child page.
In other words, maybe your site has 10 child pages that share one master page / template. One of them is, let’s say, a contact us / directions page, and on that page, you want to display a map from one of the many mapping APIs out on the Web; for simplicity’s sake, let’s use the Google Maps API.
Proper implementation of the Google Maps API requires you to call, in the head section of your page’s HTML, the API library. So, you’re left with four options:
- Import the library in the master page’s code for all child pages, and thus incur that overhead for every page using that master page / template;
- Create a second version of the master page, containing the code, and apply that master to the child page that needs it;
- Don’t use a master page for the page that needs the API; have that erstwhile “child” page contain all the HTML it needs; or
- Figure out a way to add the needed code to the head of the master page for the child that needs it.
Option 1 seems reasonable, but it’s messy; although simply bringing in the Google Maps API for every page isn’t resource-intensive in this strong-computer, fast-bandwidth world, it’s sloppy at best and a potential security risk at worst (although the Google Maps API is pretty much safe to import, even if you’re not going to use it).
Option 2 leaves you with what is fundamentally two versions of the same thing, which is pretty much the dictionary definition of “inelegant.” Option 3 isn’t any better.
Option 4 is the best approach, and thankfully, there are a couple of ways to do it in ASP.NET. I’m going to describe the way to do it by dynamically adding HTML elements to the master page’s head, but first, a quick digression on using a PlaceHolder control.
Continue reading: Dynamically Adding JavaScript To Your ASP.NET Master Page From A Child Page »
Posting Status Updates (Tweets) To A Twitter Profile Via ASP.NET
I have a client that posts several news stories to its Web site every day. So it makes tremendous sense for them to post the headlines to Twitter as tweets, so that followers might be informed of breaking news, or just what’s new on the site.
The Twitter API makes posting status updates (tweets) to Twitter as simple as invoking a WebRequest. Unfortunately, most of the ASP.NET examples on the Web that aim to show you how to post status updates are either written in C#, have coding errors, simply will not work, or all three.
So here’s a simple VB.NET subroutine that will post tweets.
Sub TwitIt(ByVal strUser As String, ByVal strPass As String, ByVal strMessage As String)
'this subroutine requires your ASP.NET page to have a label control with an ID of lblStatus
'create post variable for tweet
Dim strTweet As String = "status=" & Server.HtmlEncode(strMessage)
'convert post variable to byte array for transmission purposes
Dim bRequest As Byte() = System.Text.Encoding.ASCII.GetBytes(strTweet)
Try
'create HttpWebRequest to status update API resource
Dim objRequest As HttpWebRequest = WebRequest.Create("http://twitter.com/statuses/update.xml")
'pass basic authentication credentials
objRequest.Credentials = New NetworkCredential(strUser, strPass)
'set method to post and pass request as a form
objRequest.Method = "POST"
objRequest.ContentType = "application/x-www-form-urlencoded"
'tell the server it will not receive a 100 Continue HTTP response
objRequest.ServicePoint.Expect100Continue = False
'set content length of request
objRequest.ContentLength = bRequest.Length
'capture the stream (content) of the request
Dim objStream As Stream = objRequest.GetRequestStream()
'put the bytes into request
objStream.Write(bRequest, 0, bRequest.Length)
'close the stream to complete the request
objStream.Close()
'uncomment line below to report success
'lblStatus.Text = "Tweet sent!"
'You can also capture the XML response Twitter sends back
'uncomment lines below to capture responses
'Dim objResponse As WebResponse = objRequest.GetResponse()
'Dim objReader As New StreamReader(objResponse.GetResponseStream())
'lblStatus.Text = objReader.ReadToEnd()
Catch ex As Exception
'uncomment line below to report ASP.NET errors
'lblStatus.Text = ex.Message
End Try
End Sub
Continue reading: Posting Status Updates (Tweets) To A Twitter Profile Via ASP.NET »
Free Books, Just For The Asking
I’m clearing my bookshelves and have four books that I was planning to trash (since the local library doesn’t want them). Then I thought, what the heck, give ‘em away on the blog.
So, forthwith, I have the following four books available:
Associated Press Stylebook and Libel Manual (1998 Edition): This is a reference book used by reporters to ensure they spell things correctly, use words properly, capitalize appropriately, etc. I am replacing it with the latest edition; this one is still useful, especially for the casual writer.
Microsoft Visual Basic 2005 Step by Step (Step By Step (Microsoft)): An excellent book for the beginning programmer who wants to get up to speed quickly. It teaches you how to program by getting you to make real programs that employ each chapter’s lesson. Includes a CD-ROM with source code. I’m using Visual Studi0 2008, and this is mostly relevant, but I’ve pretty much outgrown its message.
Microsoft Expression Web Step by Step (Microsoft): Like the book above, teaches you how to use Expression Web with real-world examples you can employ. (This is the first edition of the book, not the one for Expression Web 2.) Also includes a CD-ROM with sample code. I got this as a gift but I don’t use Expression Web.
Professional ASP.NET 2.0 (Programmer to Programmer): An extremely big book that pretty much covers every aspect of ASP.NET 2.0 programming. Heavily thumbed but still in very good shape. I’ve used it for reference for a number of years and highly recommend it for programmers who are familiar with .NET programming but not ASP.NET, or for other experienced programmers who want a good desk reference. This one really isn’t appropriate for beginners.
These books are first-come, first-served and will be sent by media mail to US addresses only (be advised that media mail can take up to three weeks to arrive). I will pay the shipping; it’s totally free.
If you would like one of these books, post a comment on this blog with your real e-mail address. Provide me with the name of the book you want and a reason you want it. The first reason I like for each book will be contacted by e-mail for shipping info. This will continue until all four books are gone.
How I Code On This Blog: Elegance Vs. Transparency
My recent comment exchange with Scriptar has prodded me to post an explanation of how I code on this blog.
I view the code I post here as having two priorities: elegance and transparency, weighted slightly more toward elegance.
What I mean is that most importantly, the code I post should be as simple, compact and direct as possible. It should solve the problem with as few lines of code as possible, using as direct an approach to solving the problem as possible, and without introducing any additional steps or problems that need to be resolved.
For example, suppose I want to demonstrate how, in PHP, to concatenate an array into a string. I could do this:
$val = array('here', 'there', 'everywhere');
$out = "The words in the array are ";
foreach($val as $item) {
$out .= "$item, ";
}
$out = substr($out, 0, (strlen($out) - 2)) . ".";
echo $out;
But I’d almost always write the solution thus:
$val = array('here', 'there', 'everywhere');
echo "The words in the array are " . implode(", ", $val) . ".";
That is, I’d write it as the second example — since it’s far more elegant — unless doing it that way either removed the ability to make an important point about the way things work, or obfuscates some consideration that ought to be made plain.
Continue reading: How I Code On This Blog: Elegance Vs. Transparency »

