Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
Hi,

I have one static dropdown. I need to change the value of dropdownon text change event.
The textbox(textPaid) and the drop down(ddlPaymentStatus) are placed inside a grid.
So, please give a javaScript solution to handle this.

Thanks in advance.


Eg: .if the amount is entered in text box() ,then the value in drop down() shld be changed as paid().. the static value of dropdown(open,paid, pending ,advance)
Posted
Updated 2-Dec-12 20:21pm
v4
Comments
Please post your codes by clicking on "Improve question" link.
aravindnass 3-Dec-12 2:22am    
.if the amount is entered in text box() ,then the value in drop down() shld be changed as paid()..here is the static value of dropdown <asp:ListItem Value="4">--Select-- <asp:ListItem Value="0">Open <asp:ListItem Value="1">Paid <asp:ListItem Value="2">Pending <asp:ListItem Value="3">Advance
So, what amount you will enter in the textbox.
Tell me how you want to select the dropdown. I mean what amount will make the dropdown to select the value "paid"?
aravindnass 3-Dec-12 3:21am    
if any value enter(on textbox) means it is paid and drop down will change paid..
aravindnass 3-Dec-12 2:07am    
haiii...tadit ..I have update my question ..plz see and give me a java script for dropdown changes based on text change in textbox

I worked this out for you. Take a look at the code.

JavaScript code
XML
<script type="text/javascript">

// Function is used to select the DropDown Paid value 
// if textbox contains any value.
function SelectPaidValue() {

      // Get the controls.
      var textPaid = document.getElementById('<%=textPaid.ClientID%>');
      var ddlPaymentStatus = document.getElementById("<%=ddlPaymentStatus.ClientID %>");

      // Check if data is there in textbox or not
      if (textPaid.value != "" && textPaid.value != null) {
          // If data is there select "Paid" from DropDownList.
          selectItemByValue(ddlPaymentStatus, 1);
      }
      else {
          // If data is not there, then select "--Select--" from DropDownList.
          selectItemByValue(ddlPaymentStatus, 4);
      }
}

// Function is used to select the DropDown option by value.
function selectItemByValue(elmnt, value) {
      // Loop through all the options and select the provided value.
      for (var i = 0; i < elmnt.options.length; i++) {
          if (elmnt.options[i].value == value)
               elmnt.selectedIndex = i;
          }
      }

</script>

aspx code
XML
<asp:TextBox ID="textPaid" runat="server" onblur="SelectPaidValue()"></asp:TextBox>
<asp:DropDownList ID="ddlPaymentStatus" runat="server">
        <asp:ListItem Value="4">--Select--</asp:ListItem>
        <asp:ListItem Value="0">Open</asp:ListItem>
        <asp:ListItem Value="1">Paid</asp:ListItem>
        <asp:ListItem Value="2">Pending</asp:ListItem>
        <asp:ListItem Value="3">Advance</asp:ListItem>
</asp:DropDownList>


Thanks...
 
Share this answer
 
v2
Comments
aravindnass 3-Dec-12 5:59am    
thanks a lot...dude...
Hi @aravindnass,

Please accept this answer and upvote, if it has helped you in any way.
This will help others to find the answer in one go and you will also be awarded with some points for this action...

Thanks,
Tadit
Hey you have not replied ?
Check my comment below.
Thanks...
aravindnass 3-Dec-12 6:09am    
the dropdown is placed inside a repeater..,How to get the element Id of dropdown...???
That's why I have written like "<%=ddlPaymentStatus.ClientID %>".
Are you facing issues in getting the dropdown with the current code ?
If yes, then tell me.
Try this

JavaScript
 <script type="text/javascript">
        function SomeFunction() {
var txt= document.getElementById('<%=Test.ClientID%>');
if(txt.value !="")
{
            var x=document.getElementById("<%=dropdownid.ClientID %>");
x.selectedIndex =1;
}
        }
    </script>



C#
<asp:TextBox ID="TextBox1" runat="server" onchange="SomeFunction()"></asp:TextBox>
 
Share this answer
 
v3
Comments
aravindnass 3-Dec-12 2:23am    
.if the amount is entered in text box() ,then the value in drop down() shld be changed as paid()..here is the static value of dropdown <asp:ListItem Value="4">--Select-- <asp:ListItem Value="0">Open <asp:ListItem Value="1">Paid <asp:ListItem Value="2">Pending <asp:ListItem Value="3">Advance
pradiprenushe 3-Dec-12 2:33am    
try my update solution
Solution is:
C#
 protected void ddlPaymentStatus_SelectedIndexChanged(object sender, EventArgs e)
{

        DropDownList ddl = (DropDownList)sender;
        GridViewRow row = (GridViewRow)ddl.NamingContainer;
        int rowIndex = row.RowIndex;
        
        DropDownList DrpLocation1 = (DropDownList)row.FindControl("DrpLocation");
       

        TextBox textPaid1= (TextBox)row.FindControl("textPaid");

        textPaid1=DrpLocation1.text;

}


Please Accept if my answer is right.
 
Share this answer
 
v2
Comments
aravindnass 3-Dec-12 2:10am    
heyy ...thanks for reply..
but what I need is change dropdown on textchange .plz give me javascript for thiss....
aravindnass 3-Dec-12 2:23am    
.if the amount is entered in text box() ,then the value in drop down() shld be changed as paid()..here is the static value of dropdown <asp:ListItem Value="4">--Select-- <asp:ListItem Value="0">Open <asp:ListItem Value="1">Paid <asp:ListItem Value="2">Pending <asp:ListItem Value="3">Advance
Arun kumar Gauttam 3-Dec-12 2:28am    
you need to just change condition on my upper code:
if(textPaid1.text="What value";
DrpLocation1.text="paid";

there is no need of java script, use only asp.net code

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