/ / stringbuilder文字列を使用して動的に作成されたテーブルに新しいハイパーリンクコントロールを追加する方法-c#、asp.net、.net、stringbuilder

stringbuilder文字列で動的に作成されたテーブルに新しいハイパーリンクコントロールを追加する方法 - c#、asp.net、.net、stringbuilder

私は動的に作成しているテーブルがあり、このテーブルのセルで私は使用しようとしている StringBuilder インスタンスを使用して、マスターページのコンテンツホルダーを指す画像として各セルに新しいハイパーリンクコントロールを追加します。 私のコードは、テーブル作成の一部です...

// 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;
}

この部分で...

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

そのため、使用できません。不明なコンストラクタなので、「実行することすらできません。これと似たようなことがありますが、Writeコマンドを使用しています。 各セルでその文字列をhtmlにレンダリングして、問題が発生する必要があります。

ありがとう

回答:

回答№1は0

それは本当です。そのようなコンストラクタはありません HyperLink それは StringBuilder パラメータとして。代わりに StringBuilder を使用してプレーンOOPを使用する必要があります HyperLink クラス、それが目的です。代わりにこれを行います...

//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);