Bibu
Bibu

Reputation: 201

make items from checkboxlist links to other pages

I have a checkboxlist.When I check one the value will appear in a table.Now I want that value and each value I check to make it a link.This is my code for getting the checked values:

foreach (ListItem item in check.Items)
            {
                if (item.Selected)
                {


                    TableRow row = new TableRow();
                    TableCell celula = new TableCell();
                    celula.Style.Add("width", "200px");
                    celula.Style.Add("background-color", "red");

                    //celula.RowSpan = 2;
                    celula.Text = item.Value.ToString();




                    row.Cells.Add(celula);

                    this.tabel.Rows.Add(row);

Now I want the item.value to make it a link..I'm using c# in asp.net application

Upvotes: 0

Views: 1099

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460238

Add a Hyperlink control to the celula.Controls collection instead of setting the TableCell's Text property.

// Create a Hyperlink Web server control and add it to the cell.
System.Web.UI.WebControls.HyperLink h = new HyperLink();
h.Text = item.Value;
string url = "~/Default.aspx?Item=" + Server.UrlEncode(item.Value);
h.NavigateUrl = url;
celula.Controls.Add(h);

Note that you need to recreate this table on every postback, so you might want to add this to Page_PreRender instead.

Upvotes: 2

Related Questions