DropDownList onChange: one TextBox changes to two TextBoxes to specify a date range?

Last post 03-19-2008 11:37 PM by shawpnendu. 6 replies.

Sort Posts:

  • DropDownList onChange: one TextBox changes to two TextBoxes to specify a date range?

    03-14-2008, 7:50 PM
    • Loading...
    • tmpuzer
    • Joined on 03-07-2008, 5:45 PM
    • Posts 315

     I have a gridview with a dropdownlist and a textbox to search the gridview.  The dropdownlist specifies the column to search and the text box holds the string to search for.   Like this:

    <asp:DropDownList ID="SearchByDropDownList" runat="server">
    <asp:ListItem Text="First Name" Value="first_name" />
    <asp:ListItem Text="Last Name" Value="last_name" />
    <asp:ListItem Text="Birth Date" Value="birth_date" />
    </asp:DropDownList>

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

    <asp:Button ID="SearchButton" runat="server" Text="Search" OnClick="SearchButton_Click" />
    <asp:Button ID="ClearButton" runat="server" Text="Clear" OnClick="ClearButton_Click" />

     
    What I want to happen is that when the user selects the "Birth Date" list item, the single text box changes into two text boxes so that the user can specify a date range (i.e., starting date and ending date).  If the user selects one of the non-date list items then the display would change back to a single text box.  Preferably it would be client-side, so that no round trip to the server is necessary just to change the display.  How can I do this?

    If a post helps me I'll always eventually mark it as an answer. But I frequently don't mark it right away because I feel once a thread is marked as answered, discussion tends to end. And I like to discuss things a bit.
  • Re: DropDownList onChange: one TextBox changes to two TextBoxes to specify a date range?

    03-15-2008, 1:20 AM

    HERE AN EXAMPLE HOW U CAN DO IT:
    TEST PURPOSE GET A NEW FORM(DON'T INCLUDE MASTER PAGE)
    UNDER FORM TAG PASTE THE BELOW CODE:
            <asp:DropDownList ID="SearchByDropDownList" runat="server" onchange="changeParameter();">    
            <asp:ListItem Text="First Name" Value="first_name" />    
            <asp:ListItem Text="Last Name" Value="last_name" />    
            <asp:ListItem Text="Birth Date" Value="birth_date" />
            </asp:DropDownList>       
            <asp:TextBox ID="SearchTextBox1" runat="server"></asp:TextBox>   
            <asp:TextBox ID="SearchTextBox2" runat="server" CssClass="hide"></asp:TextBox>   

    UNDER HEAD TAG PASTE THE BELOW SCRIPT+STYLE:
        <style type="text/css">.hide{display:none;}</style>
        <script language="javascript" type="text/javascript">
        <!--
        function changeParameter()
        {
            if (document.getElementById("SearchByDropDownList").selectedIndex <2)
                document.getElementById('SearchTextBox2').className= 'hide';
            else
                document.getElementById('SearchTextBox2').className= '';
        }
        //-->
        </script>
    NOW RUN THE PAGE & CHECK IT. IF U WANT TO INCLUDE MASTER PAGE LET ME KNOW.

    HOPE IT WILL HELP YOU.
    MARK AS ANSWER IF ITS HELPED YOU.

    Shawpnendu Bikash Maloroy
    System Engg.
    Grameenphone, Bangladesh
  • Re: DropDownList onChange: one TextBox changes to two TextBoxes to specify a date range?

    03-15-2008, 1:34 AM
    • Loading...
    • tmpuzer
    • Joined on 03-07-2008, 5:45 PM
    • Posts 315

    Thanks for your reply.  This page will use a master page.  How does this change things?  Does it change the way I have to reference the controls in the javascript? 

    If a post helps me I'll always eventually mark it as an answer. But I frequently don't mark it right away because I feel once a thread is marked as answered, discussion tends to end. And I like to discuss things a bit.
  • Re: DropDownList onChange: one TextBox changes to two TextBoxes to specify a date range?

    03-15-2008, 4:47 AM
    Answer
    YES. Here is my test page:

    <%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default13.aspx.cs" Inherits="Default13" Title="Untitled Page" %>

    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

    <script language="javascript" type="text/javascript">

    <!--

    function changeParameter()

    {

    if (document.getElementById("ctl00_ContentPlaceHolder1_SearchByDropDownList").selectedIndex <2)

    document.getElementById('ctl00_ContentPlaceHolder1_SearchTextBox2').className= 'hide';

    else

    document.getElementById('ctl00_ContentPlaceHolder1_SearchTextBox2').className= '';

    }

    //-->

    </script>

    <style type="text/css">.hide{display:none;}</style>

    <asp:DropDownList ID="SearchByDropDownList" runat="server" onchange="changeParameter();">

    <asp:ListItem Text="First Name" Value="first_name" />

    <asp:ListItem Text="Last Name" Value="last_name" />

    <asp:ListItem Text="Birth Date" Value="birth_date" />

    </asp:DropDownList>

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

    <asp:TextBox ID="SearchTextBox2" runat="server" CssClass="hide"></asp:TextBox>

    </asp:Content>

    HOPE IT WILL HELP YOU.
    MARK AS ANSWER if its helped you.

    Shawpnendu Bikash Maloroy
    System Engg.
    Grameenphone, Bangladesh
  • Re: DropDownList onChange: one TextBox changes to two TextBoxes to specify a date range?

    03-18-2008, 3:00 PM
    • Loading...
    • tmpuzer
    • Joined on 03-07-2008, 5:45 PM
    • Posts 315

     Okay this is working so now if the user chooses "Birth Date" from the drop down list they are presented with two text boxes to search (I even added a calendar control from the AJAX Control Toolkit).  However, how do I remember this state after a post back?  After a post back the two text boxes are hidden and the single text box is displayed.

    If a post helps me I'll always eventually mark it as an answer. But I frequently don't mark it right away because I feel once a thread is marked as answered, discussion tends to end. And I like to discuss things a bit.
  • Re: DropDownList onChange: one TextBox changes to two TextBoxes to specify a date range?

    03-19-2008, 3:59 PM
    • Loading...
    • tmpuzer
    • Joined on 03-07-2008, 5:45 PM
    • Posts 315

     I refactored the design a little bit, but basically I got this working after postback by registering the script as a startup script by calling RegisterStartupScript() from code behind and then calling the changeParameter() function after the javascript function definition.

    If a post helps me I'll always eventually mark it as an answer. But I frequently don't mark it right away because I feel once a thread is marked as answered, discussion tends to end. And I like to discuss things a bit.
  • Re: DropDownList onChange: one TextBox changes to two TextBoxes to specify a date range?

    03-19-2008, 11:37 PM

    OHH SORRY tmpuzer i forget to reply ur post.
    YES register client script in pageload event(outside of !ispostback method) will resolve ur problem. 

    Shawpnendu Bikash Maloroy
    System Engg.
    Grameenphone, Bangladesh
Page 1 of 1 (7 items)
Microsoft Communities
Page view counter