![](/img/trans.png)
[英]ASP.NET Click event does not work for multi buttons what created in tab button control
[英]Calling a WebMethod from the click event of dynamically added Button control in Asp.net does not work
我无法从动态添加的Button
控件的click
事件中调用 web 方法。 这是 C# 代码
public partial class Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
Button button = new Button();
button.Text = "Click Me.";
button.OnClientClick = "return Remove()";
pnlFiles.Controls.Add(button);
}
[WebMethod]
public void ClickEvent(int id)
{
}
}
这是 javascript
<script type="text/javascript"> function Remove() { $.ajax({ url:"Default.aspx/ClickEvent", data: "{'id':5}", type: "POST", cache: false, headers: { "cache-control": "no-cache" }, contentType: "application/json; charset=utf-8", dataType: "json", success: function (msg) { alert(msg); }, error: function (xhr, status, error) { } }); } </script>
<asp:Panel runat="server" ID="pnlFiles" />
非常感谢这方面的任何帮助。
[WebMethod]
public void static ClickEvent(int i)
{
}
我认为 WebMethod 应该是 static。 还使用 JSON.stringify 获取数据。 这应该可以解决问题。 如果没有,您可以尝试查看 chrome dev 控制台的网络选项卡中是否有任何错误。
注意:保持 c# 方法的参数名称与您在 json 主体中传递的参数相同。
jQuery $.ajax 错误响应文本为“身份验证失败”。 我在 RouteConfig.cs 中注释掉了以下行并且它起作用了。
jQuery $.ajax error response text is "Authentication Failed"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.