[英]ASP.NET Button OnClick not firing without setting UseSubmitBehavior=“false”
[英]asp.net setting onclick of input button dynamically
我有一些看起來像這樣的動態按鈕:
<input type="button" name="button" id="button" value="Click me"
onclick="window.open('somelink');" />
我需要能夠從后面的代碼中更改“ somelink”。
怎么做? 我知道Request.Form["button"]
獲取按鈕,但是如何設置onlick屬性?
添加runat =“ server”,您可以從后面的代碼訪問該鏈接
<input runat="server" type="button" name="button" id="button" value="Click me"
onclick="window.open('somelink');" />
如果要更改onclick屬性,則可以在后面的代碼中執行此操作
button.Attributes.Add("onclick", "window.open('someOtherLink');")
應在頁面加載時(而不是在提交表單時)更改按鈕上的單擊處理程序。 從C#訪問按鈕之前,需要添加runat="server"
。 之后,您可以訪問其屬性:
的HTML
<input runat="server" type="button" name="button" id="button" value="Click me"
onclick="window.open('somelink');" />
C#
button.Attributes.Add("onclick", "window.open('[DynamicValueHere]');");
你可以這樣做:
<asp:Button id="button" Text="Click me" OnClick="window.open('somelink');" />
和
從后面的代碼
button.Attributes.Add("onclick", "window.open('somelink');");
然后,您可以添加所需的任何內容來代替somelink
。
而已。
你為什么不像這樣的window.open(<%=SomeUrl%>)
在window.open
內部傳遞<%=SomeUrl%>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.