Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Changing the value of a textbox via javascript on dropdownlist change (no postback). Textbox updates properly but the value does not come through when the form is submitted. I assume this is because the javascript change is only happening on the client side? How can I get this value on server side?

What I have tried:

<pre>  <script type="text/javascript">
      function ddlChange() {
          var ddl = document.getElementById('<%=DDLg4corp.ClientID %>');
          var textBox = document.getElementById('<%= txtg4action.ClientID%>');

          textBox.value = ddl.options[ddl.selectedIndex].value;
      }
</script>

<asp:DropDownList runat="server" ID="DDLg4corp" onchange="ddlChange();">
  <asp:ListItem Value="1" Text="Option 1"></asp:ListItem>
  <asp:ListItem Value="2" Text="Option 2"></asp:ListItem>
  <asp:ListItem Value="3" Text="Option 3"></asp:ListItem>
  <asp:ListItem Value="4" Text="Option 4"></asp:ListItem>
</asp:DropDownList>

<asp:TextBox runat="server" id="txtg4action"></asp:TextBox> 


 Private Sub Button_Click(sender As Object, e As EventArgs) Handles btnSubmit1.Click,


With update
  .Connection = connection
  .CommandType = CommandType.Text
    If qrtSelect.SelectedValue = 1 Then
       .CommandText = "Update table set txtg4action=@txtg4action where users=@users"
       .Parameters.AddWithValue("@txtg4action", txtg4action.Text)
Posted
Updated 4-Jan-18 9:11am
v3
Comments
A_Griffin 4-Jan-18 11:48am    
On the face of it there's nothing wrong with that. YOu must be doing something else somewhere else that's causing your issue.
Member 9388700 4-Jan-18 11:54am    
When I get the value of txtg4action in vb, it returns an old value; not the value from the DDL. Edited and added some more code.
A_Griffin 4-Jan-18 12:28pm    
Well I don't know but the code you have will post back the selected value. Unless you maybe have EnableViewState = False in your page, maybe?
Member 9388700 4-Jan-18 13:36pm    
Screenshot of the debug: https://imgur.com/a/lozni

txtg4action.text should be "2". It displays as 2 on the page but when submitted, it still has the old value. Not sure where else to look or where else I could be going wrong. Do not have EnableViewState = False
A_Griffin 4-Jan-18 13:45pm    
WHat about in your page_load event? Are you re-setting its value there, not taking account of the PostBack?

1 solution

Well, as promised, here's a simple page using your code:
VB
<%@ Page Language="VB" %>
<script runat="server"> 
    Private Sub btnGo_Click(ByVal Sender As Object, ByVal e As EventArgs)
      litX.Text = "You selected " & txtg4action.Text
    End Sub
</script>
<html>
<head>
   <title></title> 
   <script type="text/javascript">
      function ddlChange() {
         var ddl = document.getElementById('<%=DDList.ClientID %>');
            var textBox = document.getElementById('<%= txtg4action.ClientID%>');
            textBox.value = ddl.options[ddl.selectedIndex].value;
         }
   </script>
</head>
<body>
    <form id="Form1" runat="server"> 	   
      <p><asp:DropDownList runat="server" ID="DDList" onchange="ddlChange();">
        <asp:ListItem Value="1" Text="Option 1"></asp:ListItem>
        <asp:ListItem Value="2" Text="Option 2"></asp:ListItem>
        <asp:ListItem Value="3" Text="Option 3"></asp:ListItem>
        <asp:ListItem Value="4" Text="Option 4"></asp:ListItem>
      </asp:DropDownList></p>
      <p><asp:TextBox runat="server" id="txtg4action"></asp:TextBox></p>  
      <p><asp:Button runat="server" ID="btnGo" OnClick="btnGo_Click" Text="Submit" /></p>
	  <p><asp:Literal runat="server" id="litX" /></p>	
    </form>
</body>
</html>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900