![](/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.