1

I have a textbox that I change values in depending on what's selected in my DropDownList. So far so good.

But when I want to get that text to code behind it retrieves nothing. This is because the server textboxcontrol have not changed its state or information within it.(I know the problem, not the solution)

The question is, how can i get the text that updates with javascript in codebehind? (I will submit a code example below) (NOTE: this is not my code, its just a simplified example)

<asp:Panel ID="AnswerPanel" runat="server" >
   <asp:TextBox ID="tbxExample" runat="server" ></asp:TextBox>                
</asp:Panel>
<asp:Button ID="btnSend" onClientClick="Example" runat="server" Text="Send" />

Jquery:

function Example{
    $('#tbxExample').val("Test");
}

CodeBehind

string ex = tbxExample.Text; // I want it to say "Test", but it comes out as ""
J.Olsson
  • 815
  • 3
  • 13
  • 29

2 Answers2

2

You can make a slight hack for this using ASP.NET hidden fields. You can create a JS variable that stores the hidden field, assign the value, then pull that value on the server-side:

Create a hidden field with an ID (in this example, the ID is hiddenTest):

JS:

$hiddenField = $("#<%= hiddenTest.ClientID %>");
$('#<%=tbxExample.ClientID%>').blur(function() {
    $hiddenField.val(this.value);
});

You should now be able to access that hidden field in the code behind, and it should have the correct value.

tymeJV
  • 103,943
  • 14
  • 161
  • 157
1

The rendered id of asp.net are diferent than the one you use coding and you can get it using the .ClientID as:

$('#<%=tbxExample.ClientID%>').val("Test");
Aristos
  • 66,005
  • 16
  • 114
  • 150
  • I think he wants it the other way around, from client to server. – Hanlet Escaño Jun 25 '13 at 22:20
  • 1
    @HanletEscaño Yes, but to make it work, to change the value using jQuery from the client is must have the correct id of the Text box – Aristos Jun 25 '13 at 22:21
  • That is not the problem. I know this, but the example i wrote is not my code as mentioned.. The Changing of the Value in the textbox is NOT the trouble. the trouble is to take the change of the textbox and carrie it over to server side – J.Olsson Jun 25 '13 at 22:22
  • 1
    @J.Olsson The moment you change it and you make post back, you must have the value on code behind, unless you change it on code behind somehow (eg you are not use the IsPostBack). – Aristos Jun 25 '13 at 22:24
  • @Aristos I dont want to use postbacks. Im currently using updatepanels to avoid that. – J.Olsson Jun 25 '13 at 22:29
  • @J.Olsson Can you please then show the actual code that you use on aspx page and on code behind ? I can not help if I view other code than the one you actually use. – Aristos Jun 25 '13 at 22:32
  • 1
    @J.Olsson, That's something you should have mentioned on the post, that you are using UpdatePanel and ScriptManager :) – Hanlet Escaño Jun 25 '13 at 22:32
  • Well I thought it didnt matter if i used updatepanel or not(and hey, it did't matter).. And to post a long example with my code would't help for readability either. THATS why I wrote a simplified version of the problem. My existing relevant code on the page including aspx is in my opinion to long to post. (So to downrate my OP, because i dont paste my code for readability issues is something extraordinary of y'all) – J.Olsson Jun 25 '13 at 23:11
  • @J.Olsson Its a different matter to give the correct source code than not give a critical part of the code. The UpdatePanel can change very much the functionality both client side and server side. Now if you have find a solution you must be happy, and everything is ok, you are not here for the votes. – Aristos Jun 25 '13 at 23:19