Really Weird ASP.Net Reserved Request.Form key name - asp.net

I seem to have stumbled across a really weird issue when posting a static HTML page to an ASP.NET page:
Example:
<form action="Kiosk.aspx" method="post">
<input type="hidden" id="key" name="key" value="1234" />
<input type="hidden" id="action" name="action" value="download" />
<input id="btnGo" type="submit" value="Download" />
</form>
When I call it using
string strKioskKey = incomingWebContext.Request.Form["key"];
it always returns null - if I change both the parameters to "key1" it works sweet...
But the question is WHY? I have never heard of a reserved word for requests.

I cannot reproduce this. Your problem lies elsewhere.
You may wish to try and re-create the problem as a small standalone app, and see at what point you can reproduce it (run it locally, then on your dev/live environment).

Related

ASP.NET WebForms form not being posted - why?

Although I don't consider myself a web programmer, I've done a fair amount of web programming, so I'm almost embarrassed to ask what is wrong with the below code. There is something fundamental about ASP.NET that I must be missing.
I have two pages, source.aspx and destination.aspx:
source.aspx - html:
<body>
<form id="form1" action="destination.aspx" method="post" runat="server">
<input id="Text1" type="text" />
<input id="Text2" type="text" />
<input id="Checkbox1" type="checkbox" />
<input id="Submit1" type="submit" value="submit" />
</form>
</body>
destination.aspx - code behind:
protected void Page_Load(object sender, EventArgs e)
{
// Below variable gets assigned null.
string text1 = Request.Form["Text1"];
}
When I submit the source.aspx form, once it gets to the destination.aspx form, there is no information in the FORM variables. I thought that the forms 'runat="server" ' would ensure that I ended up in the ASP.NET page pipeline, and in fact I can step through this code. There are no POSTed form variables other than viewstate, and the PARAMs collection doesn't have anything corresponding to control data either, not even ones that would correspond to decorated control names. The question is, what is happening that is making my POSTed variables 'disappear', at least to the destination page?
I'm not looking for alternatives how to make this work (i.e. make the controls server controls with runat="server", etc). I can fix the problem. What I am trying to determine is 'what is it about ASP.NET that makes my controls not appear to be receivable by the destination page. Thanks - I thought that I understood HTTP pretty well, but there seems to be a little sleight of hand courtesy of ASP.NET that I'm not seeing.
You can remove the runat="server" off of your form tag since you want to opt out of what ASP.NET gives you with server controls. You are basically thinking correctly that this should work without needing all the ASP.NET page processing bits.
It's a small change you need to make - you need to use 'name' instead of 'id' on your input controls in order for them to appear in the Form collection. It's a subtle thing but I believe it's not exclusive to ASP.NET - the name attribute specifies what to associate the value with in the POST variable collection.
Consult HTML input - name vs. id for more information on id vs. name
Good luck
Request.Form uses name attribute from elements. So you should write name attributes to each of the html elements.
<body>
<form id="form1" action="destination.aspx" method="post" runat="server">
<input id="Text1" name="Text1" type="text" />
<input id="Text2" name="Text2" type="text" />
<input id="Checkbox1" name="Checkbox1" type="checkbox" />
<input id="Submit1" type="submit" value="submit" />
</form>
</body>

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.

Logging in to my site opens a new tab?

I've been working on my personal website as of late, and even thought it's not a big problem because I'm the only one that sees it, it's annoying nonetheless.
Whenever I go to my login page, it all works fine. If I enter a bad username/password, it will just print a simple text message error(intended). But, if I login correctly, then instead of redirecting to the root of my website as I intend, it will instead create a new tab in firefox and from there it will be in / (which is correct). The problem is why does it create a new tab? I didn't even think forms were capable of such a thing:
My full login page is at http://lastyearswishes.com/login The gist of it is:
<form method="post" target="/login">
Username: <input type="text" name="username" /> <br />
Password: <input type="password" name="password" /> <br />
<input type="submit" name="Submit" />
</form>
And my server side code is also very simple:
if(RouteID=="login"){
if(AuthenticationModule.Login(Form["username"],Form["password"],false)){
//logged in correctly
Response.Redirect("/"); //just go to the root of my site
}else{
Write("You fail at life");
}
}
«target="/login"» means "open in window named «/login»". If it doesn't exist, a new one is opened. I think you meant to use
<form method="post" action="/login">
HTML Form Element

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

Google search code in ASP.NET Master Page

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.

Resources