Creating an ASP.NET 2.0 Polling User Control: Creating Administration Web Pages
By Scott Mitchell
Creating an ASP.NET 2.0 Polling User Control |
---|
Many websites include some sort of polling user interface through which visitors can cast their vote on the topic du jour.
This article series examines a polling User Control. Readers are welcome to use this control in their own websites and
extend or customize it as needed.
|
Introduction
The polling User Control examined in the first two installments (Design Decisions and the Data Model and Building the Polling User Control) is functionally complete at this point. A page developer can add, edit, or delete new polls and poll answers by manually inserting, updating, or deleting records into or from the corresponding database tables; they can view a poll's results by running a query against the
UserResponses
table.
Having to work directly with the database in terms of SELECT
, INSERT
, UPDATE
, and
DELETE
queries can be a pain, of course, especially for commonly performed techniques. A more palatable option
is to provide a web-based interface for these common tasks. The download available at the end of each installment of this
article series includes, in addition to the polling User Control, a set of administration web pages that make it easy to
create, edit, and delete polls and poll questions and to view the results of any poll in the system.
In this third and final installment we'll examine these three administration pages' functionality, declarative markup, and code. Read on to learn more!
Administration Section Details
The polling administration pages are available in the download at the end of this article (as well as from the previous two installments). They are placed in the
~/Admin
directory, which includes a Web.config
file that
specifies authorization information. In particular, the demo application I've made available allows any authenticated
user to visit the administration pages. In short, I have configured Web.config
to only deny anonymous users:
<authorization>
|
Feel free to change these authorization rules to only allow a particular set of users or to limit access to a specific role
(or set of roles). For more information on specifying URL authorization through Web.config
, see:
ASP.NET Authorization and
Role-Based Authorization with Forms Authentication.
The three web pages in the administration section are:
Default.aspx
- lists all of the existing polls and enables the administrator to edit the poll, delete it, or view its results. The administrator can also add a new poll from here.EditPoll.aspx
- lists the details for a particular poll, including the poll question ("What is your favorite programming language?") and the poll's answers ("Visual Basic", "C#", "Pascal", and so forth). Existing poll answers may be edited or deleted; moreover, new poll answers may be added.PollResults.aspx
- displays the results for a specified poll.
Viewing, Deleting, and Creating Polls
The
Default.aspx
page in the ~/Admin
folder is responsible for listing all of the current polls
and enabling administrators to edit, delete them, or view their responses. This page also allows administrators to add a
new poll. The listing of the polls is accomplished by using a GridView control. The Edit and View Results columns are implemented
as HyperLinkFields: the Edit link sends users to EditPoll.aspx?pid=PollID
while View Results takes them
to PollResults.aspx?pid=PollID
.
The Delete button uses the GridView's built-in deleting functionality. In short, clicking the Delete button causes a postback
and invokes the corresponding SqlDataSource control's Delete()
method. The SqlDataSource control then
connects to the database and executes its DeleteCommand
, which deletes the particular record from the database.
I augmented the actual Delete LinkButton so that it includes a client-side confirmation before deleting. This can be
accomplished by setting the LinkButton's OnClientClick
property to the following JavaScript:
return confirm('message to display in confirm messagebox');
. See
Deleting a GridView's Underlying Data for more information
on implementing delete functionality in a GridView and for techniques for adding client-side confirmation.
What About Foreign Key Constraints? |
---|
When deleting a record from the Polls table we also need to delete the corresponding answers and responses from
the PollAnswers and UserResponses tables. This can be accomplished in one of two ways: by
executing multiple DELETE statements to first delete the needed UserResponses records, then
the PollAnswers , and then the Poll record; or we can use cascading deletes.
For this data model I enabled cascading deletes on the foreign key constraints. Consequently, we can delete a poll
and all of its related answers and responses with a single |
In addition to this page's GridView, there is a DetailsView that is used to add new polls to the system. The DetailsView can
support inserting capabilities because the SqlDataSource control's InsertCommand
is appropriately set. Furthermore,
the DetailView is permanently rendered in its inserting interface because its DefaultMode
property is set to
"Insert". Entering a new poll question and clicking the "Add a New Poll" button causes the new poll to be inserted and refreshes
the GridView to include the new poll in the list.

Editing, Deleting, and Adding Poll Answers
Clicking the Edit button for a poll takes users to
EditPoll.aspx
, passing the PollID
in the querystring.
EditPoll.aspx
allows the administrator to perform the following tasks:
- Change the poll's question text
- Edit an existing answer, updating its display text and sort order
- Delete an existing answer (along with any votes that may have been cast for it)
- Add a new answer to the poll
Polls
table, the second inserts data into the PollAnswers
table), two different SqlDataSource controls are used: PollDataSource
and PollAnswersDataSource
.
There's also a GridView on the page that lists the current poll's answers ordered by the SortOrder
column in
the same way the poll answers are displayed by the polling User Control. This GridView uses the same SqlDataSource as
the DetailsView for adding new answers (PollAnswersDataSource
). The Edit and Delete buttons utilize the
GridView's built-in editing and deleting capabilities. Refer to Accessing
and Updating Data in ASP.NET 2.0 for a more thorough discussion on the SqlDataSource and editing and deleting from
the GridView control.

Viewing Poll Results
The final administration page is
PollResults.aspx
, which displays the results of the poll whose PollID
is specified through the querystring parameter pid
. As we saw in the Building the Polling User
Control article, we can show a poll by simply dropping the polling User Control on the page and setting its PollID
property to the ID of the poll to display. The only caveat here is that the polling User Control will display the voting
interface if the person viewing the poll is authenticated and has not yet taken the poll. Therefore, an administrator
who wants to see a poll's results will instead see the voting interface (assuming he has yet to take the poll in question).
To remedy this, I added another property to the polling User Control called PollEnabled
. If this Boolean property
is set to True (the default) then poll displays the voting interface for authenticated users who have not yet taken the
poll and the read-only results interface for all others (this is the behavior we discussed in Building the Polling User
Control). If, however, PollEnabled
is set to False, the read-only results interface is displayed regardless
of whether the currently visiting user has taken the poll or not.
The PollEnabled
property has its value persisted in view state (so
that it would be remembered across postbacks if it was assigned programmatically). The property code, which is located in the
code-behind class of the polling User Control, is shown below:
public bool PollEnabled
|
I also updated the FormView's DataBound
event handler to show the results if PollEnabled
is
False. (Recall that the FormView's DataBound
event handler is where we decide what interface to show in
the polling User Control.)
With this property in place, the PollResults.aspx
page in the administration section can be correctly
implemented. The page contains the polling User Control whose PollEnabled
property is set to False.
In Page_Load
the User Control's PollID
is set to the pid
querystring value.
Consequently, this page will now display the results of the specified poll regardless of whether the administrator viewing
it has taken the poll or not.
... The User Control markup in PollResults.aspx ...
|

Conclusion
Over the course of this article and the previous two (Design Decisions and the Data Model and Building the Polling User Control) we looked at a polling User Control and associated administrative web pages. This started with a discussion of the design goals and a look at the data model. We then explored the steps of building the polling User Control and looked at how it could be used in an ASP.NET 2.0 web application. In this third and final installment we looked at the administration web pages.
You are welcome to use the polling User Control in your websites. Also, you are free to make any enhancements, customizations, or modifications to fit your particular requirements. If you do use this User Control I would appreciate some form of attribution, but this is just a friendly request and not a requirement for use.
Happy Programming!
Attachments
Creating an ASP.NET 2.0 Polling User Control |
---|
Many websites include some sort of polling user interface through which visitors can cast their vote on the topic du jour.
This article series examines a polling User Control. Readers are welcome to use this control in their own websites and
extend or customize it as needed.
|