I have some dynamic buttons that look like this:
<input type="button" name="button" id="button" value="Click me"
onclick="window.open('somelink');" />
I need to be able to change 'somelink' from the code behind.
How is that done? I know Request.Form["button"]
gets the button but how to set the onlick property?
add runat="server" and you can access that link from code behind
<input runat="server" type="button" name="button" id="button" value="Click me"
onclick="window.open('somelink');" />
and if you want to change the onclick attribute then you do this in code behind
button.Attributes.Add("onclick", "window.open('someOtherLink');")
Changing the click handler on a button should be done when the page loads, not when the form is submitted. Before you can access your button from C#, you need to add runat="server"
. After that, you may access its properties:
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]');");
You can do this:
<asp:Button id="button" Text="Click me" OnClick="window.open('somelink');" />
and
from code behind
button.Attributes.Add("onclick", "window.open('somelink');");
And you can add whatever you want in place of somelink
.
that's it.
你为什么不像这样的window.open(<%=SomeUrl%>)
在window.open
内部传递<%=SomeUrl%>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.