Note that there are some explanatory texts on larger screens.

plurals
  1. POConverting listview to a composite control
    primarykey
    data
    text
    <p>The link below shows a listview composite control in its most basic form however I can't seem to extend this to what I'm trying to do.</p> <p><a href="https://stackoverflow.com/questions/92689/how-to-define-listview-templates-in-code">How to define listview templates in code</a></p> <p>My listview has 1 tablerow with 2 fields. The first field contains 1 element whilst the second field contains 2 elements as shown below.</p> <pre><code>&lt;asp:ListView ID="lstArticle" runat="server"&gt; &lt;LayoutTemplate&gt; &lt;table id="itemPlaceholder" runat="server"&gt; &lt;/table&gt; &lt;/LayoutTemplate&gt; &lt;ItemTemplate&gt; &lt;div class="ctrlArticleList_rptStandardItem"&gt; &lt;table width="100%"&gt; &lt;tr&gt; &lt;td valign="top" class="ctrlArticleList_firstColumnWidth"&gt; &lt;a href="&lt;%# GetURL(Container.DataItem) %&gt;"&gt; &lt;%# GetImage(Container.DataItem)%&gt; &lt;/a&gt; &lt;/td&gt; &lt;td valign="top"&gt; &lt;span class="ctrlArticleList_title"&gt; &lt;a href="&lt;%# GetURL(Container.DataItem) %&gt;"&gt; &lt;%# DataBinder.Eval(Container.DataItem, "Title") %&gt; &lt;/a&gt; &lt;/span&gt; &lt;span class="ctrlArticleList_date"&gt; &lt;%# convertToShortDate(DataBinder.Eval(Container.DataItem, "ActiveDate"))%&gt; &lt;/span&gt; &lt;div class="ctrlArticleList_standFirst"&gt; &lt;%# DataBinder.Eval(Container.DataItem, "StandFirst")%&gt; &lt;/div&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/div&gt; &lt;/ItemTemplate&gt; </code></pre> <p>So to convert this I have to use the InstantiateIn method of the ITemplate for the layouttemplate and the ItemTemplate. Data is bound to the itemtemplate using the DataBinding event. </p> <p>My question at the moment is how do I setup the ItemTemplate class and bind the values from the above ListView ItemTemplate. This is what I've managed so far:</p> <pre><code>private class LayoutTemplate : ITemplate { public void InstantiateIn(Control container) { var table = new HtmlGenericControl("table") { ID = "itemPlaceholder" }; container.Controls.Add(table); } } private class ItemTemplate : ITemplate { public void InstantiateIn(Control container) { var tableRow = new HtmlGenericControl("tr"); //first field in row var tableFieldImage = new HtmlGenericControl("td"); var imageLink = new HtmlGenericControl("a"); imageLink.ID = "imageLink"; tableRow.Controls.Add(imageLink); // second field in row var tableFieldTitle = new HtmlGenericControl("td"); var title = new HtmlGenericControl("a"); tableFieldTitle.Controls.Add(title); tableRow.Controls.Add(tableFieldTitle); //Bind the data with the controls tableRow.DataBinding += BindData; //add the controls to the container container.Controls.Add(tableRow); } public void BindData(object sender, EventArgs e) { var container = (HtmlGenericControl)sender; var dataItem = ((ListViewDataItem)container.NamingContainer).DataItem; container.Controls.Add(new Literal() { Text = dataItem.ToString() }); } </code></pre> <p>One of the functions in the code behind used in the ListView just for an example is:</p> <pre><code> public string GetURL(object objArticle) { ArticleItem articleItem = (ArticleItem)objArticle; Article article = new Article(Guid.Empty, articleItem.ContentVersionID); return GetArticleURL(article); } </code></pre> <p><strong>Summary</strong></p> <p>What do I need to do to convert the following into a composite control:</p> <p>GetURL(Container.DataItem) GetImage(Container.DataItem)</p> <p>GetURL(Container.DataItem) DataBinder.Eval(Container.DataItem, "Title")</p> <p>convertToShortDate(DataBinder.Eval(Container.DataItem, "ActiveDate"))</p> <p>DataBinder.Eval(Container.DataItem, "StandFirst")</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload