Pergunta

I think I'm getting blind....! What's wrong with the following code? With Visual Studio 2013 "searchBox" doesn't return a value but with VS 2008 it works well.

CODE BEHIND

Partial Class _Default
Inherits Page

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
           Response.Write(Request.Form("searchBox"))
End Sub

End Class

HTML PAGE

<%@ Page validateRequest="false" Title="Home Page" Language="VB" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeFile="Default.aspx.vb" Inherits="_Default" %> 
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">

<asp:TextBox ID="SearchBox" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Search" />
</asp:Content>
Foi útil?

Solução

It can't works with VS2008 either. As it is a server control, the actual client id of the control won't be searchbox but a concatenation of all parents ids of the control. Something like ctl00_somePanel_someContainer_SearchBox.

Request.Form will contain raw html form control's values, including the value generated by the client side version of the SearchBox, with its actual generated id.

To solve your issue, you can:

  • read the SearchBox.Text property instead of reading the Form object (probably the best option)
  • replace the server control by a pure client one (<input type='textbox' id='SearchBox'/>
  • fix the control ID using CliendIDMode, but I believe this is a poor option

Outras dicas

The easiest would be when you want to use Request.Form("searchBox") is to set it as follows:

<asp:TextBox ID="SearchBox" clientmode="Static" runat="server"></asp:TextBox>

This forces the object to be created with the name "SearchBox" that you set it too, not "ctl00_somePanel_someContainer_SearchBox" that is a name that only it knows how to interpret.

It is not the best way to access the object, but this will get you going the way you are doing it.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top