DataRepeater suggestions

fizzled

Regular
Joined
Aug 9, 2004
Messages
52
Location
Atlanta, GA, USA
I'm using a Repeater to display the news articles on my site as per the following code:

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:
add a linkbutton to the item template. Bind its CommandArgument property to the id of the record, set its CommandName property to something line "Delete", write an eventhandler for the ItemCommand event, check for e.CommandName to see if its "Delete". If its so, use the e.CommandArgument to get the ID of the row and perform the delete
 
So I have:

Code:
      <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>
      <p class="pContentText"><ASP:LinkButton CommandName="Delete Article" CommandArgument="" RunAt="server" /></p>
    </div>
      </ItemTemplate>

Do I need to specify an ID for the LinkButton(s), and if so how do I make each one unique? Also, how do I "bind its CommandArgument property to the id of the record?" Is it simply:

Code:
<ASP:LinkButton CommandArgument="DataBinder.Eval(Container.DataItem, ""NewsID"")" />

And do I need an event handler for the LinkButton's OnCommand event, or the Repeater's OnItemCommand event?
 
Give the linkbutton any ID you want, ASP.NET will take care of generating unique ids for them at runtime.

And yes, thats how you bind the CommandArgument to the record ID. All what you need to do now is write an eventhandler to the ItemCommand event
 
This will require enclosing the Repeater inside a form with runat="server". However, if I do that, then the form tag will be sent regardless of whether the user is an administrator or not (and therefore regardless of whether or not there's actually any need for the form). How would I do this without sending a pointless form to the client if they are not an administrator?
 
I don't get it, so you're telling me you're not using a serverside form in your page??? This is impossible, since you're already using serverside controls.
 
Back
Top