Search code examples
c#asp.netrequest.form

Object reference not set to an instance of an object in Request.Form["ElementName"]


I want to access the value of an HTML input element in C#. The HTML Code is:

<input id="tbASPHTMLAdd" type="text" />

I need to push the value of this input box to an ASP Listbox, the code is here:

protected void bAddASPHTML_Click(object sender, EventArgs e)
{
    lbItems.Items.Add(Request.Form["tbASPHTMLAdd"].ToString());
}

But every time I came to the error:

Object reference not set to an instance of an object

When I remove the .ToString() it pushes me something in my listbox, but with no value

No, I do not want to add the runat="server" attribute. It is to demonstrate a few things.

Greets


Solution

  • You need to mention name attribute of textbox, ID is used for client purpose only, when you submit page all input element submit information with its name attribute as a key.

    <input id="tbASPHTMLAdd" type="text" name="tbASPHTMLAdd" />