Wednesday, 4 July 2012

How to get the selected value and Text from a dropdown list in ASP.NET using JavaScript?


How to get the selected value and Text from a dropdown list in ASP.NET using JavaScript?

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>How to get the selected value and Text from a dropdown list in ASP.NET using JavaScripttitle>

    <script type="text/javascript">
      functionshowdetail(flag)
      {
        var e=null;
        if(flag==1)
          e = document.getElementById("ddlHTML");
        else
          e = document.getElementById("ddlAspx");
        if(e.length==0)
        {
          alert('Please Select');
          return;
        }     
        alert('selected value: '+e.options[e.selectedIndex].value +' and selected text:  '+e.options[e.selectedIndex].text );
      }           
     
    script>

head>
<body>
    <form id="form1" runat="server">
    <div>
        HTML select(DropDown list)
        <br />
        <select id="ddlHTML" style="width: 100px">
            <option value="1">oneoption>
            <option value="2">twooption>
            <option value="3">threeoption>
        select>
        <br />
        <br />
        ASPX select(DropDown list)
        <br />
        <asp:DropDownList ID="ddlAspx" runat="server" Style="width: 100px">
            <asp:ListItem Value="1" Text="one">asp:ListItem>
            <asp:ListItem Value="2" Text="two">asp:ListItem>
            <asp:ListItem Value="3" Text="three">asp:ListItem>
        asp:DropDownList>
        <br />
        <br />
        <input type="button" class="button" tabindex="15" title="Add" value="Show HTML Value"
            onclick="showdetail('1')" />
        <input type="button" class="button" tabindex="15" title="Add" value="Show ASPX Value"
            onclick="showdetail('2')" />
    div>
    form>
body>
html>

Downloads  
You can download the complete source code in  C# 

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Powered by Blogger