Personal Website Starter Kit - Adding A Links Table - Part 2 - Submit Links
| |
| The Bush Survival Guide - 250 Ways to Make It Through The NExt Four Years Without Misunderestimating the Dangers Ahead, and Other Subliminable Stategeries |
|
Links:
| |
Subjects > Computers (Search for Computers) > Microsoft (Search for Microsoft) > Visual Studio 2005 Beta (Search) > Personal Website Starter Kit
Visual Studio 2005 includes a Personal Website Starter Kit as a sample web application. This is a nice simple application. Maybe even too simple! One of the first things I wanted to do in this sample is to update it to include a table drive links page with categorized lists of links.
This is part two of my article about extending the Personal Website Starter Kit to include a table driven list of categorized links. If you haven't followed the first part of the article, Adding A Links Table (Search), please go back and follow it's instructions. This part builds on the work that was done in the first part.
In this second part, we're going to add two important features:
At this point we have an architectural choice to make: Should we continue adding more functionality to the Links.aspx page, including this ability to submit new links, or should we add a new page for handling submission of links?
Having everything in a single file has some benefits: It keeps everything centralized, it cuts down the number of files to be deployed, it can provide a nice central point that pulls together all the related features into one place. But it also has some drawbacks: Changes to one part could accidentally affect other parts, the page would need to contain state logic to select between different functional modes, and it would be hard for two programmers to work on different parts of the page, concerning different functions, at the same time.
Since we've already added a couple of new files, LinkManager?Create|Search.cs and LinkObjectTypes?Create|Search.cs, it doesn't hurt to add an extra file to handle the submit link functionality. Although our link submission will be designed to be open to anyone, having the links submit functionality in a separate file makes it easier to protect it and limit it's availability.
OK, let's get started.
Use Visual Studio 2005 to open the Personal Website Starter Kit from part 1 of this article.
Go to File -> New and select a new Web Form from the Visual Studio installed templates. Name it LinksSubmit?Create|Search.aspx, and select the language Visual C#. Also select "Place code in separate file" and "select master page", then click "Add". At the "Select a Master Page" dialog, select the "Default.master" page.
Go to the source mode, and use this for a title on the first line of the file:
Title="Your Name Here | Submit New Links"
Go to design mode, and type in "Submit New Link" at the top of the page. You might also make it larger, with a paragraph heading 4.
We should also add this new file to the website.map. Open web.sitemap. Look for the SiteMapNode?Create|Search for Links: <siteMapNode?Create|Search title="Links" url="Links.aspx" />
Lets change it so we can nest a page under it. Change it to look like this:
<siteMapNode?Create|Search title="Links" url="Links.aspx" > <siteMapNode?Create|Search title="Submit New" url="LinksSubmit?Create|Search.aspx"/> </siteMapNode?Create|Search>
Go to Links.aspx and just after the "Back..." link, add some text "Submit New Link..." and make it a link to LinksSubmit?Create|Search.aspx. You must also include the current CatID?Create|Search in the URL so it knows what category the link should be added to. To do this go to the source view, and make sure the link looks like this:
<a href="Links.aspx">Back...</a> <a href="LinksSubmit?Create|Search.aspx?CatID?Create|Search=<%= CatID?Create|Search %>">Add...</a>
Now save everything, go to the links page, check the link works to the new submit page, and make sure the breadcrumbs are correct.
Now we need a way to add a new link to the database, this means we need to add a new stored procedure, and a new routine to the LinkManager?Create|Search.
Go to the LinkManager?Create|Search.cs and add this routine:
public void AddLink?Create|Search(Link link)
{
command.CommandText?Create|Search = "AddLink?Create|Search";
command.Parameters.Add(new SqlParameter?Create|Search("@Url", link.Url));
command.Parameters.Add(new SqlParameter?Create|Search("@Domain", link.Domain));
command.Parameters.Add(new SqlParameter?Create|Search("@Title", link.Title));
command.Parameters.Add(new SqlParameter?Create|Search("@Description", link.Description));
command.Parameters.Add(new SqlParameter?Create|Search("@IsApproved?Create|Search", link.Approved));
command.Parameters.Add(new SqlParameter?Create|Search("@CatID?Create|Search", link.CatID?Create|Search));
// command.Parameters.Add(new SqlParameter?Create|Search("@IsApproved?Create|Search", IsApproved?Create|Search));
command.ExecuteNonQuery?Create|Search();
}
You'll also need to add set accessors to the Link object, and create a parameterless constructor in the LinkObjectTypes?Create|Search.cs. Replace the previous get accessors (read only) with the following:
public string Url
{
get { return _Url; }
set { _Url = value; }
}
public string Domain
{ get { return _Domain; }
set { _Domain = value; }
}
public string Title
{ get { return _Title; }
set { _Title = value; }
}
public string Description
{ get { return _Description; }
set { _Description = value; }
}
public bool Approved
{ get { return _Approved; }
set { _Approved = value; }
}
public int CatID?Create|Search
{ get { return _CatID?Create|Search; }
set { _CatID?Create|Search = value; }
}
public Link()
{
}
Then go to the Server Explorer, and right click on Stored Procedures, and click Add Procedure:
CREATE PROCEDURE dbo.AddLink?Create|Search
@Url nvarchar(128), @Domain nvarchar(70), @Title nvarchar(80), @Description nvarchar(255), @IsApproved?Create|Search bit, @CatID?Create|Search intAS
INSERT INTO [Links] ([Url],[Domain],[Title],[Description],[IsApproved?Create|Search],[CatID?Create|Search]) VALUES (@Url,@Domain,@Title,@Description,@IsApproved?Create|Search,@CatID?Create|Search) RETURN
Now upen the LinksSubmit?Create|Search.aspx.
Go to the Toolbox and drop an ObjectDataSource?Create|Search onto the FormView?Create|Search, and change it's ID to ObjectDataSourceLinks?Create|Search
Go to design view, and then drag and drop a DetailsView?Create|Search onto it. Change it's property ID to DetailsViewLink?Create|Search. From the DetailsViewLink?Create|Search tasks list, select "Choose Data Source" of our ObjectDataSourceLinks?Create|Search object.
Next click on Configure Data Source, choose LinkManager?Create|Search as the business object, and click next.
Then choose GetLinks?Create|Search for the Select function. You also want to connect AddLink?Create|Search as the procedure on the Insert tab. If you have any problem with this, you might have a compilation error in the LinksManager?Create|Search.cs file.
As we did earlier, connect the CatID?Create|Search parameter to the query string of the same name.
Try things out with a Debug -> Start With Debugging.
You should be able to add a new link to any category. If you check the Approved box, and then go to the proper links listing for that category, you should be able to see the new link in that category. You can also use the Server Explorer to look at the data in the links table by right clicking on it and selecting "Show Table Data"
Lets add a link back to the links list for the current category. Following the DetailsViewLink?Create|Search add this line to the source view of LinksSubmit?Create|Search.aspx:
Back to the <a href="Links.aspx?CatID?Create|Search=<%=CatID?Create|Search %>">links listing</a>...</p>
We need to setup the CatID?Create|Search variable. Go to LinksSubmit?Create|Search.aspx.cs andd at this at the begiining of the definition for LinksSubmit?Create|Search:
public string CatID?Create|Search;
Also add a Page_Load routine to set this variable:
protected void Page_Load(object sender, EventArgs?Create|Search e)
{
CatID?Create|Search = this.Request.Params.Get("CatID?Create|Search");
if (CatID?Create|Search == null)
{
CatID?Create|Search = "0";
}
}
First let's make the CatID?Create|Search field not visible since we do not want to allow it to be edited.
From the DetailView?Create|Search Tasks list, click on Edit Fields….
Now add an event to set the CatID?Create|Search to the proper value. Click on the DetailsViewLink?Create|Search object, then select events in the properties view. Double click on ItemInserting?Create|Search event. Put the following code in the event procedure:
e.Values["CatID?Create|Search"] = CatID?Create|Search;
We also might want to force the approved field to be invisible, and always set to false. You can use the DetailView?Create|Search task list, or you can edit the source to do this. Let's edit the source:
Click on Source view for the LinksSubmit?Create|Search.aspx:
Look for <asp:CheckBoxField?Create|Search DataField?Create|Search="Approved" HeaderText?Create|Search="Approved" ReadOnly?Create|Search="True" SortExpression?Create|Search="Approved" />
And change it to: <asp:CheckBoxField?Create|Search DataField?Create|Search="Approved" HeaderText?Create|Search="Approved" ReadOnly?Create|Search="True" SortExpression?Create|Search="Approved" Visible="False"/>
And add another line to the [DetailsViewLink ItemInserting]?Create in LinksSubmit?Create|Search.aspx.cs:
e.Values["Approved"]= false;
Now all new links must be approved by the administrator. You might test the website at this point, and make sure that a new link does not show up in the list for the category, and also check the database file to make sure the link was added there with an IsApproved?Create|Search status of false. If you still have a window to the database open, you might need to right click the Links table in the Server Explorer, and select Refresh from the menu.
What to do next?
|
Interested in Information Transfer?