[英]Cannot get Javascript function to fire from a textbox in a gridview
我在Gridview的ItemTemplate中有一個文本框。 在_RowDataBound事件上,我將一個屬性添加到文本框中,如下所示:
TextBox txtQuantity = (TextBox)e.Row.FindControl("txtQuantity");
txtQuantity.Attributes.Add("onkeypress", "CheckInputForNumeric(event)");
而且它根本不會觸發JS函數。
我嘗試過onClick,onBlur,onKeyPress ...甚至嘗試將大小寫更改為:onclick,onblur,onkeypress ...似乎沒有什么可以觸發我的JS函數。
我在同一頁面上的其他地方:
txtAddMarkup.Attributes.Add("onkeypress", "CheckInputForNumeric(event)");
(該文本框不在gridview中),並且可以正常工作。
這時我完全陷入沮喪之中,因為無論我做什么,我都無法獲取此文本框來觸發JavaScript函數
請運行您的項目,並查看通過查看瀏覽器中的源(IE,Firefox,Safari等)生成的文本框的名稱。 您可能會看到文本框的名稱已更改。 謝謝,ASP。
您無法使用DOM來按名稱訪問元素,因為它們已為您重命名。
由於某種原因,刪除臨時Internet文件並重新加載頁面並沒有獲得最新的.js。 我不得不卸載該項目並重新構建它。 這很奇怪,因為我之前從未對其他控件執行過此操作
感謝您的投入!
試試這個:
TextBox txtQuantity = (TextBox)e.Row.FindControl("txtQuantity");
txtQuantity.Attributes.Add("onkeypress", "CheckInputForNumeric(event)");
txtQuantity.Attributes["onkeypress"] =
string.Format("javascript:CheckInputForNumeric(this,'{0}','{1}','{2}');", argument1, argument2, argument3);
或簡單地
txtQuantity.Attributes["onkeypress"] =
string.Format("javascript:CheckInputForNumeric (this);");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.