0

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?.

Community
  • 1
  • 1
ispiro
  • 26,556
  • 38
  • 136
  • 291
  • @SteveB To change text there for example. – ispiro Jun 20 '12 at 12:24
  • your edit help us to understand your requirement. it's a perfect example of why it's important to say what you want to do, and not how (or at least not only). @Curt's answer reflect this. At first he answered exactly to your question, then, after your edit which changed the meaning of the question, he gave a very detailled answer. To conclude, always remember that people here are not in your head, and never hesitate to give more details than required. – Steve B Jun 20 '12 at 12:29

2 Answers2

2

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.

Curtis
  • 101,612
  • 66
  • 270
  • 352
  • I actually tried that. But it doesn't work even though there's only one form. But the page is using a master page which does have a form. Can that be the problem? – ispiro Jun 20 '12 at 12:05
  • @ispiro Yes that'll be the problem. You can't use more than 1 form ran at server (one of the major restrictions with .NET web forms). Perhaps post more details of what you are trying to achieve here. Theres probably a decent enough workaround with your existing Master Page form. – Curtis Jun 20 '12 at 12:07
  • Thanks. I haven't tried it yet, but this seems to be the answer. Thanks for the details. I wouldn't have been able to do them alone. By the way – is there no way to submit the second page from its codebehind on FormLoad? – ispiro Jun 20 '12 at 12:21
  • @ispiro Not that I know of but might be worth looking into – Curtis Jun 20 '12 at 12:23
1

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

MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263