Click here to Skip to main content
14,385,176 members
Rate this:
Please Sign up or sign in to vote.
See more:

Here i got one requirement i have to bind coma seperated value to one linkbutton in a grid view.....but the problem is each coma separated value i have to get as each link button in same row of a grid view.....because if i click each link button(ie each coma seperated value) it should redirect to another page.....

please help me.....
Mahesh Bailwal 16-May-13 8:04am
Can you please provide sample data?
Member 9155255 16-May-13 8:46am
grdTest.DataSource = c.ds.Tables[0];

here in that above query STRAGG(TNAME) contains coma separated value such as xray test,sugar test...this thing i am binding to the grid with linkbutton...but the problem is linkbutton holds all the coma separated value at a time...but this thing i dont want....i need each one means if i click xray,xray related page should open....if i click sugartest,sugartest related page should many coma separated value will come each should have unique link button ......please help me...
Rate this:
Please Sign up or sign in to vote.

Solution 1

Try something like this:

<asp:TemplateField ...>

   <asp:Repeater runat="server" DataSource='<%# Eval("CsvColumn", "{0}").Split(",") %>'>
      <asp:HyperLink runat="server"

         NavigateUrl='<%# "~/yourpage.aspx?value=" + HttpUtility.UrlEncode((string)Container.DataItem) %>'

         Text='<%# Container.DataItem %>'


Rate this:
Please Sign up or sign in to vote.

Solution 2

Below is the code snippet to give you an idea of one of the way to solve this problem. Let me know if you need some clarification.

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            //Index of the cell
            int cellIndex = 0;
            if (e.Row.RowType == DataControlRowType.DataRow)
                string content = e.Row.Cells[cellIndex].Text;
               var arr = content.Split(new char[] { ',' });
               StringBuilder sb = new StringBuilder();
                foreach (var item in arr)
                   switch (item)
                       case "xray test":
                           sb.Append("<a href=xray.aspx> xray test </a>");
                       case "sugar test":
                           sb.Append("<a href=sugar.aspx> sugar test </a>");


                e.Row.Cells[cellIndex].Text = sb.ToString();
Rate this:
Please Sign up or sign in to vote.

Solution 4

You can add the Itemtemplate to the grid dynamically.
Add Placeholder and Link buttons in the place holder.

<form id="form1" runat="server">
    <asp:GridView ID="gvTest" AutoGenerateColumns="false" runat="server">


In the code behind just add the item template.

public partial class _Default : System.Web.UI.Page
        private static DataTable tblTemp = new DataTable();
        protected void Page_Load(object sender, EventArgs e)
        public void BindGrid()
            DataTable tbl = new DataTable();
            tbl.Rows.Add("Mumbai,Pune,Nasik", "Maharashtra");
            tbl.Rows.Add("Amritsar,Jalandhar", "Punjab");
            tblTemp = tbl;
            TemplateField objTemplateFieldState = new TemplateField();
            objTemplateFieldState.HeaderText = "State";
            objTemplateFieldState.HeaderStyle.Width = Unit.Percentage(30);
            objTemplateFieldState.ItemTemplate = new CreateItemTemplate(ListItemType.Item, new Label());
            TemplateField objTemplateFieldCities = new TemplateField();
            objTemplateFieldCities.HeaderText = "Cities";
            objTemplateFieldCities.HeaderStyle.Width = Unit.Percentage(30);
            objTemplateFieldCities.ItemTemplate = new CreateItemTemplate(ListItemType.Item, new PlaceHolder());
            gvTest.DataSource = tbl;
        public class CreateItemTemplate : ITemplate
            //Field to store the ListItemType value
            private ListItemType myListItemType;
            private Control cntrlType;
            int i = 0;
            public CreateItemTemplate()
            //Parameterrised constructor
            public CreateItemTemplate(ListItemType Item, Control cntrlTypeTemp)
                myListItemType = Item;                
                cntrlType = cntrlTypeTemp;
            //Overwrite the InstantiateIn() function of the ITemplate interface.
            public void InstantiateIn(System.Web.UI.Control container)
                //Code to create the ItemTemplate and its field.
                if (myListItemType == ListItemType.Item)
                    if (cntrlType is PlaceHolder)
                        PlaceHolder phCities = new PlaceHolder();
                        string[] strCities = tblTemp.Rows[i][0].ToString().Split(',');
                        foreach (string strCity in strCities)
                            LinkButton lnkCity = new LinkButton();
                            lnkCity.Text = strCity.Trim();
//Here you can add link or event for every link button
                            Literal ltr = new Literal();
                            ltr.Text = " | ";
                    if (cntrlType is Label)
                        Label lblState = new Label();
                        lblState.Text = tblTemp.Rows[i][1].ToString();

It's simple and will help you to create seperate events for every link button.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100