I have a problem with a such simple thing as to get values from the Request.Form collection.
I created ASP.NET application using VS.NET and C# as a programming language. On the first webform I have <form id="test"> with the two textfields and one <asp:button>.
Within the button event I have
When I am trying to retrieve values from the "test" form in the "NameOfThePage.aspx" webform by using:
var = Context.Request.Form.Get("textBox1").ToString();
it gives me an error message in this line.
The error message is:
Object reference not set to an instance of an object
Could anyone explain me what is going on here.
Your help is very much appreciated.
The problem is you are mixing this with <asp: asp.net elements. Use one or the other, use all html forms with an action pointing to your script with the request stuff on it, or use all asp.net elements, leave out the action and jam all the scripts in the head section. With the asp.net elements you do not need to specify an action or anything like that, when the page is resent (which can be specified on many elements with autopostback and will happen by default with command buttons) the scripts at the top of your page will then execute, and those ids/names do not need ot be requested because the ids of asp.net elements are treated as variables thoughtout your page. You are thinking in terms of asp classic where you need to request the form attributes, but asp.net event aware controls keep you from having to do this and stream line you code a bit.
Forms in asp.net
Thanks for you help, but the problem is that I havn't really got you. Could you please give me a very simple code example how to do this. Maybe create form with one textfield and button in one webform and show me how to get value from the textfield in another webform. Just bear in mind that i am using Visual Studio.NET so my html part and code behind are separated.
Thanks in advance
P.S. Your answer is very important for me because I am a very beginner in asp.net and currently I got involved in a real life project where without this knowledge I can't proceed.I already went through a couple of books and I canít find the right answer.
Last edited by allagliner; 05-06-2004 at 07:47 PM.
Look, you have the asp.net elements, which start with a <asp: and then you have your standard html forms. Now, this asp.net elements output html form elements, but when using the asp.net elements, they are event aware. This means you can call a block of code based on the user clicking on the button. This can also be done with the html form elements in a way, but the key difference is that with the asp.net elements, you do not need to play with the form action, and you do not need to do a request.form. Plus even if you did, request.form will not work with a response.redirect, those form imputs are carried to the next page when the user clicks the button because the next page is the form action. So basically here is what you do, make all of your elements <asp: elements, then on the click of a command button call a sub within a sub that is at the top of you page or in a code behind and set some variables in the session, then if you need to redirect pass the variables over to the next page. But you can do all of your data base stuff right from the script up above, moving a bunch of crap to the next page would most likely be pointless. Do you own the books you have? You can't just scoure them for what you are looking for because what you are looking for cannot, and does not need to be done. You need to learn how to use asp.net, and that will mean either learning to work with a form action and html form elements, or the asp.net elements, or both, but not at the same time. You can use a server side text box and stuff like that, but you are going to have to specify a form action within a server side form and it could get really messy real fast. It probably will not work for you, but I am not going to say that is impossible because it could be.
I don't want to step on your toes PeOfEo (because you obviously know a hell of a lot more about ASP.NET than I do), but I get the feeling that the miss-understanding is that the Form data from the 1st page will be transferred over as part of the Response.Redirect to the 2nd page.
It's my understanding that the information does not get passed through to the 2nd, and that all Request.Form information will be empty.
I apologise if I have totally missed the point of the question.
I am not young enough to know everything - Oscar Wilde
If you use a form action, on a regular form, the data will be passed to the next page and you can use request.froms. If you use a server side form and asp.net elements, the data will be automatically passed by the server right back to the page the form is on, no second page what so ever. There would also be no need for request.forms.
I totally agree with you - that is exactly how I understand it to work.
But allagliner used Response.Redirect("NameOfThePage.aspx")
and that - I think - is where the miss-understanding lies. It's my belief that the Server Side redirection (using Response.Redirect) will not pass any of the form values over.
I am not young enough to know everything - Oscar Wilde
nope, not unless he redirects w/ a query string or sets a cookie or something. But its just that he is trying to do a request.forms while using a server side form. Its just uneccessary.
Hi everyone i guess that the solution is:
(in the button that send to the next page)
Private Sub btnContinuar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnContinuar.Click
(in the page that receives the values [in this case is NextPage.aspx])
Var1 = Page.Request.Form("campo1")
Var2 = Page.Request.Form("campo2")
Hope it helps...
Why posting to other form? use the same from and get the values from the
property it is better to implement the logic in the same form and If you really want to post to another page please read this article.
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)