fizzled
Regular
I'm using a Repeater to display the news articles on my site as per the following code:
I'd like to conditionally insert "Delete Article" links into each ItemTemplate when the currently logged in user is an administrator. I was just wondering what the best way to accomplish this would be.
Code:
<%@ Page Language="C#" Debug="True" ContentType="text/html" ResponseEncoding="iso-8859-1" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<%@ Register TagPrefix="BOW" TagName="PageHeader" src="siteheader.ascx" %>
<%@ Register TagPrefix="BOW" TagName="PageFooter" src="sitefooter.ascx" %>
<script runat="server">
public void Page_Load (Object Sender, EventArgs e) {
string qrySiteNews = "SELECT SN.NewsID, SN.Headline, M.Handle AS Author, SN.PostDateTime, SN.NewsText, SN.IconID FROM SiteNews AS SN INNER JOIN Members AS M ON SN.AuthorID = M.MemberID WHERE Display = @Display ORDER BY PostDateTime DESC";
bool bDisplay = true;
OleDbConnection cnMain = new OleDbConnection(Application.Contents["ConnectionString"].ToString());
OleDbCommand cmdMain = new OleDbCommand(qrySiteNews, cnMain);
OleDbDataReader rdrSiteNews;
// Retrieve existing news articles from database
cmdMain.Parameters.Clear();
cmdMain.Parameters.Add(new OleDbParameter("@Display", OleDbType.Boolean));
cmdMain.Parameters["@Display"].Value = bDisplay;
cnMain.Open();
rdrSiteNews = cmdMain.ExecuteReader();
// Determine if any news articles were returned
if (rdrSiteNews.HasRows) {
// At least 1 news article was returned, display news
rptrSiteNews.DataSource = rdrSiteNews;
rptrSiteNews.DataBind();
pnlSiteNews.Visible = true;
} else {
// No news articles were found, display 'no news' message
lnkErrorContact.HRef = "mailto:" + Application.Contents["AdminEmail"].ToString() + "?subject=BOW No News Displayed";
pnlNoNews.Visible = true;
}
rdrSiteNews.Close();
cnMain.Close();
// Determine if current user is an admin
if (Session.Contents["MemberID"] != null) {
if (Session.Contents["UserLevel"].ToString() == "Administrator") {
pnlAdminControl.Visible = true;
}
}
}
</script>
<BOW:PageHeader ID="hdrPageHeader" RunAt="server" />
<ASP:Panel ID="pnlAdminControl" Visible="false" RunAt="server">
<form name="frmAddNews" method="post" action="savenews.aspx?rp=index.aspx&proc=add">
<div id="divMainContent">
<div class="divContentItem">
<p class="pContentTitle">Add News Article</p>
<p class="pContentText">
Headline:<br/>
<input type="text" name="txtHeadline" size="50" maxlength="150" /><br/>
Article Text:<br/>
<textarea name="txtArticleText" rows="6" cols="40"></textarea><br/>
<input type="submit" name="cmdSaveNews" value="Add News" />
<input type="reset" name="cmdResetNews" value="Clear Form" /></p>
</div>
</div>
</form>
</ASP:Panel>
<ASP:Panel ID="pnlNoNews" Visible="false" RunAt="server">
<div id="divMainContent">
<div class="divContentItem">
<p class="pContentTitle">No Site News</p>
<p class="pContentText">No Site News was found in the Beans of Wrath database. This is most likely an error.</p>
<p class="pContentText">Please contact <a href="" id="lnkErrorContact" title="Contact Fizzled" runat="server">Fizzled</a> to report this issue.</p>
</div>
</div>
</ASP:Panel>
<ASP:Panel ID="pnlSiteNews" Visible="false" RunAt="server">
<ASP:Repeater ID="rptrSiteNews" RunAt="server">
<HeaderTemplate>
<div id="divMainContent">
</HeaderTemplate>
<ItemTemplate>
<div class="divContentItem">
<p class="pContentTitle"><%# DataBinder.Eval(Container.DataItem, "Headline") %></p>
<p class="pContentSubinfo"><%# DataBinder.Eval(Container.DataItem, "Author") %>,
<%# DataBinder.Eval(Container.DataItem, "PostDateTime") %></p>
<p class="pContentText"><%# DataBinder.Eval(Container.DataItem, "NewsText") %></p>
</div>
</ItemTemplate>
<FooterTemplate>
</div>
</FooterTemplate>
</ASP:Repeater>
</ASP:Panel>
<BOW:PageFooter ID="ftrPageFooter" RunAt="server" />
I'd like to conditionally insert "Delete Article" links into each ItemTemplate when the currently logged in user is an administrator. I was just wondering what the best way to accomplish this would be.
Last edited: