Google search code in ASP.NET Master Page - asp.net

I'm trying to put the following Google generated search box code into a Master page on a site:
<form action="http://www.google.com/cse" id="cse-search-box">
<div>
<input type="hidden" name="cx" value="partner-pub-xxxxxxxxxx:u3qsil-l6ut" />
<input type="hidden" name="ie" value="ISO-8859-1" />
<input type="text" name="q" size="31" />
<input type="submit" name="sa" value="Search" />
</div>
</form>
<script type="text/javascript" src="http://www.google.com/coop/cse/brand?form=cse-search-box&lang=en"></script>
The problem, I believe, is something to do with the form tags. I've tried putting this code inside a user control and embedding that in the master page but no luck yet...

Do you by any chance have a "form runat=server" wrapping around where your user control is being placed?
That would cause a problem.

ASP.NET likes to enclose the entire page with a Form tag, and you can't nest form tags...So move it outside of the <form runat="Server"> tag and you'll be fine.

Related

Bootstrap file upload not working with ASP Ajax AsyncFileUpload

So I am having an issue on my ASP web form, where I am using Bootstrap and trying to make use of the Ajax AsyncFileUpload control. The file upload control looks like this:
<div id="LoafFileUploadTemplate" class="custom-file">
<asp:AsyncFileUpload ID="LoafFileUpload" runat="server" ClientIDMode="Static" CssClass="custom-file-input file-upload" enctype="multipart/form-data" method="post" OnClientUploadStarted="Loaf_UploadStarted" OnClientUploadComplete="Loaf_UploadComplete" OnUploadedComplete="LoafFileUpload_UploadedComplete" />
<label class="custom-file-label">Choose File</label>
</div>
<asp:Button ID="LoafFileUploadComplete" runat="server" ClientIDMode="Static" CssClass="d-none" OnClick="LoafFileUploadComplete_Click" />
For some reason the LoafFileUpload_UploadedComplete method didn't fire when the uploader was finished. I've read that I need to add enctype="multipart/form-data" method="post" to the file input. I have done that, but still doesn't work. Turns out the way bootstrap renders the AsyncFileUpload control, these two attributes get applied to a div instead of the file upload control, as seen below:
<div id="LoafFileUploadTemplate" class="custom-file">
<div id="LoafFileUpload" class="custom-file-input file-upload" enctype="multipart/form-data" method="post">
<input type="hidden" name="ctl00$CPH_main$CsatoltFajlok$LoafFileUpload$ctl00"><div><input name="ctl00$CPH_main$CsatoltFajlok$LoafFileUpload$ctl02" type="file" id="ctl00_CPH_main_CsatoltFajlok_LoafFileUpload_ctl02" style="" size="20"></div>
</div>
<label class="custom-file-label">Fájl feltöltése</label>
</div>
</div>
<input type="submit" name="ctl00$CPH_main$CsatoltFajlok$LoafFileUploadComplete" value="" id="LoafFileUploadComplete" class="d-none">
So as seen here, the enctype="multipart/form-data" method="post" attributes are applied onto the div containing the file input, not the input itself. Because of this, the file doesn't get posted to the server. Anyones know a fix for this?
Turns out I didn't need to add enctype="multipart/form-data" method="post" to the file input, but to the form itself (in my case, in the master page). It works now.

Cross-page posting. Is it a good pratice to use PreviousPage in Asp.net?

I found this article on MSDN talking about Cross-page posting.
I never worked with Page.PreviousPage. But I think its interesting.
Do you use it? Is it a good pratice, or a bad idea?
What do you think about?
Thanks.
The cross page posting is a helper to post some data to a different page and still have the asp.net code behind functionality.
Why is this exist ? because asp.net have a limitation of one and only form per page. But actually to an html page you can have many forms and many different post to different pages.
So to give a tool to that case, is let you set a second page to post the data, and you setup this on the Button (and not by placing second form), and from there is solve this issue, to post the data to a different page.
For example... with out asp.net and with simple html on a page you can do that.
<body>
<form method="post" action="samepage.html">
Username: <input type="text" name="user" />
<input type="submit" value="Submit" />
</form>
<form method="post" action="page_b.html">
email for news letter: <input type="text" name="email" />
<input type="submit" value="Submit" />
</form>
</body>
To solve a situation like this, and because asp.net not allow two forms at the same page, gives this option.
<body>
<form id="form1" runat="server">
Username: <asp:TextBox runat="server" ID="Name" />
<asp:Button runat="server"/>
email for news letter: <asp:TextBox runat="server" ID="email" />
<asp:Button runat="server" PostBackUrl="page_b.aspx" />
</form>
</body>
In the second case, you have one form, but you set the PostBackUrl to a different page, and from there asp.net still handle the data on code behind direct on a second page (with out redirect).
I hope this example gives you and an idea where to really use the previous page. Also what is more usually is the Redirect, how ever there are case that you need to have the result to a different page. So its per case if you use it or not.

Long delay when submiting form onload?

To post data to a remote payment server, my website generates a page that looks like this:
<html>
<head>
</head>
<body onload="document.form1.submit()">
<form name="form1" method="post" action="https://secure.paymentsite.com/purchase" >
<input type="hidden" name="instId" value="1">
<input type="hidden" name="itemId" value="23">
<input type="hidden" name="currency" value="USD">
<input type="hidden" name="amount" value="9">
</form>
</body>
</html>
I generate a page like this so that I can log the choice the user has made to my database before redirecting the user to an external site to pay.
However, it takes around 6 seconds for the form to be submited once the page loads. During this time, the user is displayed a plain white screen, and may wonder if the website is broken.
Is it possible to make a form that submits as soon as the page has loaded?
jquery's $(document).ready(function(){....} executes when the DOM has finished loading. That would be one of many solutions.

Form tag in .Net doesn't work if alone

I have just create a blank .net web form page and added a simple html form, it reads:
<body>
<form id="form1" runat="server">
<div>
<form method="post" action="2ndpage.aspx">
<input type="text" name="Value" />
<input type="submit" value="click" />
</form>
</div>
</form>
</body>
If I click the button I'm not taken to 2ndpage.aspx. But If I add "<form></form>" just after the <div> so that the body now reads:
<body>
<form id="form1" runat="server">
<div>
<form></form>
<form method="post" action="2ndpage.aspx">
<input type="text" name="Value" />
<input type="submit" value="click" />
</form>
</div>
</form>
</body>
it works and I'm taken to 2ndpage.aspx when click.
I can look at this for another month but I'm sure I couldn't see where the bug is, I'd need a pair of fresh eyes.
Any ideas?
Thanks
you cant place form inside another form!
There's a good article here - https://web.archive.org/web/20170420110433/http://anderwald.info/internet/nesting-form-tags-in-xhtml/
It discusses not being able to nest one form in another form and also deals specifically with the scenario you describe.
You can't have nested form tags.
I'm not quite sure why it works when you add another empty form tag. But my guess is that the parser simply ignores the start of the tag and sees the end tag as the end tag for the first form. And therefore doesn't see it as nested forms anymore.

Make Google Searchbox code compatible with ASP.NET WebForms

I was provided the following code to integrate into my ASP.NET WebForms page:
<form action="http://www.google.com/cse" id="cse-search-box" target="_blank">
<div>
<input type="hidden" name="cx" value="partner-pub-8127518365728966:9snx3s9v6fx" />
<input type="hidden" name="ie" value="ISO-8859-1" />
<input type="text" name="q" size="25" /><br />
<input type="submit" name="sa" value="Search" class="formoutput"/>
</div>
</form>
<script type="text/javascript" src="http://www.google.com/cse/brand?form=cse-search-box&lang=en"></script>
However, I'm not sure how to do this because of the extra form element that this poses. Has somebody translated this to work with ASP.NET WebForms previously, and if so can you help me out?
Thanks!
Take a look here: http://am22tech.com/s/22/Blogs/post/2010/05/26/How-can-I-add-Google-search-box-to-my-website-on-master-page-with-aspnet.aspx
It's a bit of a hack but the idea is to do the following:
Place the search box in a separate HTML page
Modify the Target and Action properties of the form
Add an iFrame to your main page with the source of the above HTML page
The article is pretty detailed and, if you don't mind using an iFrame, then it should answer your question.
Update
Scott Mitchell has posted a very detailed article that does not use iFrames. It looks like more work to implement but may be the better choice.
URL: http://dotnetslackers.com/articles/aspnet/Implementing-Search-in-ASP-NET-with-Google-Custom-Search.aspx

Resources