-1

Well, I'm trying to do a simple command: get the text from TextBox. I've searched for that, but every answer failed for me.

In my code aspx:

<form id="form1" runat="server" method="post">
    <div class="form">

        <div class="form-search ngen-search-form">

            <span id="search-trigger" class="form-search-submit">
                <img src="Imagens/Lupa_Icon 2.png" id="lupa"/>
            </span>

            <asp:TextBox ID="txtBoxSearch" runat="server" CssClass="form-search-input" placeholder="Pesquise..." ViewStateMode="Enabled"/>

        </div>

    </div>
</form>

I have a Javascript function that verifies what key my client pressed:

$(document).keypress(function(e) {
        if(e.which == 13){

            if( document.getElementById("txtBoxSearch").value == "" ) return true;

            else {

                <% setSearch(); %>

                document.getElementById("testeArv").innerHTML='<%=search.ToString()%>';

                return false;

            }

        }// First if

});

And finally, my function setSearch() in code behind is:

public void setSearch( )
{
    if( !Page.IsPostBack ) {

        search = txtBoxSearch.Text;

    }
}
Elson Jr
  • 3
  • 1
  • 3

3 Answers3

2

Main mistake is about how to call codebehind method in jQuery. Take a look here to know how to do it. Summary: you need ajax

Then you will have to keep in mind that it does Page.IsPostBack.

With !Page.IsPostBack you are saying "first page access" but when you press the button !Page.IsPostBack will be false. If you try the same example so:

public void setSearch( )
{
    if(!Page.IsPostBack) {
        // First page access
        search = txtBoxSearch.Text;
    } else {
        // The page has been reloaded
        search = txtBoxSearch.Text;
      }
}

It will work. (The else will run)

I am not sure but I have tried it (without jQuery) with a button and the same error occurs. You want to run the function when the Enter is pressed so the page will do the reload. Take a look here.

Maybe it helps you in some way.

Community
  • 1
  • 1
ricopo
  • 455
  • 6
  • 17
0

It's the <form> tag blocking your way to get the value in post back. Remove the <form> tag and use AutoPostBack=true for your text box. You won't need to use jQuery to catch the event and the value.

Geek Sado
  • 1
  • 1
-2

You can't call code behind code from client-side code. They execute on different machines.