Click here to Skip to main content
15,898,371 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hi guys,

i was writing javascript, one simple function where i'm fetching values from the textboxes n
dropdownlist in asp.net

my code:


<asp:TextBox ID="StartDate" runat="server"></asp:TextBox>

 <asp:TextBox ID="EndDate" runat="server"></asp:TextBox>

  <asp:DropDownList ID="ddSection" Width="200px" runat="server">
                                            </asp:DropDownList>

<input id="Button1" type="button" value="Report" class="btn btn-success" onclick="hhmm()" />


function GetValues() {
                var Section = $('#ddSection option:selected').val();
                var StartDate = $('#StartDate').val();
                var EndDate = $('#EndDate').val();
                alert('hi...............' + Section + '-' + StartDate + '--' + EndDate);
            }


here i'm getting undefine for last three values,

can anyone plzzz help me


thanks
Posted
Comments
ZurdoDev 4-Jun-14 7:49am    
All you have to do is debug and you'll find the problem right away. Are these controls in a user control? If so, they won't be rendered with IDs as StartDate and EndDate. All you have to do is look at the source in your browser or just debug the code.
Anup kumar(MCA) 9-Jun-14 10:40am    
i read solution 2 .. what a funny question :)

Try these modifications:
$("#<%= ddSection.ClientID %>")
$("#<%= StartDate.ClientID %>")
$("#<%= EndDate.ClientID %>")

Read more: A-generic-way-to-find-ASPNET-ClientIDs-with-jQuery[^]
++++++[Part 2]+++++++
Try this simple example:
XML
<head runat="server">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js">
</script>
<script>
    $(function () {
        var ddl = "#<%= DropDownList1.ClientID %>";
        $(ddl).on("change", function () {
            var selectedItem = $(ddl + ' option:selected').val();
            alert(selectedItem);
        });
    });
</script>
</head>
<body>
    <form id="form1" runat="server">
    <asp:DropDownList ID="DropDownList1" runat="server">
    </asp:DropDownList>
    </form>
</body>
 
Share this answer
 
v4
Comments
abdul subhan mohammed 4-Jun-14 4:02am    
not working
Did you debug and see what is the exact issue?
Peter Leow 4-Jun-14 4:44am    
Try this example in solution 1.
you have written onclick="hhmm()" that's why it doesn't work replace it with onclick="GetValues()"

JavaScript
function GetValues() {
          var Section = $('#ddSection option:selected').val();
          var StartDate = $('#StartDate').val();
          var EndDate = $('#EndDate').val();
          alert('hi...............' + Section + '-' + StartDate + '--' + EndDate);
      }


ASP.NET
<asp:TextBox ID="StartDate" runat="server" ClientIDMode="Static"></asp:TextBox>
     <asp:TextBox ID="EndDate" runat="server" ClientIDMode="Static"></asp:TextBox>
     <asp:DropDownList ID="ddSection" Width="200px" runat="server" ClientIDMode="Static">
      <asp:ListItem Value="Nirav">Nirav</asp:ListItem>
     <asp:ListItem Value="Prabtani">Prabtani</asp:ListItem>
     </asp:DropDownList>
     <input id="Button2" type="button" value="Report" class="btn btn-success" onclick="GetValues()" />


try this i get both textbox value and dropdown value in alert, here i have bind dropdown manually for demo purpose check it.. :)
 
Share this answer
 
v2
Comments
abdul subhan mohammed 4-Jun-14 5:28am    
I have changed.. but problem in d javascript, dude.
Nirav Prabtani 4-Jun-14 7:47am    
try updated solution of mine ,Solution 2
HardikPatel.SE 9-Jun-14 6:04am    
my 5+
Nirav Prabtani 9-Jun-14 6:06am    
thank you.. :)
you can get values from server side control in jquery like this

$('#<%= StartDate.ClientID %>').val();


and also you have to change your function name hhmm() to GetValues()
 
Share this answer
 
Hi Abdul,

It works perfect as your requirement.

<div>
<asp:textbox id="TextBox1" runat="server" xmlns:asp="#unknown"></asp:textbox>
<asp:textbox id="TextBox2" runat="server" xmlns:asp="#unknown"></asp:textbox>
<asp:dropdownlist id="DropDownList1" runat="server" xmlns:asp="#unknown">
<asp:listitem text="Item1" value="item1"></asp:listitem>
<asp:listitem text="Item2" value="item2"></asp:listitem>
</asp:dropdownlist>
<asp:button id="Button1" runat="server" text="Button" onclientclick="check()" xmlns:asp="#unknown">
</asp:button>
</div>


Other scripts didnt help you right. So i wrote this script. Looks lil Odd. But works perfectly.

<script type=""text/javascript"><br" mode="hold" />    function check()
    {
        var txt1 = document.getElementById("<%=TextBox1.ClientID %>").value;
        var txt2 = document.getElementById("<%=TextBox2.ClientID %>").value;
        var sel = document.getElementById("<%=DropDownList1.ClientID %>");
        var selectedText = sel.options[sel.selectedIndex].innerHTML;
        alert('hi...............' + txt1 + '-' + txt2 + '--' + selectedText);
    }


Good Luck
 
Share this answer
 
Comments
Ajith K Gatty 5-Jun-14 5:48am    
This solution was down voted. May i know the reason so that i can learn from my mistake. Please. :)

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