0

I am still fairy new to C# development and have a question about creating objects.

I am working on a website and I want to pull form values in the behind code after submit. Ultimately I am trying to convert a page to a user control.

Now I am trying to make an instance of a Page object (System.Web.UI.Page) so I can access the Request property of the object and read the submit values.

My problem is, it always throws a NullReferenceException and doesn't read the submitted values

Here is my code:

<form id="myForm" runat="server" > 
Name: <input type="text" name="name" id="name" /> 
<input type="submit" value="Submit Name" /> 
</form>


public partial class testing1 : BasePage
{
// Created an instance of Page Object
public System.Web.UI.Page requestVar;

protected void Page_Load(object sender, EventArgs e)
{
    try
    {
        // Try and use request, throws null exception
        string holder = requestVar.Request["name"];
    }
    catch (NullReferenceException)
    { }
}

}

Any idea why I get the null exception? What would be the correct way to create a Page object so I can use it's Request property?

Please let me know, Thank you!

Learn12
  • 206
  • 1
  • 3
  • 11
  • Why do you need to create a Page in the first place? The Request is what actually holds the information you're looking for, and is [available from the user control](https://msdn.microsoft.com/en-us/library/system.web.ui.usercontrol.request(v=vs.110).aspx). – mason Nov 29 '16 at 20:10
  • Im not sure, I thought I needed to make an instance of the page object. So as long as I have the System.Web using statements, I dont need to create a new instance correct? – Learn12 Nov 29 '16 at 20:18
  • 1
    What type does `BasePage` inherit from? If you're in a user control, you can use the Request property that's already available. – mason Nov 29 '16 at 20:25
  • Thanks for clarifying my understanding mason. It helped me understand. – Learn12 Nov 29 '16 at 20:43
  • Are you learning Web Forms because your current company uses it? If not, then you should learn ASP.NET MVC instead of Web Forms. It's the more modern development stack and follows better design principles. – mason Nov 29 '16 at 20:45
  • Yes, unfortunately i'm working in code that uses pretty old standards. Trying to get rid of our iFrames... – Learn12 Dec 01 '16 at 18:51

2 Answers2

1

Use method="POST" in the form tag.

<form id="myForm" runat="server" method="POST" > 
    Name: <input type="text" name="name" id="name" /> 
          <input type="submit" value="Submit Name" /> 
</form>

and in code use Request.Form["name"]

try
{
    string holder = Request.Form["name"];
}
catch (Exception ex)
{ }
nobody
  • 10,892
  • 8
  • 45
  • 63
  • Ok this seems to work. Should I always use POST if I am trying to request the query string in the behind code? – Learn12 Nov 29 '16 at 20:17
  • These are form variables and not querystring variables.You will use/access form variables in this way when the form is submitted,meaning when there is a postback. – nobody Nov 29 '16 at 20:27
  • I see thank you for the insight, I will carry this in the future. – Learn12 Nov 29 '16 at 20:42
-1

I am surprised that your code compiled. Normally the compiler can detect uninitialized variables. I'm guessing your example left some stuff out.

Anyway, to address your specific question, change

public System.Web.UI.Page requestVar;

to

public System.Web.UI.Page requestVar = new Page();

That being said, you don't need a Page object. If you need to access the request, you can obtain a reference to it using

var request = HttpContext.Current.Request;
var someVariable = request["ParamName"];
John Wu
  • 50,556
  • 8
  • 44
  • 80