Domanda

I have a table that I am dynamically creating, and in the cells of this table I'm trying to use a StringBuilder instance to add a new hyperlink control to each cell as an image that points to my content holder in my masterpage. My code is this for part of my table creation...

// Create a new cell and add it to the row.
TableCell tCell = new TableCell();


/* If the rowcounter is equal to the record numbers
* then it has to break because if not it will throw an error
* saying that there is no row at ending position */

if (rowCtr == rN)
    break;

string myStr = myDs.Tables[0].Rows[rowCtr]["SubjectName"].ToString();
string subjectid = myDs.Tables[0].Rows[rowCtr]["SubjectID"].ToString();
string iconUrl = myDs.Tables[0].Rows[rowCtr]["IconUrl"].ToString();

myHtmlString.Append("<asp:HyperLink id=" + subjectid + 
" runat='Server' ImageUrl=" + iconUrl + 
" NavigateUrl='~/Webform2.aspx'>HyperLink</asp:HyperLink>");

tCell.Controls.Add(new HyperLink(myHtmlString));
tRow.Cells.Add(tCell);
rowCtr++;

/* If the cellcount is 3 then it needs to break, if not then 
* you'll miss every 4rth record, don't know why. But this works */

 if (cellCtr == 3)
    {
        rowCtr = rowCtr - 1;
        break;
    }

with this part...

tCell.Controls.Add(new HyperLink(myHtmlString));

It won't allow me to use it because its an unknown constructor, so I can't even run it. I have seen something similar to this being done but using the Write command. I need to render that string to html in each cell and running into a problem.

Thanks

È stato utile?

Soluzione

That's true. There's no such constructor of HyperLink that takes in a StringBuilder as a parameter. Instead of using a StringBuilder you should use plain OOP by using the HyperLink class, that's what it is for. Do this instead...

//create the HyperLink control
HyperLink link = new HyperLink();

//set the related properties
link.ID = subjectid;
link.ImageUrl = iconUrl;
link.NavigateUrl = "~/Webform2.aspx";
link.Text = "HyperLink";

//add it to the cell's control collection
tCell.Controls.Add(link);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top