Showing posts with label Asp.Net jQuery. Show all posts
Showing posts with label Asp.Net jQuery. Show all posts

Friday, October 19, 2012

Asp.net jQuery dropdown change Text value


In this post, we will see on how to change the Text values of the items in a DropDown

To change the text values of the dropdown list items we can use the text() function as follows.  

$('yourDropDown option[value=1]').text ('New Text');

Here is a full example

Asp.net jQuery dropdown add items


In this post, we will see on how to add additional items to an existing Dropdown list

To add items to an existing dropdown we can use the append() function as follows.  $('yourDropDown') .append("<option value=\"1\">Test</option>");

Here is a full example

Asp.net jQuery clear dropdown items


In this post, we will see on how to clear the options which exist in a Dropdown list

To clear the existing items from a dropdown we can use the empty() function as follows.  $('yourDropDown').empty();

Here is a full example

Friday, August 10, 2012

Asp.Net jQuery loop through Dropdown


The jQuery library provides various options to work with Textboxes, in this post Asp.Net jQuery loop through Dropdown, we shall see on how to loop through all the Dropdown controls in the page and read the selected Index of each of the controls.

We can loop through all the Dropdowns in a page using the following jQuery script

// Loop Through DropDown and Get Selected IDs
$('select').each(function() {
alert($(this).attr('id') + ' => ' + $(this).prop("selectedIndex"));
});


Example
Create a new HTML/ASPX page 
Copy the below code.
Change the mapping of the jQuery file 
jquery-1.7.2.js to map to your local drive.
Run the application, Click on the Loop Dropdown Button to view the list of Dropdowns in the page and their selected index in message boxes.


<html xmlns="http://www.w3.org/1999/xhtml" >
<
head runat="server">
    <title>TextBox</title>
    <link type="text/css"
href="Stylesheet.css"
rel="Stylesheet" />
    <script
type="text/javascript"
src="JavaScript/jquery-1.7.2.js"></script>
    <script
type="text/javascript"
language="javascript">
        $(document).ready(function() {
        //
        // Loop All DropDowns
        //
        $('#cmdLoop').click(function(event) {
            // Disable Postback
            event.preventDefault();
            // Loop Through DropDown and Get Selected IDs
            $('select').each(function() {
                alert($(this).attr('id') + ' => ' +  
                      $(this).prop("selectedIndex"));
            });
        });
   });
    </script>
</head>
<body>
    <form id="fromDropDownList" runat="server">
    <div>
        Country:
        <asp:DropDownList
             ID="drpCountry"
             runat="server">
            <asp:ListItem
                 Value="USA">United States</asp:ListItem>
            <asp:ListItem
                 Value="UK">United Kingdom</asp:ListItem>
        </asp:DropDownList><br /><br />
       
        Loop through all TextBoxes in the page:
        <asp:Button
             ID="cmdLoop"
             runat="server"
             Text="Loop DropDowns" />
    </div>
    </form>
</body>
</html>

Asp.Net jQuery Textbox Enable


The jQuery library provides various options to work with Textboxes, in this post Asp.Net jQuery Textbox Enable, we shall see on how to enable an Asp.net Textbox control which was previously disabled.

We can disable a Textboxes using jQuery as follows.

$('#txtEnableDisable').prop("disabled", false);

Example
Create a new HTML/ASPX page 
Copy the below code.
Change the mapping of the jQuery file 
jquery-1.7.2.js to map to your local drive.
Run the application, Click on the Enable Radio Button to remove the disable Property



<html xmlns="http://www.w3.org/1999/xhtml">
<
head runat="server">
    <title>TextBox</title>
    <link type="text/css"
href="Stylesheet.css"
rel="Stylesheet" />
    <script
type="text/javascript"
src="JavaScript/jquery-1.7.2.js"></script>
    <script
type="text/javascript"
language="javascript">
        $(document).ready(function() {
            //
            // TextBox Enable
            $('#radEnableDisable_0').click(function() {
                if ($('#radEnableDisable_0:checked').is(':checked')) {
                    $('#txtEnableDisable').val('');
                    $('#txtEnableDisable').prop("disabled", false);
                    $('#txtEnableDisable').focus();
                }
            });
        });
    </script>
</head>
<body>
    <form id="frmTextBox" runat="server">
    <div id="pageControls">
        <table>
                        <tr>
                <td>Textbox (Enable/Disable)</td>
                <td>
                    <asp:TextBox id="txtEnableDisable" 
                                 runat="server"></asp:TextBox> 
                    <asp:RadioButtonList
                                 ID="radEnableDisable"
                                 runat="server"
                                 RepeatDirection="Horizontal">
                        <asp:ListItem
                                 Value="Enable"
                                 Text="Enable"></asp:ListItem>
                        <asp:ListItem
                                 Value="Disable"
                                 Text="Disable"></asp:ListItem>
                    </asp:RadioButtonList>
                </td>
            </tr> 
  </table>
    </div>
    </form>
</body>
</html>