在后面的代码中生成动态点击事件

时间:2015-06-03 19:18:20

标签: javascript c# asp.net sharepoint web-parts

我在sharepoint的visual webpart中生成动态html。

 htmlString = "<a href='#'  onclick='updateAge(" + itemId + ")'>";

我想在c#中为click事件编写click事件。 click事件应该接受参数。

如何实现这一目标?我可以通过编写函数轻松地完成这个javascript,但不知道我在后面的代码中是如何做的。

1 个答案:

答案 0 :(得分:1)

假设您已经解决了如何发送链接html并且您只需要发送Javascript,则可以使用Page.ClientScript.RegisterStartupScript

string script = @"function updateAge(itemId) { 
    // here write your logic!
    alert(itemId);
    someOtherFunction();
}

function someOtherFunction() {
    // your logic for this other function
}";
Page.ClientScript.RegisterStartupScript(GetType(), "myscript", script, true);