问题
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