【问题标题】:Calling a function in code behind using the anchor tag is not working使用锚标记在代码后面调用函数不起作用
【发布时间】:2017-06-22 03:35:27
【问题描述】:

我正在尝试通过在后面的代码中将锚标记动态创建到标签中来显示用于调用函数的链接,从而将“NEXT”链接添加到测验面板上。普通链接:

<a href=\"#\" runat=\"server\"onserverclick=\"getTestG6Ques\" style=\"text-decoration: none; color: #000000;\">NEXT</a>

适用于 aspx 页面,它已经是内容的一部分。它调用该函数并提出下一个问题。虽然它的位置并不理想。当我将此代码从 aspx.cs 页面插入标签时,会显示“NEXT”链接,但它不会调用该函数,尽管页面会刷新。代码需要在标签内才能放置在正确的位置。

testLabel.Text = "<span style=\"margin:0.5em 0.5em 0.5em 0.5em; display:block;\">" + "<b>Test Quiz Grade 6: " + testTopic + "<span style=\"margin-left:5em;\"><a href=\"#\" runat=\"server\"onserverclick=\"getTestG6Ques\" style=\"text-decoration: none; color: #000000;\">NEXT</a></span>" + "</b><br /><br />" + dataReader["question"].ToString() + "</span>";

【问题讨论】:

    标签: c# html dynamic hyperlink


    【解决方案1】:

    我测试了您的代码并且它有效。你唯一的问题是写这部分:

    runat=\"server\"onserverclick=\ ...
    

    你必须在它们之间放一个空格,所以你的锚标记是这样的:

    <a href=\"#\" runat=\"server\" onserverclick=\"getTestG6Ques\" style=\"text-decoration: none; color: #000000;\">NEXT</a>
    

    它会调用该方法。只有一件事:getTestG6Ques 必须是公共的或受保护的 void

    protected void getTestG6Ques(object sender, EventArgs e)
    {
        // To do some thing.
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-12
      • 1970-01-01
      • 2018-07-26
      • 2013-12-02
      • 2011-02-16
      相关资源
      最近更新 更多