How do I refer to an html id in code?

末鹿安然 提交于 2019-12-11 18:47:41

问题


I have things like:

<form  id="Form1"> ... </form>

How do I refer to this form in my C# codebehind? I tried simply using "Form1" but it gives me an error. And googling hasn't helped either.

What I'm trying to achieve is to enter the item name or one of those fields dynamically on a google checkout "buy now" button. See: How to subscribe to click event of html form?.


回答1:


You need to run the form server side:

<form id="Form1" runat="server> ... </form>

However, if you are using ASP.NET Web Forms you can only have 1 form per web form running server side.


EDIT: After seeing your edit, I would recommend posting the values to a standalone value on your website using Response.Redirect():

Response.Redirect("GoogleCheckout.aspx?field=" + fieldvalue);

Then on this standalone page have the following:

<form action="https://sandbox.google.com/checkout/..." id="Form1" method="post" name="..." target="_top">
<input name="item_name_1" type="hidden" value="<%= Request.Querystring["field"] %>" />
...
<input alt="" src="https://sandbox.google.com/checkout/buttons/buy.gif?merchant_id=..." type="image" />
</form>

Then use javascript/jquery to automatically post this form onload:

$("form").submit();

This gets around the issue of only having 1 form per page.




回答2:


Add runat="server" to get that tag visible in codebehind.



来源:https://stackoverflow.com/questions/11119100/how-do-i-refer-to-an-html-id-in-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!