htmlasp.netvb.netsearchweb-controls

Get value of HTML5 input type search from code behind after postback in .NET


I'm creating a sample textbox for search like this (I'm using VB) in my webpage:

Dim txtSearchFilter As New HtmlGenericControl("input")
With txtSearchFilter
  .ID = "txtSearchFilter"
  .Attributes.Add("placeholder","Filter")
  .Attributes.Add("type","search")
  .Attributes.Add("maxlength","80")         
End With

It's great, because this is the HTML5 style box which has some useful features, such as the "x" on the right side of the box to clear the text entry. However, from the code behind, I can't retrieve the text that was entered. I've tried:

txtSearchFilter.InnerText
txtSearchFilter.InnerHtml
txtSearchFilter.Attributes("value")
Request.Form("txtSearchFilter")

But none of these work. Is there a way to get the value?

P.S., SO prevents me from selecting the HTML5 tag and, instead, puts in the HTML tag even though I selected the HTML5 tag.


Solution

  • Add a name attribute to the input element.

    txtSearchFilter.Attributes.Add("name", "txtSearchFilter")
    

    Then on postback, you can retrieve the value by Request.Form(name_attribute). Example based on the above code :

    Dim value as String = Request.Form("txtSearchFilter")