Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Real - World ASP .NET—Building a Content Management System - StephenR. G. Fraser

.pdf
Скачиваний:
68
Добавлен:
24.05.2014
Размер:
4.59 Mб
Скачать

You will see as you play with AutList that the states of the buttons change depending on which version you are viewing. The most current version allows you to update, remove, and move to the previous version (if there is more than one version), but you will notice that the Next Version button is grayed out and you can't click it. When you are on a prior version, the Update and Remove buttons disappear, and the Next Version button is enabled. Depending on whether there are more versions, the Previous Version button might be grayed out or enabled. (See Figure 11-11.)

Figure 11-11: The AutView Web page

If you look at the design in Listing 11-15, you will see that it contains no code to handle any of this logic. In fact, it is simple. It is just a table with headings, labels, and five buttons.

Listing 11-15: The AutView Web Design

<form id=AutView method=post runat="server">

<H2><FONT color=darkslategray >Author : Content View</FONT></H2>

<P>

<TABLE cellSpacing=1 cellPadding=1 width="90%" border=1>

<TR>

<TD style="WIDTH: 20%"><STRONG>ContentID: </STRONG></TD >

<TD>

<asp:label id=lbContentID runat="server" Width="100%">

</asp:label>

</TD>

</TR >

<TR>

<TD style="WIDTH: 20%"><STRONG>Version: </STRONG></TD >

<TD>

<asp:Label id=lbVersion runat="server">

</asp:Label>

</TD>

</TR >

<TR>

<TD style="WIDTH: 20%"><STRONG>Headline:</STRONG></TD>

<TD>

<asp:label id=lbHeadline runat="server" Width="100%">

</asp:label>

</TD>

</TR > <TR>

<TD style="WIDTH: 20%"><STRONG>Byline: </STRONG></TD > <TD width="100%">

<asp:label id=lbByline runat="server" Width="100%"> </asp:label>

</TD>

</TR > <TR>

<TD style="WIDTH: 20%"><STRONG>Teaser:</STRONG></TD > <TD>

<asp:label id=lbTeaser runat="server" Width="100%"> </asp:label>

</TD>

</TR > <TR>

<TD style="WIDTH: 20%"><STRONG>Body:</STRONG></TD > <TD>

<asp:label id=lbBody runat="server" Width="100%"> </asp:label>

</TD>

</TR > <TR>

<TD style="WIDTH: 20%"><STRONG>Tagline:</STRONG></TD> <TD>

<asp:label id=lbTagline runat="server" Width="100%"> </asp:label>

</TD>

</TR > <TR>

<TD style="WIDTH: 20%"><STRONG>Status:</STRONG></TD> <TD>

<asp:label id=lbStatus runat="server" Width="100%"> </asp:label>

</TD>

</TR > <TR>

<TD style="WIDTH: 20%"><STRONG>Update User:</STRONG></TD> <TD>

<asp:Label id=lbUpdateUser runat="server" Width="100%"> </asp:Label>

</TD>

</TR >

<TR>

<TD style="WIDTH: 20%"><STRONG>Modified Date: </STRONG></TD >

<TD>

<asp:Label id=lbModifiedDate runat="server" Width="100%">

</asp:Label>

</TD>

</TR >

<TR>

<TD style="WIDTH: 20%"><STRONG>Creation Date: </STRONG></TD >

<TD>

<asp:Label id=lbCreationDate runat="server" Width="100%">

</asp:Label>

</TD>

</TR >

</TABLE>

</P>

<asp:Button id=bnReturn runat="server" Text="Return" >

</asp:Button>

 

<asp:Button id=bnNext runat="server" Text="Next Version">

</asp:Button>

 

<asp:Button id=bnPrevious runat=" server" Text="Previous Version" >

</asp:Button>

 

<asp:Button id=bnUpdate runat="server" Text="Update" >

</asp:Button>

 

<asp:Button id=bnRemove runat="server" Text="Remove" >

</asp:Button>

</form>

The AutView Codebehind

The code for loading the AutView Web form (see Listing 11-16) is standard: Get the data and place labels in a table. The first time the page is loaded, the version is set to 0. Because the content is retrieved in descending order, the first row is the most recent version. Later, when a user clicks the Next Version and Previous Version buttons, which both map to the bnMove_Click() method, a different version is loaded in the table. The version loaded is determined by the argument stored in the CommandEventArgs parameter.

Listing 11-16: The AutView Loading View Table

private void Page_Load(object sender, System.EventArgs e)

{

int cid = Convert.ToInt32(Request.QueryString["ContentID"]);

if (cid == 0)

{

Response.Redirect("error.aspx?ErrMsg=" +

HttpUtility.UrlEncode("ContentID Missing"));

}

dt = new Content(

new AppEnv(Context).GetConnection()).GetContentForID(cid);

if (!IsPostBack)

{

BuildPage(0);

}

}

private void BuildPage(int cver)

{

DataRow dr = dt.Rows[cver];

lbContentID.Text = dr["ContentID"].ToString(); lbVersion.Text = dr["Version"].ToString(); lbHeadline.Text = dr["Headline"].ToString(); lbByline.Text = dr["Byline"].ToString(); lbTeaser.Text = dr["Teaser"].ToString(); lbBody.Text = dr["Body"].ToString(); lbTagline.Text = dr["Tagline"].ToString(); lbStatus.Text = dr["Status"].ToString();

lbUpdateUser.Text = dr["UpdateUserID"].ToString(); lbModifiedDate.Text = dr["ModifiedDate"].ToString(); lbCreationDate.Text = dr["CreationDate"].ToString();

...

}

private void bnMove_Click(object sender, CommandEventArgs e)

{

BuildPage(Convert.ToInt16(e.CommandArgument));

}

Hiding and Enabling Buttons

The BuildPage() method (see Listing 11-17) shows how the changes in the button states occur.

Listing 11-17: The AutView's BuildPage Method

private void BuildPage(int cver)

{

...

if (cver > 0)

{

bnNext.Enabled = true; int tmp = cver - 1;

bnNext.CommandArgument = tmp.ToString();

}

else

bnNext.Enabled = false;

if (cver < dt.Rows.Count-1)

{

bnPrevious.Enabled = true; int tmp = cver + 1;

bnPrevious.CommandArgument = tmp.ToString();

}

else

bnPrevious.Enabled = false;

bnUpdate.Visible =

(cver == 0 && Convert.ToInt32(dr["Status"]) == StatusCodes.Creating); bnRemove.Visible =

(cver == 0 && Convert.ToInt32(dr["Status"]) == StatusCodes.Creating);

}

Two options exist when it comes to stopping a user from using a button: Disable it (gray it out) or don't display it. In the case of the Next Version and Previous Version buttons, I decided to simply gray them out. To do this, just set the button control's Enabled property to false. For grins and giggles, in the cases of the Update and Remove buttons, I decided to not display the buttons at all. To do this, you simply set the button's

Visible property to false.

The only other thing that the BuildPage() method does of note is set the Next Version and Previous Version buttons' CommandArgument to its next and previous values, which will be retrieved later (as stated previously) when the buttons are clicked.

Removing Content

Normally, you wouldn't give an author the authority to remove content from a CMS system. Most likely, you might allow him to suggest that a piece of content should be removed. Usually, to do this, the remove process would set some sort of remove flag. The status flag would do nicely for this purpose.

Usually only someone with a high level of authority should be able to remove anything from the CMS repository.

For now, because the author is also the administrator, I am allowing the remove process to actually delete the content from the database. Plus, in early stages of development, a lot of test data is added to the database, and it needs to be removed. Without this function, there is no way to delete the content within CMS.NET. And hey, it also enables me to show you how to code a database remove process.

The AutRemove Web Page

The design for AutRemove.aspx, as you can see in Figure 11-12, contains nothing you haven't already covered. Display the headline and body so that the author can verify that it is the content to be removed. Write a warning message of how dangerous this step is and then provide two buttons: Remove Content or Cancel. Listing 11-18 shows that the design to accomplish this is short and sweet.

Figure 11-12: The AutRemove Web page

Listing 11-18: The AutRemove Web Design

<form id=AutRemove method=post runat="server">

<H2><FONT color=darkslategray >Author : Content Removal</FONT></H2>

<P>

<asp:label id=lbWhichHeadline runat="server" Font-Bold="True">

</asp:label>

<BR>

<asp:label id=lbWhichBody runat="server" Width="75%" BorderStyle="Inset">

</asp:label>

</P>

<P>

<B>Warning:</B>

This will permanently remove this version of the content. Are you really sure you want to do this?

</P>

<P>

<asp:button id=bnRemove runat="server" Text="Remove Content">

</asp:button>

 

<asp:button id=bnCancel runat="server" Text="Cancel">

</asp:button>

</P>

</form>

The AutRemove Codebehind

You have already seen most of AutRemove's code (see Listing 11-19) in some form or another in the preceding listing. The process is simple. It gets the ContentID passed to it. If the Web page was called without a ContentID, the error.aspx Web page is shown. Otherwise, the content is retrieved from the database, and the headline and body are placed in labels so that they can be displayed.

Listing 11-19: The AutRemove Web Page Codebehind

private void Page_Load(object sender, System.EventArgs e)

{

cid = Convert.ToInt32(Request.QueryString["ContentID"]);

if (cid == 0)

{

Response.Redirect("error.aspx?ErrMsg=" +

HttpUtility.UrlEncode("ContentID Missing"));

}

content = new Content(new AppEnv(Context).GetConnection());

dt = content.GetContentForID(cid);

lbWhichHeadline.Text = dt.Rows[0]["Headline"].ToString();

lbWhichBody.Text = dt.Rows[0]["Body"].ToString();

}

private void bnCancel_Click(object sender, System.EventArgs e)

{

Response.Redirect("AutList.aspx");

}

private void bnRemove_Click(object sender, System.EventArgs e)

{

content.RemoveContent(Convert.ToInt32(dt.Rows[0]["ContentID"]),

Convert.ToInt32(dt.Rows[0]["Version"]));

Response.Redirect("AutList.aspx");

}

When the user decides to cancel and not remove the displayed content, the bnCancel_Click() method is called, redirecting the user back to the AutList Web page.

When the user decides to remove the content, the bnRemove_Click() method is called. It, in turn, calls the RemoveContent() database table help method. When the helper function returns, the user is redirected to the AutList Web page.

The RemoveContent() method (see Listing 11-20), like all helper functions that call stored procedures, is quite simple. Tell the SqlCommand which stored procedure to run, set up and populate any parameters, and then execute the stored procedure.

Listing 11-20: The Content Database Table Helper RemoveContent

public void RemoveContent(int cid, int ver)

{

//DELETE FROM Content

//WHERE ContentID=@cid

//AND Version=@ver

SqlCommand Command = new SqlCommand("Content_Remove", m_Connection);

Command.CommandType = CommandType.StoredProcedure;

Command.Parameters.Add(new SqlParameter("@cid", SqlDbType.Int));

Command.Parameters.Add(new SqlParameter("@ver", SqlDbType.Int));

Command.Parameters["@cid"].Value = cid;

Command.Parameters["@ver"].Value = ver;

try

{

m_Connection.Open();

Command.ExecuteNonQuery();

}

finally

{

m_Connection.Close();

}

}

Submitting Content

The submitting process for now is just a placeholder. There is no one or no place to which to submit the content. Later, this function will send an e-mail to the next role, telling it that the content is available. For now, however, it will simply change the status from creating to submitted.

After this code is executed, you will see that you can no longer update or remove the content because all the options to do so have been disabled. To gain access again to those functions, just submit the same piece of content again. (Like I said, it is just a stub for now.)

The AutSubmit Web Page

As you can see in Figure 11-13, AutSubmit.aspx is virtually identical to AutRemove.aspx. In fact, I just copied it and changed the text and the button IDs. I am providing Listing 1121 just so that you can see where I made the changes.

Figure 11-13: The AutSubmit Web page

Listing 11-21: The AutSubmit Web Design

<FORM id=AutSubmit method=post runat="server" >

<H2><FONT color=darkslategray >Author : Content Submittal</FONT></H2>

<P>

<asp:label id=lbWhichHeadline runat="server" Font-Bold="True">

</asp:label>

<BR>

<asp:label id=lbWhichBody runat="server" Width="75%" BorderStyle="Inset">

</asp:label>

</P>

<P>

Normally, submitting would make the content no longer available to the author except if it is returned by whomever it was submitted to. But

for now the submit process will simply be a toggle.

</P>

<P>

<asp:button id=bnSubmit runat="server" Text="Submit Content">

</asp:button>

 

<asp:button id=bnCancel runat="server" Text="Cancel">

</asp:button>

</P>

</FORM>

The AutSubmit Codebehind

AutSubmit's code for submitting is almost exactly the same as AutRemove's. The only way it differs is that it has a bnSubmit_Click() method (see Listing 11-22) instead of a bnRemove_Click() method. Also, this method gets the ContentID and the Version and then toggles the Status by calling the SetStatus() content database table helper method (see Listing 11-23).

Соседние файлы в предмете Программирование