views:

153

answers:

2

Hello

I've an item template inside a grid which has a <asp:LinkButton/> inside it. I assign the text for the link button as

<%# Convert.ToString(Eval("Tags"))%>

Tags can have a string with multiple tags in it delimited by space. For eg. "sports", "sports cricket", "sports cricket sachin" are the examples of some possible tags.

I want to create a button for each tag inside the string. How can i create the controls (server control - linkbutton) dynamically during runtime inside the grid item template?

Thank you

+1  A: 

I assuming the grid you are referring to is a data grid?

This tutorial has a great example. To summarise:

Add a Template column to your grid view in place of the link button column:

<asp:datagrid id="dataGrid1" runat="server" Width="792px" 

    AutoGenerateColumns="False" 
    CellPadding="0" >

    <Columns>


        <asp:BoundColumn DataField="id" HeaderText="ID"> 
            <HeaderStyle Width="190px" HorizontalAlign="Center" >
            </HeaderStyle> 
        </asp:BoundColumn> 


        <asp:TemplateColumn HeaderText="Tags" 
                     HeaderStyle-HorizontalAlign="Center"> 
            <ItemStyle HorizontalAlign="Left" Wrap="True"></ItemStyle> 
            <ItemTemplate> 
                <asp:Repeater ID="rptChild" runat="server" DataSource='<%# DataBinder.Eval(Container.DataItem, "tags").ToString().Split(tagSplitChars) %>'> 
                    <ItemTemplate> 
                        <asp:LinkButton ID="linkChild" 
                        runat="server" 
                            CommandArgument="<%# Container.DataItem%>"
                            > 
                            <%# Container.DataItem%> 
                        </asp:LinkButton> 
                    </ItemTemplate> 
                </asp:Repeater> 
            </ItemTemplate> 
        </asp:TemplateColumn> 
    </Columns> 
    <PagerStyle PageButtonCount="20" Mode="NumericPages"></PagerStyle> 
</asp:datagrid>

Note that tagSplitChars should be defined in your code behind as:

 protected char[] tagSplitChars  = new char[] { ' '};

Clearly you can add an "onclick" handler to you link button as you need.

I have tested this with this code behind and it works perfectly:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Demo
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        public char[] splitChars = new char[] { ' '};
        protected void Page_Load(object sender, EventArgs e)
        {
            dataGrid1.DataSource = new List<dynamic>() { new { id = 1, names = "one single" }, new { id = 2, names = "two double" } };
            dataGrid1.DataBind();
        }

    }
}

UPDATE

If you are just trying to add a list of buttons, and have no other columns to display in your grid, you can simplify the solution significantly:

<asp:Repeater ID="rptChild" runat="server" > 
    <ItemTemplate> 
        <asp:LinkButton ID="linkChild" runat="server"> 
             <%# Container.DataItem%> 
        </asp:LinkButton> 
    </ItemTemplate> 
</asp:Repeater> 

Then in the code behind

        protected void Page_Load(object sender, EventArgs e)
        {
            dataGrid1.DataSource = myTagsString.split(splitChars);
            dataGrid1.DataBind();
        }

Clearly you will have to access the data in your datatable manually to extract the string, haven't done this in a while but from memory it is quite simple.

Giles
How can i assign just the tags as a datasource to the whole grid? I've other columns? Tags is just a column on that datatable which is assigned to the grid (it is actually Telerik RadGrid). From that column i need to take the 'tags string' and need to split it to get the actual tags. Any thoughts?
NLV
If you assign a list of tags to your datagrid, then you can replace DataBinder.Eval(Container.DataItem, "tags").ToString().Split(tagSplitChars) with Container.DataItem.ToString().Split(tagSplitChars).If however you want 1 column of your datagrid to hold a list of tag linkbuttons then the code in the answer works.
Giles
I'm getting the following error when i include the repeater control - The server tag is not well formed. at System.Web.UI.TemplateParser.ProcessError(String message) at System.Web.UI.TemplateParser.DetectSpecialServerTagError(String text, Int32 textPos) at System.Web.UI.TemplateParser.ParseStringInternal(String text, Encoding fileEncoding) at System.Web.UI.TemplateParser.ParseString(String text, VirtualPath virtualPath, Encoding fileEncoding) Any ideas?
NLV
When i remove the repeater control the error vanishes. I'm not able to find a quote mismatch or something.
NLV
Okie i got it. Missed a closing tag.
NLV
A: 

If the tags reside in a single data table column, construct a select Sql query which fetches the tag values from it and assign the result data table or reader as grid source. If some some of the column values contain more than one tag separate with comma, split them at runtime as Giles proposed.

Dick Lampard