0

我在 c# 中生成了一堆链接标签。我想要的是用一个 URL 填充 textBox1,每个链接标签都不同。我将如何生成动态事件?这是一个例子:

foreach (var node in nodes)
{
    HtmlAttribute att = node.Attributes["href"];
    HtmlAgilityPack.HtmlDocument tempDoc = new HtmlAgilityPack.HtmlDocument();
    tempDoc.LoadHtml(node.InnerHtml);
    var tempNode = tempDoc.DocumentNode.SelectSingleNode("//img[@alt]");
    HtmlAttribute tempAtt = tempNode.Attributes["alt"];
    LinkLabel ll = new LinkLabel();
    ll.Location = new Point(20, 20 * i);
    ll.Text = tempAtt.Value;
    this.Controls.Add(ll);
    i++;
}

节点文本应为tempAtt.Value,单击时 textBox1 应填充att.Value

4

1 回答 1

2

您不能直接将 dat 传递给事件,您必须以其他方式从处理程序内部获取它。

foreach (var node in nodes)
{
    ...
    LinkLabel ll = new LinkLabel();
    ...
    ll.Click += MyLabelClickHandler;
    this.Controls.Add(ll);

    i++;
}

void MyLabelClickHandler(object sender, Eventargs e)
{
    senderLabel = sender as LinkLabel;
    string text = senderlabel.text;
    ....
}
于 2012-05-23T11:33:25.100 回答